问题
I'm trying to setup a two-way sync with a Google Spreadsheet. I can push changes in my dataset to a Google Spreadsheet using its Google Sheets API V4
Now, I'd like to be able to get updates from the Google spreadsheet whenever someone make edits or adds new row in real-time or near-real-time.
Any help that points me in the right direction is greatly appreciated.
回答1:
You can do this manually by going to Tools-> Notification rules..
If the file is in Google Drive, you can try and make use of Push Notifications: To use push notifications, you need to do three things:
Register the domain of your receiving URL. For example, if you plan to use
//mydomain.com/notificationsas your receiving URL, you need to register//mydomain.com. Set up your receiving URL, or "Webhook" callback receiver. This is an HTTPS server that handles the API notification messages that are triggered when a resource changes. Set up a notification channel for each resource endpoint you want to watch. A channel specifies routing information for notification messages. As part of the channel setup, you identify the specific URL where you want to receive notifications. Whenever a channel's resource changes, the Drive API sends a notification message as a POST request to that URL.
Google walkthrough video here.
You can also make use of Appscript. This simple hack is from this SO thread:
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
}
}
来源:https://stackoverflow.com/questions/38748534/receive-real-time-updates-from-a-google-spreadsheet