我想创建一种“堆栈”,每次我删除一个项目时,工作表都会删除空白单元格。显然,我不能为此使用过滤器功能。
我无法读取为此目的创建的数组。
我非常伪代码:我创建一个空数组,获取所有值(包括空值),用除空值之外的所有值填充我的数组,最后清除堆栈并用我的数组设置值。
这是我的代码:
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);
}
}
我的代码中有很多注释,希望你能理解。我的测试表的链接:http: //bit.ly/1JiWutn
谢谢你的帮助 !