【问题标题】:Google Sheets API: get the name of the tab that was last editedGoogle Sheets API:获取上次编辑的标签的名称
【发布时间】:2018-09-18 15:41:00
【问题描述】:

我正在尝试找出电子表格中的哪个选项卡上次更改。

我有一个谷歌表格,其中包含多个用户共享的多个标签。我可以检测到用户何时进行了这样的更改:

def testChanges(self):
    response = self.service.revisions().list(fileId='some-file-id').execute()
    revs = response.get('revisions',[])
    if not len(revs):
        return
    revID = revs[-1].get('id')
    response = self.service.revisions().get(
                            fileId='some-file-id',
                            revisionId=revID,
                            fields='kind, id, modifiedTime, lastModifyingUser'
                            ).execute()

我得到了回应:

{
  "kind": "drive#revision",
  "id": "12",
  "modifiedTime": "2018-09-17T20:10:41.330Z",
  "lastModifyingUser": {
    "kind": "drive#user",
    "displayName": "USER NAME",
    "me": true,
    "permissionId": "08921584708523400585",
    "emailAddress": "email.address@gmail.com"
  }

现在我需要处理用户刚刚更改的选项卡。我怎么能弄清楚呢?我检查了Drive documentationSheets API,但到目前为止还没有运气。

【问题讨论】:

    标签: python google-sheets google-drive-api


    【解决方案1】:

    我认为 REST API 不支持或不可能做到这一点。您可能需要创建一个installed edit 触发器,并使用它来记录edit event object(例如,将其发布到 URL,将其附加到一些“哨兵”电子表格等)。您可以将此与您当前的 revisions 方法结合使用,以了解谁执行了修改(因为用户身份通常是受限信息,除非您在同一个 GSuite 域中)。

    将数据写入指定电子表格的示例。

    function installedEdit(e) {
      const edited = e.range,
            sheet = edited.getSheet();
      const row = [
        new Date(),         // Time of the edit
        e.source.getId(),   // id of the edited workbook.
        e.source.getName(),
        sheet.getName(),    // edited sheet name
        sheet.getSheetId(), // gridid for REST API
        edited.getA1Notation(),
        edited.oldValue !== undefined ? edited.oldValue : "",
        edited.value !== undefined ? edited.value : ""
      ];
      try { row.push(e.user.getEmail()); }
      catch (e) { row.push("Unknown User"); }
      // Log this row somehow
      SpreadsheetApp.openById("some spreadsheet id").getSheetByName("Edit log").appendRow(row);
    }
    

    您可以在独立的 Apps 脚本项目中创建此函数,并 programmatically install 此函数作为您需要查看的各种电子表格的编辑触发器。

    【讨论】:

    • 我认为这是一个很好的解决方法。例如,使用the custom properties of Drive API 怎么样?在这种情况下,它通过 GAS 使用可安装的触发器放置值,并且可以通过 python 使用 Drive API 检索值,而无需添加和修改范围和 API。因为需要的信息很少,我想应该可以用。如果这对 OP 的情况没有用,请忽略它。
    【解决方案2】:

    您可以创建一个 for 循环来扫描所有选项卡(每隔几秒)并将所有选项卡的 modifiedTime 存储在一个表中。对循环进行编程,以将每个选项卡的当前 modifiedTime 与上次扫描的值进行比较。当不匹配时,这意味着当前选项卡已被修改。

    只是一个想法。

    【讨论】:

    • 如何获取特定标签的修改时间?
    • 我从您提交的回复中看到有一个 modifiedTime 字段。 (“修改时间”:“2018-09-17T20:10:41.330Z”)。您对每个选项卡都有此字段吗?每次标签更新时,此值都会更改。
    猜你喜欢
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多