【发布时间】:2015-09-10 22:56:48
【问题描述】:
我想创建一种“堆栈”,每次我删除一个项目时,工作表都会删除空白单元格。显然,我不能为此使用过滤器功能。
我无法读取为此目的创建的数组。
我的伪代码:我创建了一个空数组,获取所有值(包括空值),用除空值之外的所有值填充我的数组,最后清除堆栈并用我的数组设置值.
这是我的代码:
function updateStack() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("main");
var zone = sheet.getRange(1, 1, 1, 10);
//seems that .getValues() returns a 2d array
var values = zone.getValues();
var j = 0;
var data = new Array();
for (var i = 0 ; i < 10 ; i++) {
//the problem seems to be here : I can't access the 2d array. After reading the debugging console about 1000 thousand times
// I discovered the 2 pairs of []
//I've found multiple ways to detect empty cells. Not sure if this is the right one. I've tried the .length = 0 trick, but something
// was wrong, maybe because of the "2dimensionality"
if (values[i] != "") {
data[j] = values[i];
j = j++;
} else {
// do nothing if the cell contains nothing
}
//not sure if I have to use return ! Don't know where to put it exactly too...
return data;
zone.clear();
//length of range must be the same as the array's length
zone = sheet.getRange(1, 1, 1, data.length);
zone.setValues(data);
}
}
我的代码中有很多cmets,希望你能理解。 我的测试表的链接:http://bit.ly/1JiWutn
感谢您的帮助!
【问题讨论】:
-
直到您提到删除空白单元格的工作表之前我都明白了...您可能需要包含一些中间步骤/信息。
-
感谢您指出这一点。我已经编辑过了,希望现在更清楚了。
标签: google-apps-script google-sheets google-apps-for-education