【问题标题】:How to fetch the ID of a Google Spread Sheet via Google Apps Script?如何通过 Google Apps 脚本获取 Google 电子表格的 ID?
【发布时间】:2012-07-05 07:17:14
【问题描述】:

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

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

谢谢

【问题讨论】:

标签: google-apps-script google-sheets


【解决方案1】:

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

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

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

【讨论】:

  • 知道#gid 的用途吗?
  • @ClickUpvote 它是工作表 ID。如果您想以 HTML/PDF/CSV 等格式下载,请使用
  • @ClickUpvote 我知道有点晚了,但是getSheetId()getGridId()
【解决方案2】:

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

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

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

【讨论】:

【解决方案3】:

工作表存在于
d之间 和 编辑 的电子表格网址。
例子 -: 如果工作表 url 是

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

工作表ID是

1rV2_S2q5rcakOuHs2E1iLeKR2floRIozSytAt2iRXo8

欲了解更多信息,请访问google sheets api official documentation

【讨论】:

    【解决方案4】:

    Google 应用脚本 要获取特定文件夹中电子表格的电子表格 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2020-12-15
      • 2017-02-13
      相关资源
      最近更新 更多