Reputation: 115
When I run my script, I get this error message:
Syntax error: TypeError: Cannot read property "1" from undefined. line: ?
This is a snippet of my script:
var posConts = new Array();
var posContsData = SpreadsheetApp.openById(dataSheetValuesID).getSheets()[0].getRange('A:A');
var posContsDataValues = posContsData.getValues();
for (ii = 1; ii <= posContsData.getNumRows(); ii++) {
iii = ii-1;
posConts[iii] = posContsDataValues[ii][1];
}
I had no problems with using the collection in the values[0][0] format before.
Is this a bug?
Thanks.
Upvotes: 1
Views: 1046
Reputation: 46802
The array that you get with getValues is indexed by rows & columns so in your case (datarange = column A) there is only 1 column index and it's referenced by [0] since arrays are 0 indexed. AND you should not iterate beyond the array length. Try it like this :
for (ii = 1; ii < posContsDataValues.length; ii++) {
iii = ii-1;
posConts[iii] = posContsDataValues[ii][0];
}
best regards, Serge
Upvotes: 0
Reputation: 17752
You're confusion the indexes. Both arrays start on 0 (as all arrays in javascript). But you're using ii
which starts on 1 on posContsDataValues
and zero based iii
on posConts
. You're also accessing index 1
on each row, which is wrong, since you get only one column A:A
and it's also zero-based.
I'm not sure what you're trying to do, but from your snippet, here is a "fixed" loop.
for( var i = 0; i < posContsDataValues.length; ++i )
posConts.push(posContsDataValues[i][0]);
Upvotes: 3