Retrieving Spreadsheet ID from Range using Google Apps Script

Gists

This is a sample script for retrieving spreadsheet ID from a range using Google Apps Script. I sometimes want to retrieve spreadsheet ID from ranges. In such case, I always use this.

  • Range
  • -> Retrieve Sheet using getSheet()
  • -> Retrieve Spreadsheet using getParent()
  • -> Retrieve spreadsheet ID
var id = "123456789abcdefg";
var sheet = "Sheet";
var cells = "a1:b10";
var range = SpreadsheetApp.openById(id).getSheetByName(sheet).getRange(cells);

var id = range.getSheet().getParent().getId();

>>> id ---> 123456789abcdefg

 Share!