12

我创建了一个 Google 电子表格,我想找到该电子表格的 ID。我在谷歌上搜索了太多,但没有成功。

请告诉我我可以获取活动电子表格的电子表格ID的方式/脚本。

谢谢

4

4 回答 4

21

电子表格的 ID(键)在 URL 中(key= 和 #gid= 之间的部分)。您可以使用 GAS 检索它,例如:

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

请注意,通常每种方法都会得到完全不同的字符串,但它们的工作方式应该相同。

于 2012-07-05T07:33:48.187 回答
4

请记住,与来自 File 对象的 getId() 相比,来自 Spreadsheet 对象的 getId() 函数返回不同的 id,即使 File(由 DriveApp 管理)是同一个电子表格。

无论如何,如果您使用电子表格提供的 id 从 DriveApp 打开文件,您将获得正确的 File 对象,使用 getId() 返回“文件”id,这与您用来打开的不同文件。

这似乎令人困惑,但它确实如此。我在一些来自这个“双重”ID的脚本中遇到了一些问题。

于 2013-09-19T10:25:28.913 回答
0

该工作表存在于 电子表格 url 的
dedit之间。
示例 -:如果工作表 url 是

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

工作表ID是

1rV2_S2q5rcakOuHs2E1iLeKR2floRIozSytAt2iRXo8

有关更多信息,请访问google sheet api 官方文档

于 2018-12-09T03:45:13.050 回答
0

Google App Script 要获取特定文件夹中电子表格的电子表格 ID:

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
}

要获取当前电子表格 ID:

function getCurrentSSID(){
  let ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
  Logger.log(ssID);
}
于 2021-12-24T15:55:53.313 回答