0

I'm confused about how to get a value out of the row2 array below. When I log the full array, as I've done in the code, I see all nine values listed that come from row two of the spreadsheet. However when I try to pull out the 9th value, also listed in the code, the result is undefined. Can anyone tell me why?

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // get the spreadsheet object
var reportConfig = spreadsheet.getSheetByName("Report Configuration"); // set report configuration sheet
var lastColumn = reportConfig.getLastColumn();
var lastColumnTitle = reportConfig.getRange(1,lastColumn).getValue();
var row2 = reportConfig.getRange(2,1,1,lastColumn).getValues();
Logger.log(row2);
Logger.log(row2[9]);

3
  • 2
    Arrays are indexed starting from 0, position 9 is row[8] Commented Nov 17, 2014 at 20:40
  • Yes, that's right. I made a typo in the code. I am getting a result of undefined with Logger.log(row2[8]) Commented Nov 18, 2014 at 0:26
  • Oooops, answer below is right indeed... didn't notice the getValues in your code. Sorry for misleading. Commented Nov 18, 2014 at 10:27

1 Answer 1

1

row2 will be a 2D-array, so row2[9] would refer to the 10th row in that array which doesn't exist. try: Logger.log(row2[0][9])

or change row2 to var row2 = reportConfig.getRange(2,1,1,lastColumn).getValues()[0]

and then do: Logger.log(row2[9])

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! That did the trick. I'm confused why it's a two-dimensional array though. If I'm using only one row of data, shouldn't it be a one-dimensional array? I appreciate the help.
.getValues() always returns a 2D-array, even for just one row.. But glad to hear the issue is solved !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.