4

如果我有一个 Google 电子表格,例如

https://docs.google.com/spreadsheet/ccc?key=0AjAdgux-AqYvdE01Ni1pSTJuZm5YVkJIbl9hZ21PN2c&usp=sharing

而且我已经在其上设置了通知,以便在单元格更改时立即给我发送电子邮件。

我通过电子表格 API 对该电子表格进行了更改 - 即不是手动更改。

然后我收到这样的电子邮件:

主题:“通知测试”最近被编辑

查看您的 Google 文档“通知测试”中的更改:单击此处

其他人从 10/01/2014 12:23 到 12:23(格林威治标准时间)进行了更改

  • 值已更改

如果我打开“单击此处”链接,则会得到这个 URL,它显示了电子表格中已更改的单元格:

https://docs.google.com/a/DOMAINGOESHERE/spreadsheet/ver?key=tn9EJJrk6KnJrAEFaHI8E3w&t=1389356641198000&pt=1389356621198000&diffWidget=true&s=AJVazbUOm5tHikrxX-bQ0oK_XEapjEUb-g

我的问题是:

有没有办法以我可以以编程方式使用的格式(例如 JSON)获取有关哪个单元格已更改的信息?

我查看了 Google 电子表格 API: https ://developers.google.com/google-apps/spreadsheets/

并在 Drive API Revisions: https ://developers.google.com/drive/manage-revisions

我还尝试使用 Google Apps 脚本设置 onEdit() 事件:https ://developers.google.com/apps-script/understanding_triggers

我认为最后一种方法就是答案。

这种方法的问题在于,虽然 onEdit 可用于通过电子邮件发送更改的详细信息,但它似乎只有在电子表格是手动编辑而我的电子表格是通过电子表格 API 以编程方式更新时才会被触发。

有任何想法吗?

4

1 回答 1

5

您可以构建一个检查更改的函数。一种方法是比较同一电子表格的多个实例。如果有差异,您可以给自己发电子邮件。使用时间驱动触发器,您可以检查每分钟、每小时、每天或每周(取决于您的需要)。

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
    }
  }

然后从资源>当前项目的触发器中,您可以将checkMatch设置为每分钟运行一次。

另请查看https://developers.google.com/gdata/samples/spreadsheet_sample以将数据提取为 json

于 2014-03-31T15:26:41.163 回答