从 Google 电子表格接收实时更新

use*_*017 6 google-sheets google-apps-script google-sheets-api

我正在尝试设置与 Google 电子表格的双向同步。我可以使用Google Sheets API V4将数据集中的更改推送到 Google 电子表格

现在,我希望每当有人实时或近实时地进行编辑或添加新行时,我都能够从 Google 电子表格中获取更新。

非常感谢任何为我指明正确方向的帮助。

Rey*_*cia 4

您可以通过转到“工具”->“通知规则”手动执行此操作。

在此输入图像描述

如果文件位于 Google Drive 中,您可以尝试使用推送通知:要使用推送通知,您需要执行三件事:

注册您的接收 URL 的域。例如,如果您打算用作//mydomain.com/notifications接收 URL,则需要注册//mydomain.com。设置您的接收 URL 或“Webhook”回调接收器。这是一个 HTTPS 服务器,用于处理资源更改时触发的 API 通知消息。为您要监视的每个资源端点设置通知通道。通道指定通知消息的路由信息​​。作为通道设置的一部分,您可以确定要接收通知的特定 URL。每当通道的资源发生更改时,Drive API 都会向该 URL 发送一条通知消息作为 POST 请求。

谷歌演练视频在这里

您还可以使用 Appscript。这个简单的 hack 来自这个SO 线程

var sheet = **whatever**;//The spreadsheet where you will be making changes
var range = **whatever**;//The range that you will be checking for changes
var compSheet = **whatever**;//The sheet that you will compare with for changes
function checkMatch(){
  var myCurrent = sheet.getRange(range).getValues();
  var myComparison = compSheet.getRange(range).getvalues();
  if(myCurrent == myComparison){//Checks to see if there are any differences
    for(i=0;i<compSheet.length;++i){ //Since getValues returns a 'multi-dimensional' array, 2 for loops are used to compare each element
     for(j=0;j<compSheet[i].length;++i){
      if(myCurrent[i][j] != myComparison[i][j]){//Determines if there is a difference;
       //***Whatever you want to do with the differences, put them here***
     }
    }

    myEmailer(sheet.getUrl());//Passes the url of sheet to youur emailer function 
    compSheet.getRange(range).setValues(myCurrent);//Updates compSheet so that next time is can check for the next series of changes
    }
  }
Run Code Online (Sandbox Code Playgroud)