14

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

ross
  • 2,684
  • 2
  • 13
  • 22

4 Answers4

25

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.

AdamL
  • 23,691
  • 6
  • 68
  • 59
  • 2
    Any ideas what the #gid is for? – Ali Feb 02 '13 at 19:54
  • 1
    @ClickUpvote it is the sheet ID. Use if you want to download as HTML/PDF/CSV etc – eddyparkinson Feb 17 '14 at 23:10
  • 1
    @ClickUpvote I understand that it's somehow late but [getSheetId()](https://developers.google.com/apps-script/reference/spreadsheet/sheet#getSheetId()) and [getGridId()](https://developers.google.com/apps-script/reference/spreadsheet/range#getGridId()) – contributorpw May 15 '15 at 11:18
4

Remember that the getId() function from a Spreadsheet object returns a different id compared with getId() from File object, even if the File (that is managed from DriveApp) is the same spreadsheet.

Anyhow, if you open a file from DriveApp using the id provided by the Spreadsheet, you will obtain the correct File object, returning - with getId() - the "File" id, which is different from the one that you used to open the File.

It seems confusing, but it works so. I had some issues in some scripts coming from this "double" id for the same thing.

  • Google Drive SDK is no longer able to open files using the id: SpreadsheetApp.getActiveSpreadsheet().getId() see: https://code.google.com/p/google-api-java-client/issues/detail?id=859&can=4&colspec=Milestone%20Priority%20Component%20Type%20Summary%20ID%20Status%20Owner – eddyparkinson Feb 17 '14 at 23:07
0

the sheet is is present in between the the
d and edit of the spreadsheet url.
example -: if the sheet url is

https://docs.google.com/spreadsheets/d/1rV2_S2q5rcakOuHs2E1iLeKR2floRIozSytAt2iRXo8/edit#gid=0

the sheet id is

1rV2_S2q5rcakOuHs2E1iLeKR2floRIozSytAt2iRXo8

for more information go to google sheets api official documentation

dinith jayabodhi
  • 531
  • 2
  • 8
  • 19
0

Google App Script To get the spreadsheet id of a spreadsheet in a specific folder:

function getSSID(){
  let folderID = "the id of the folder to search in"
  let folder = DriveApp.getFolderById(folderID);
  let SSName = "the name of the spreadsheet to search for"
  let allSSIDs = []; // empty array to hold the Id's of spreadsheets with the name SSName 
  let allMatching = folder.getFilesByName(SSName);
      while(allMatching.hasNext()){
        let ss = allMatching.next();
        allSSIDs.push(ss.getId());
      }

  Logger.log(allSSIDs); 
  // array of all spreadsheet ids if the spreadsheet had the name we are looking for
  // which hopefully there is only one that matches the exact spreadsheet name
}

To get the Current Spreadsheets ID:

function getCurrentSSID(){
  let ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
  Logger.log(ssID);
}
SudoHaris
  • 41
  • 3