Cody
Cody

Reputation: 1

How do I reference specific cells on Google Sheets in a script? (Preferably when I'm offline)

I'd like to run a Google script that references specific cells on separate sheets (same project, different pages). Ideally, I would like to set a time trigger to run this while I'm offline.

I have used the active spreadsheet() function but since I want to reference multiple sheets that doesn't cover all my cases. (Again I'd like this to run while offline so I don't know if active sheet is the answer at all).

Upvotes: 0

Views: 107

Answers (2)

Luke
Luke

Reputation: 21

As mentioned, you can access any sheets in the active spreadsheet, then reference cells from each as necessary. Would look basically something like this (fixed for tehhowch's point in comments):

function example() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet1 = ss.getSheetByName('Sheet 1'); 
var sheet2 = ss.getSheetByName('Sheet 2');
var sheet3 = ss.getSheetByName('Sheet 3');
var sheet1value = sheet1.getRange('A1').getValue(); 
var sheet2value = sheet2.getRange('A1').getValue(); 
var sheet3value = sheet3.getRange('A1').getValue(); 

Edit: In answer to your question about running offline, getActiveSpreadsheet doesn't require the user to be online; a timed trigger will work fine.

Upvotes: 0

Chris
Chris

Reputation: 2107

The getActiveSpreadsheet() 1 function returns a Spreadsheet Object. From this Spreadsheet object you can get all of the sheets that exists within that spreadsheet.

If you mean that you want to access other spreadsheets, you can use the .getSpreadsheetById()2, which also returns a Spreadsheet object that you can use.

To clarify, a Spreadsheet can contain multiple Sheets (the individual tabs in a Spreadsheet).

Upvotes: 0

Related Questions