Chr*_*ung 4 email notifications google-sheets revisions google-apps-script
如果我有一个谷歌电子表格,例如
https://docs.google.com/spreadsheet/ccc?key=0AjAdgux-AqYvdE01Ni1pSTJuZm5YVkJIbl9hZ21PN2c&usp=sharing
我已经在上面设置了通知,以便在单元格发生变化时立即给我发送电子邮件。
我通过电子表格 API 对该电子表格进行了更改 - 即不是手动更改。
然后我收到一封这样的电子邮件:
主题:最近编辑了“通知测试”
查看您的 Google 文档“通知测试”中的更改:单击此处
其他人从 10/01/2014 12:23 到 12:23(格林威治标准时间)进行了更改
- 值已更改
如果我打开“单击此处”链接,则会得到此 URL,其中显示了电子表格中已更改的单元格:
我的问题是:
有没有办法以我可以以编程方式使用的格式(例如 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 以编程方式更新。
有任何想法吗?
小智 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
}
}
Run Code Online (Sandbox Code Playgroud)
然后从资源>当前项目的触发器中,您可以将checkMatch设置为每分钟运行一次。
另请查看https://developers.google.com/gdata/samples/spreadsheet_sample以将数据提取为 json
| 归档时间: |
|
| 查看次数: |
2211 次 |
| 最近记录: |