6

Google Apps has some simple triggers: https://developers.google.com/apps-script/guides/triggers/

One of which is onEdit. As documented, onEdit fires for Google Spreadsheets, but it does not fire for Google Docs.

Is there a clean way to detect when the user has changed the document, and run a script when that happens?

Bryan P
  • 5,031
  • 3
  • 30
  • 44
dylanjha
  • 2,303
  • 4
  • 28
  • 47
  • I don't know anything about this API, but it looks like it might be worth taking a look at: [Files Watch](https://developers.google.com/drive/v2/reference/files/watch) – Alan Wells Sep 11 '14 at 20:37
  • 1
    Files Watch API is the one if you are not forced to used Apps script for your work. Btw, another similar q/a - http://stackoverflow.com/questions/19781406/google-doc-script-onedit – Ido Green Sep 12 '14 at 05:48
  • 1
    you might also be interested in reading [this answer](http://stackoverflow.com/questions/24773177/how-to-poll-a-google-doc-from-an-add-on/24773178#24773178) – Serge insas Sep 12 '14 at 14:19

1 Answers1

5

This is not a complete answer, but this may (not sure) help you based on your actual requirement. You can have a function like this and then using installed triggers you can call this function every minute to poll document and check whether someone has edited it or not.

 function isEdited()
    {
      var MyDoc = DocumentApp.getActiveDocument().getBody();
      var Text = MyDoc.editAsText().getText();
      var DocLen= Text.length;
      if(DocLen!= PropertiesService.getDocumentProperties().getProperty('DocLen'))
      {
        CallYourFunction();
        PropertiesService.getDocumentProperties().setProperty('DocLen', DocLen)
      }    
    }

Is 60 sec delay okey in your service... 60 sec is the maximum delay that can occur here.

Anees Hameed
  • 5,916
  • 1
  • 39
  • 43
  • 1
    Hello Anees, see also the post I linked to in question's comment, the update delay is about 2 seconds but it's not as simple as your solution of course. (upvoted yours for its simplicity) – Serge insas Sep 12 '14 at 14:22
  • 1
    Thanks Serge, thats' a realtime solution..... i was also looking for a such a kind of solution, I too was facing this 60 sec delay issue and was not sure on what to do next... Hey dylanjha, consider the one Serge pointed and not mine.... :-) – Anees Hameed Sep 12 '14 at 18:47