我在这行代码中不断收到“未定义的错误”:
while (agentSheetValues[j][0] != "") {
...即使我使用Logger.log(agentSheetValues [j] [0]),它也会输出变量,所以我知道它是定义的。我也尝试用for循环替换while循环并遇到同样的问题;发生了什么事?
下面的完整代码,谢谢。
var resultSS = SpreadsheetApp.openById('someKey');
var agentSheet = resultSS.getSheetByName('Agents');
var agentSheetRows = agentSheet.getDataRange();
var agentSheetValues = agentSheetRows.getValues();
var agentSheets = [];
var j = 1;
while (agentSheetValues[j][0] != "") {
var sheetKey = agentSheetValues[j][1];
agentSheets.push(sheetKey);
j++
}
答案 0 :(得分:0)
您应该在循环中考虑您的数据数组可能在达到空值之前结束。试试这个:
var resultSS = SpreadsheetApp.openById('someKey');
var agentSheet = resultSS.getSheetByName('Agents');
//getDataRange returns from A1 to the last column and row WITH DATA.
//you'll not get the whole sheet. Blank rows and columns after the end are ignored
var agentSheetDataRange = agentSheet.getDataRange();
var data = agentSheetDataRange.getValues();
var agentSheets = [];
//I'm assuming you started 'j = 1' on purpose, to skip the header row
//Also, if you're sure you do not have another column with more data than A
// you could drop the:: && data[j][0] != ''
for( var j = 1; j < data.length && data[j][0] != ''; ++j )
agentSheets.push(data[j][1]);