onEdit and UrlFetchApp

772 views
Skip to first unread message

N A

unread,
Aug 1, 2022, 5:45:22 AM8/1/22
to Google Apps Script Community
Hi guys,

I want to trigger UrlFetchApp.fetch(...) in order to ping my app every time there's an update in the Spreadsheet via onEdit(e) but unfortunately it doesn't allow me due to the restriction.

They cannot access services that require authorization. For example, a simple trigger cannot send an email because the Gmail service requires authorization, but a simple trigger can translate a phrase with the Language service, which is anonymous.

Do you have any other workarounds?

Thanks

Goran Kukurin

unread,
Aug 1, 2022, 9:38:57 AM8/1/22
to Google Apps Script Community
you cannot use the simple onEdit trigger for this, you must use the installable one https://developers.google.com/apps-script/guides/triggers/installable

N A

unread,
Aug 2, 2022, 4:46:56 AM8/2/22
to Google Apps Script Community
Yeah! You're right. That's what I did. I've used the TriggerBuilder:

ScriptApp.newTrigger(callback)
  .forSpreadsheet(SpreadsheetApp.getActive())
  .onEdit()
  .create();

Thanks

Goran Kukurin

unread,
Aug 3, 2022, 4:23:24 AM8/3/22
to Google Apps Script Community
Check the oauth scopes in appsscript.json. Also, you may need to check which user has created the trigger. 
Try to login with that user's account and run your onEdit method that calls UrlFetchApp.fetch. You may need to accept some permissions. After you accept them, the onEdit script will hopefully run.

N A

unread,
Aug 3, 2022, 9:39:42 PM8/3/22
to Google Apps Script Community
Yes, thank you! I've added the necessary authorization scopes in appsscript.json file.

{
  ...
  "addOns": {
    ...
    "common": {
      ...
      "homepageTrigger": {
        "runFunction": "onHomepage",
        "enabled": true
      }
    }
  },
  "oauthScopes": [
    ...
      ...
  ]
}

References

N A

unread,
Aug 3, 2022, 9:44:01 PM8/3/22
to Google Apps Script Community
However, is it possible to limit the onEdit listener (e.g., Sheet1) only trigger the listener when something has change on Sheet1. Otherwise, just ignore or won't trigger the onEdit.

Goran Kukurin

unread,
Aug 4, 2022, 2:04:43 PM8/4/22
to google-apps-sc...@googlegroups.com
you cannot limit when the onEdit is triggered, but you can examine the onEdit event argument (range property) and determine if you want to take some action, or ignore it.

On Thu, Aug 4, 2022 at 3:44 AM N A <oo7...@gmail.com> wrote:
However, is it possible to limit the onEdit listener (e.g., Sheet1) only trigger the listener when something has change on Sheet1. Otherwise, just ignore or won't trigger the onEdit.

--
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/CMwonPyUBT8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/655fdfb7-c498-4443-bd44-150c06ca22can%40googlegroups.com.

CBMServices Web

unread,
Aug 4, 2022, 2:20:18 PM8/4/22
to google-apps-sc...@googlegroups.com
Goran is correct. Just add a filter at the top of the onEdit function to just return if it is not the correct sheet or correct range that you want. Check the lines of code below:

function onEdit(e) {
  const sheetName = e.range.getSheetName();
  if (sheetName != "Sheet1") {return;}

You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/CAAgLCKzBaxLomV1uJ3SWE9%3D7zYD0JEbDQ_cHf-arhLfASEnTFA%40mail.gmail.com.

N A

unread,
Aug 9, 2022, 5:02:26 AM8/9/22
to Google Apps Script Community
Thank you guys for your help. 👍
Reply all
Reply to author
Forward
0 new messages