按索引访问嵌套数组时的Google Apps脚本错误 [英] Google Apps Script error when accessing nested array by index
问题描述
当我尝试使用双索引访问嵌套数组时,Google Apps脚本引发错误,它说:TypeError:无法从未定义中读取属性"3".(第27行,文件代码")
Google Apps Script is raising an error when I try to access the nested array with double indexing, it says: TypeError: Cannot read property "3" from undefined. (line 27, file "Code")
这是代码:
var ss = SpreadsheetApp.openById("SpreadsheetID");
var sheetMAT = ss.getSheetByName("Sheet3");
var data = sheetMAT.getRange(3, 2, sheetMAT.getLastRow() - 1, 4).getValues();
var temporaryData = [];
var dataReadyLine = [];
function getReadyLine() {
var rawData = sheetMAT.getRange(3, 2, sheetMAT.getLastRow() - 1, 4).getValues();
Logger.log(rawData[0][3]);
for (var i=0; i<=rawData.length; i++) {
if (rawData[i][3] === "A Ready Line") {
temporaryData.push(data[i][1], data[i][0]);
dataReadyLine.push(temporaryData);
temporaryData = [];
}
}
return dataReadyLine;
};
行'Logger.log(rawData [0] [3]);'成功地打印了嵌套数组项的值,但是当涉及到IF条件时,它将给出未定义的错误.为什么会出现此错误?我该如何使FOR循环起作用?
The line 'Logger.log(rawData[0][3]);' successfully prints the value of the nested array item but when it comes to IF conditional it gives the error of undefined. Why is it giving this error? How can i make the FOR loop work?
这是我尝试运行代码时出现错误的打印屏幕:打印屏幕
Here is the print screen with the error when I try to run the code: Print Screen
推荐答案
对 for
循环进行以下修改如何?
How about a following modification for the for
loop.
发件人:
for (var i=0; i<=rawData.length; i++) {
收件人:
for (var i=0; i<=rawData.length - 1; i++) {
或
for (var i=0; i<rawData.length; i++) {
Array
的索引从 0
到 Array.length-1
.因此, for(var i = 0; i< = rawData.length; i ++){
在rawData.length处发生错误.
Index of Array
is from 0
to Array.length - 1
. So for (var i=0; i<=rawData.length; i++) {
occurs an error at rawData.length.
作为另一个表达式,您可以使用
As another expression, you can use
for (var i in rawData) {
在这种情况下,您还可以通过 rawData [i] [3]
检索元素.
In this case, you can also retrieve elements by rawData[i][3]
.
这篇关于按索引访问嵌套数组时的Google Apps脚本错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!