我在每行的第一个单元格中都有带有 chekBoxes 的 FlexTable,当 checkBox 为真时,来自 FlexTable 行的数据被收集在变量中。现在我需要创建包含表格的文档,该表格包含来自变量的数据的表格。我试图将字符串的值存储在 Hidden 中,但它不起作用并且无法弄清楚如何实现它。我所有的(虽然代码不是我的,代码几乎是@Sergeinsas 的一半)代码都可以在这里找到:http ://pastebin.com/aYmyA7N2 ,提前谢谢你。
问问题
1412 次
1 回答
1
您的代码中有一些错误......像这样的小部件hidden
只能具有字符串值,并且它们只能在您检索它们的值时返回字符串值。
将数组转换为字符串(和返回)的一种可能且简单的方法是使用 and 的组合join()
,split()
这是有效的修改后的代码(仅相关部分)。
// Storing checked rows
function check(e) {
var checkedArray = [];
var data = sh.getRange(1,1,lastrow,lastcol).getValues();
for(var n=0; n < data.length;++n){
if(e.parameter['check'+n]=='true'){
checkedArray.push(data[n].join(','));// convert data row array to string with comma separator
}
}
var hidden = app.getElementById('hidden');
hidden.setValue(checkedArray.join('|'));// convert array to string with | separator
return app;
}
function click(e) {
var hiddenVal = e.parameter.hidden.split('|');// e.parameter.hidden is a string, split back in an array of strings, each string should be splitted too to get the original array of arrays
var d = new Date();
var time = d.toLocaleTimeString();
var table = []
for(var n in hiddenVal){
table.push(hiddenVal[n].split(','));// reconstruction of a 2D array
}
DocumentApp.create('doc '+time).getBody().appendTable(table);// the table is in the document
}
此处提供完整代码
编辑:建议:如果您将标题放在电子表格中,您可以很容易地在最终表格中检索它们,如下所示:
function check(e) {
var checkedArray = [];
var data = sh.getRange(1,1,lastrow,lastcol).getValues();
checkedArray.push(data[0].join(','));// if you have headers in your spreadsheet, you could add headers by default
for(var n=0; n < data.length;++n){
if(e.parameter['check'+n]=='true'){
checkedArray.push(data[n].join(','));
}
}
您也可以data[0]
在doGet
函数中使用来构建您的标头UI
,我认为这将使您的代码更易于维护,而无需对数据进行硬编码......但这只是一个建议;-)
于 2013-08-06T08:36:32.800 回答