0

Whenever my spreadsheet is edited I want my separate Java code to be notified. Google Apps Script has Simple Triggers: https://developers.google.com/apps-script/guides/triggers/

I tried out the trigger for onEdit and tested it out by making it that whenever my spreadsheet was edited I'd get an email. This worked. Now I'd like to connect the script to my Java code: https://developers.google.com/apps-script/guides/rest/quickstart/java

I went through the quickstart with the sample appscript and it worked. The java file printed out the contents of the root folders of Google Drive. When I tried to apply the same steps to my onEdit script I get an Permissions Denied error in terminal.

I'm wondering if it's because the simple trigger is bound to the spreadsheet and the Execution API is for unbounded scripts?

How can I connect the onEdit function to Java code?

Here's my error message:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "The caller does not have permission",
    "reason" : "forbidden"
  } ],
  "message" : "The caller does not have permission",
  "status" : "PERMISSION_DENIED"
}
        at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
        at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
        at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
        at Quickstart.main(Quickstart.java:179)
abielita
  • 13,147
  • 2
  • 17
  • 59
seal308
  • 131
  • 2
  • 6
  • I am not sure how you applied the same steps to onedit. On edit trigger only triggers app script code and not a code on your computer. You will have to poll for changes with your java script to figure out if any edit has been made. Equivalently you could rewrite your code in google app script such that it is triggered on edit. – Jack Brown May 02 '17 at 14:13
  • @JackBrown I thought the guide here: https://developers.google.com/apps-script/guides/rest/quickstart/java would connect your app script code to my java code. So yes, onedit works on apps script code. But I thought you could connect that to your java code so I don't have to keep polling for it once a day or something. – seal308 May 02 '17 at 14:27
  • Based from this [related post](http://stackoverflow.com/questions/32476746/com-google-api-client-googleapis-json-googlejsonresponseexception-403-forbidden), try adding `SheetsScopes.DRIVE` to the scopes to be given in `authorize()`. You may also create a new directory and it will authenticate and save the credential to the newly created directory. Here's another reference which might help: http://stackoverflow.com/questions/32920443/why-does-my-apps-script-deployed-as-api-executable-return-permission-denied – abielita May 02 '17 at 15:13
  • The execution api won't call your java code from apps script it calls apps script from your java code. You would need some interface such as HTTP or pub/sub that is exposed in you java app. As for you 403 make sure the OAuth creds you are using are from the project you are trying to call. Make sure your scopes match, look at Files->Project Properties->Scopes to get the specific scopes you need. – Spencer Easton May 02 '17 at 17:16
  • @SpencerEaston Could it be possible for the java code to call the apps script and the apps script to return a value indicating that the spreadsheet has been edited/changed? – seal308 May 03 '17 at 20:21
  • @seal308 Yup you can do that. That is the intent of the Execution API. – Spencer Easton May 04 '17 at 00:37
  • @SpencerEaston I see, so I would have to have my java code keep polling the spreadsheet anyways if I use appscript because my app script can't message the java code on its own. The java code must call it. – seal308 May 04 '17 at 02:14

0 Answers0