Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Google Spreadsheet via Google Apps Script

Is it possible to open a Google Spreadsheet via Google Apps script?
I'm trying to create a way to link to a certain cell in a spreadsheet, but I can't manage to open a sheet from a script (deployed as a web app).
I want to open the spreadsheet and then set the active sheet and range (I know how to do the latter, when the spreadsheet is opened).

Thanks in advance!

EDIT
Here's my current code:

function doGet(e){
  var ss = SpreadsheetApp.openById("[id]");
  SpreadsheetApp.setActiveSpreadsheet(ss);
  var sheet = ss.getSheetByName("Kunder");
  var row = Number(e.parameter.row); 
  var col = Number(e.parameter.col); 
  var range = sheet.getRange(row, col);

  //OPEN THE SPREADSHEET HERE

  SpreadsheetApp.setActiveSheet(sheet);
  SpreadsheetApp.setActiveRange(range);
}
like image 329
Punchlinern Avatar asked Jun 16 '26 18:06

Punchlinern


1 Answers

You need to use the correct ID for the spreadsheet. One way to find it is:

  1. Open the containing folder in Google Drive
  2. Right-click the spreadsheet
  3. Select Get link
  4. Copy the URL shown

The URL will be in the form: https://drive.google.com/open?id=xoxoxoxoxoxoxoxoxoxoxoxo

Use the id parameter value as the argument to openById, e.g.:

var ss = SpreadsheetApp.openById("xoxoxoxoxoxoxoxoxoxoxoxo");

like image 105
jondoig Avatar answered Jun 19 '26 13:06

jondoig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!