简体   繁体   中英

Google script/javascript | why does getRange return 2D array

So I was trying to get values from getRange in google script but I noticed it was returning a 2D array. I was just wondering why it is returning a 2D array instead of just 1D array.

A spreadsheet range can be a single cell or a range of rows and columns, for single cells you can use the getValue() method which returns a single object (string or number or whatever is in that cell) but when multiple cells are selected we have to be able to know in which row and column is each element, therefore the choice of an array of row values, each set of row values itself being an array of values.

You'll notice that in this latter case the method to use is getValues() (with an S).

The getValues() method returns an array of arrays to keep track of the sheet row-Column structure for multiple cells selection.

If ever you try to use getValues() for a single cell or a single row (or a single column), the result will still be a 2 dimension array but its length and/or width will be 1. It would have been very disturbing if the nature of the returned value had been different depending on the selection, don't you think ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM