2

I want to get Multiple values, so use getValues but it returns array unexpected dimentional.
In below code, sheet is an object that got SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Example");

var rng = sheet.getRange(2,1,3);    
var rngVls = rng.getValues();

Logger.log(rng.getValue()); // log1
Logger.log(rngVls);         // log2
Logger.log(rngVls[0]);      // log3

I expected the results like below. log2 is what I want to get.

  • log1 = Bravo
  • log2 = [[Bravo],[Charlie],[Delta]]
  • log3 = Bravo

But unexpected dimension appears like below. I cant understand why.

  • log1 = [Bravo,[]]
  • log2 = [[[Bravo],[Charlie],[Delta]],[]]
  • log3 = [[Bravo],[]]

Please help me, how can i get a 2-dimentional B-C-D array??


-added APOLOGY-

uhhhhhh Im so sorry for wasting your time, Its just my stupid mistake. I had read not 'Log' but 'Running transcript'. I got what i want, thank you all kindness. i feel so down lets code.

2
  • Provide Logger.log(rngVls.length) and Logger.log(rngVls[0].length) Commented Jan 30, 2019 at 12:15
  • Use the 4-parameter signature, i.e 2, 1, 3, 1? Commented Jan 30, 2019 at 13:32

1 Answer 1

1

getValues() is returning a 2D array. Don't rely on loging an object to the log to determine its attributes. Instead use the appropriate JavaScript 1.6 methods like typeof, instanceof, JSON.stringify(object), etc.

Example:

function logSomeThings(){
  var sheet = SpreadsheetApp.getActiveSheet();
  var rng = sheet.getRange(2,1,3);    
  var rngVls = rng.getValues();

  Logger.log('log1 %s', typeof rng.getValue()); 
  Logger.log('log2 %s', rngVls instanceof Array);
  Logger.log('log3 %s', rngVls[0] instanceof Array);
  Logger.log('log4 %s', typeof rngVls[0][0]); 
  Logger.log('log5 %s', typeof rngVls[0][0][0]); 
}

Result

[19-01-31 10:32:24:187 CST] log1 number
[19-01-31 10:32:24:188 CST] log2 true
[19-01-31 10:32:24:189 CST] log3 true
[19-01-31 10:32:24:189 CST] log4 number
[19-01-31 10:32:24:190 CST] log5 undefined

Regarding JSON.stringify(rngVls);

Logger.log('log6 %s', JSON.stringify(rngVls)); 

logs something like this

[19-01-31 11:02:20:061 CST] log6 [[1],["#N/A"],[2]]

Notes:

  1. getValue() returns the top-left value of the referred range.
  2. The Google Apps Script IDE doesn't has the same capabilities as other tools like the console of Google Chrome's Developer Tools.

Related

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

1 Comment

thanks , your comment make me notice that I read some different text. (content is big benefits too)

Your Answer

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