最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error while looping backwards through an array - Stack Overflow

programmeradmin1浏览0评论

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]);

}
}

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]);

}
}
Share Improve this question edited Jan 29, 2015 at 0:33 Munkey asked Jan 28, 2015 at 23:55 MunkeyMunkey 95613 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

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]);
}

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--){
发布评论

评论列表(0)

  1. 暂无评论