How to fetch the ID of a Google Spread Sheet via Google Apps Script?

google-apps-script, google-sheets

Solution

The ID (key) of the spreadsheet is in the URL (the part between key= and #gid=). You can retrieve it with GAS using something like:

function getId() {
  Browser.msgBox('Spreadsheet key: ' + SpreadsheetApp.getActiveSpreadsheet().getId());
}

Note, often you will get a totally different string with each method, but they both should work the same.

Problem

I've created one Google spread sheet and I want to find the ID of that spread sheet. I've tried too much to search on Google but could not succeed. Please show me the way / script by that I can fetch the spreadsheet ID of an Active spread Sheet.. Thanks

Original source