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.

Advertisements

About Finn Smith

Oregon... some places.... SF. Working in education, technology, and social impact (or some combination of the three).
This entry was posted in Google Apps Script. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s