【问题标题】:Google Sheet API - How to get only updated rowsGoogle Sheet API - 如何仅获取更新的行
【发布时间】:2020-03-06 23:25:35
【问题描述】:

我无法在文档中找到有关如何仅从 Google Sheets API 获取更新的记录/行的信息。

有没有办法,我可以获得每条记录上次修改时的时间戳?

任何可以解决此问题的指导或链接。

谢谢!

【问题讨论】:

    标签: google-sheets-api google-api-nodejs-client


    【解决方案1】:

    写操作:

    对于 $response = $service->spreadsheets_values->update() 过程(创建后写入文件),响应如下:

    Google_Service_Sheets_UpdateValuesResponse Object
    (
        [spreadsheetId] => XXX
        [updatedCells] => 7
        [updatedColumns] => 7
        [updatedDataType:protected] => Google_Service_Sheets_ValueRange
        [updatedDataDataType:protected] => 
        [updatedRange] => Sheet1!A1:G1
        [updatedRows] => 1
        [internal_gapi_mappings:protected] => Array
            (
            )
    
        [modelData:protected] => Array
            (
            )
    
        [processed:protected] => Array
            (
            )
    
    )
    

    获取行 = $response->getUpdatedRows();

    获取单元格 = $response->getUpdatedCells();

    获取列= $response->getUpdatedColumns();

    等等……

    追加操作:

    对于 $response = $service->spreadsheets_values->append() 流程,响应如下:

    Google_Service_Sheets_AppendValuesResponse Object
    (
        [spreadsheetId] => XXXX
        [tableRange] => Sheet1!A1:G1
        [updatesType:protected] => Google_Service_Sheets_UpdateValuesResponse
        [updatesDataType:protected] => 
        [internal_gapi_mappings:protected] => Array
            (
            )
    
        [modelData:protected] => Array
            (
                [updates] => Array
                    (
                        [spreadsheetId] => XXXX
                        [updatedRange] => Sheet1!A2:G7
                        [updatedRows] => 6
                        [updatedColumns] => 7
                        [updatedCells] => 42
                    )
    
            )
    
        [processed:protected] => Array
            (
            )
    
    )
    

    获取行 = $response->getUpdates()->getUpdatedRows();

    获取单元格 = $response->getUpdates()->getUpdatedCells();

    获取列= $response->getUpdates()->getUpdatedColumns();

    等等……

    【讨论】:

      【解决方案2】:

      您无法直接使用 Sheets API 执行此操作。不过,您可以跟踪文件 using Drive API 中的更改,但我认为这不是您想要做的。

      我建议使用 onEdit trigger 使用 Apps 脚本。每次修改电子表格时,您都可以检索 the data of the edited range 并将其存储在某处,以及当前日期。

      可能是以下几行:

      function onEdit(e) {
        var timestamp = new Date();
        var range = e.range;
        var editedRow = range.getRow();
        // Store timestamp and editedRow index somewhere you can retrieve it later (it could be in the spreadsheet itself)
      }
      

      更新:

      您可以使用Apps Script API 远程创建触发器。首先您应该创建一个项目bound to your spreadsheet,然后通过调用projects.updateContent 添加相应的代码(您应该添加两个文件,脚本本身,其中包含onEdit 触发器,以及清单文件)。请注意,您只能通过此 API 使用简单的触发器,而不是可安装的触发器。但在你的情况下,这已经足够了。

      希望对你有帮助。

      【讨论】:

      • 哇哦,你给了我一个新的思考过程。是否可以通过 API 在 google sheet 上创建此触发器?
      • 您可以使用 Apps Script API 做到这一点。我用这个更新了我的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 1970-01-01
      • 2021-02-05
      • 1970-01-01
      • 2017-03-29
      相关资源
      最近更新 更多