Munkey
Munkey

Reputation: 956

Error while looping backwards through an array

I started working on something and literally hit a roadblock early on, tried to think my way through the problem early on with my limited knowledge of javascript.

I'm sure it's something simple and annoying, but i can't seem to see what is causing the error.

Just working on a script to evenutally show data from a google spreadsheet using google scripts. But yeah, cannot seem to loop through specific values in reverse.

Please see my annotated code below and thanks in advance.

simple attempt to loop through an array forwards. decided to go to basics as i'm stumpted. Works. Logger shows i going up by 1

function getInfo() {
var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
var values = sheet.getDataRange().getValues();


for(i=0;i < values.length;i++){
Logger.log([i]);

}
}

same as the above, except i'm trying to go through the array in reverse. works but so far we're only asking it what part of the loop we're in.

function getInfo() {
var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
var values = sheet.getDataRange().getValues();


for(i=values.length;i > 0 ;i--){
Logger.log([i]);

}
}

strap your pants on time, because we are asking the array for data, looping forwards. It works. it returns the whole line of each entry in the array

function getInfo() {
var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
var values = sheet.getDataRange().getValues();


for(i=0;i < values.length;i++){
Logger.log([i]+" " + values[i]);

}
}

let's moonwalk it and try looping in reverse again. Cause i'm bad, i'm bad, heee heeeh, oww. It works :)

function getInfo() {
var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
var values = sheet.getDataRange().getValues();


for(i=values.length;i > 0 ;i--){
Logger.log([i]+ " " + values[i]);

}
}

After putting on my big boy pants, I was on the way to log a single entry in my array,

looping through it forwards. Hurrah, pants remained clean and results were as expected.

function getInfo() {
var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
var values = sheet.getDataRange().getValues();


for(i=0;i < values.length;i++){
Logger.log([i]+" " + values[i][16]);

}
}

So why oh why doesn't the same work in reverse and always kickup an error "(TypeError: Cannot read property "16" from undefined. (line 69, file "Code")Dismiss" Line 69 being ( Logger.log([i]+ " " + values[i][16]); ) in the code below.

function getInfo() {
var sheet = SpreadsheetApp.openById("ID HERE").getSheetByName("Sheet1");
var values = sheet.getDataRange().getValues();


for(i=values.length;i > 0 ;i--){
Logger.log([i]+ " " + values[i][16]);

}
}

Upvotes: 0

Views: 1682

Answers (2)

MysteriousMrEies
MysteriousMrEies

Reputation: 41

Arrays start from index 0, so the last index in the array will be at length -1, so:

for(i=values.length;i > 0 ;i--){

should be

for (i=values.length-1; i >= 0; i--){

Upvotes: 4

Stefano Dalpiaz
Stefano Dalpiaz

Reputation: 1673

Think about the indexes you should use for you 'for' loop. When you cycle an array backwards, you want to start from the last index, and end with the first one.

The last index of an array a, as you probably know, is a.length - 1, while the first one is 0. You want to include them both both in your loop, so your code to cycle backwards is:

for (i = values.length - 1; i >= 0; i--) {
    Logger.log(i + " " + values[i][16]);
}

Upvotes: 6

Related Questions