Question

I'm developing a Web App using google apps script and a spreadsheet as storage.

Basically, an HTML showing some tables for the different tabs.

From my app, users can add new tasks, edit tasks, and mark tasks as completed. Since there are many users using the app, the data showed on each client will get outdated very fast.

I would like to update the app with new records and changes to the existing ones as soon as possible.

I thought in logging the last edit tab+row in a cell and pull that data from the server every minute, but, what if many entries are added/edited during that minute?

I think WebSocket is not possible. Any other idea?

I'm using JQuery client-side.

Was it helpful?

Solution

To help avoid conflicts, give every task a unique ID. Something like creation time + random string. That way you can look it up in the spreadsheet. Also, I think the Lock Service can prevent concurrent edits temporarily to avoid conflicts: https://developers.google.com/apps-script/reference/lock/

To check for updates, try polling the last edit time of the spreadsheet. If it's greater than the previous poll, fetch updates. https://developers.google.com/apps-script/reference/drive/file#getLastUpdated()

OTHER TIPS

No other way besides polling. You can't have sockets or callbacks from HTML service. You could poll frequently but that may run you out of quotas. If you really want to poll and avoid quotas you can log the last edit on a published public spreadsheet and read it with ajax from the client, however published spreadsheets update every minute only.

You could try something like this:

var lock = LockService.getPublicLock();
var success = lock.tryLock(10000);
if (success) {
// check your spreadsheet for lastUpdated or PropertiesService.getScriptProperties();
}
} else {
// do something else (try it again or error msg)
}
lock.releaseLock(); 

I have found that it works well on my app and I have around 1000 users.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top