Tuesday tip: Ranges as objects in Google Apps Script

Maybe this will be a regular thing. Next week will be my last at NYU, so I’ve had lots of opportunities to explain the things I do. As I do, I’m seeing colleagues that I’m training running into some of the same questions I’ve had in the past about Google Apps Script and the various Google products that we use at the university.

Range != Values in Range

Google Apps Script’s Sheet class has a method called getRange(). What’s important is that it only returns the range, not the values in the range. 

Example of Google Apps Script code
Example of how NOT to use getRange()

The difference may be obvious to you, but it wasn’t to me. For Google spreadsheets (and Excel, for that matter), a range is like a geographic location on a sheet, while the values in it are separate objects.

For example, this code will log “undefined” for each row that you’re selecting in the spreadsheet.

To actually get the values in the range, you need to include the method getValues(). getRange() alone can be useful if you need to take action on the spreadsheet itself (for example, changing colors or fonts) , and not the values that it holds.