I'm having an issue with doing an HTTP Post in the onOpen event in google apps script.
What I'm trying to do is send a notification to another deployed script that someone has opened one of our spreadsheets. I'd like this functionality to persist even when someone copies the spreadsheet so that we can track where those are as well.
My problem is that if I call UrlFetchApp.fetch(url, options) from the onOpen event, I get the error - "Execution failed: You do not have permission to call fetch" however the fetch does work if I create a trigger using the ui that fires when the spreadsheet is opened. The problem with this approach is those triggers are not copied when the spreadsheet is copied.
Any suggestions for a possible solution or a simple workaround would be greatly appreciated.
Thanks,
Mark.
There are two types of triggers - simple and installable. The onOpen is a simple trigger and there are limitations on what you can do within a simple trigger and doing an UrlFetch is one of them. You can read more about simple & installable triggers at https://developers.google.com/apps-script/understanding_triggers
Related
I have a problem with a google sheet script, I would like to transfer data from one workbook to another using an onEdit trigger, but when my trigger fires, I have a message of error in the execution log which says that I do not have the authorizations to access this file, however when I do the same action but without trigger (by activating the function by hand) it works without problem. I don't know at all where the problem comes from, if someone can help me it would be really appreciated because I'm desperate (I found absolutely nothing on google after hours of research), thank you in advance for your help and have a nice day.
Joseph Menard
(PS; I am attaching a link to a video of my screen showing my problem ;
https://www.loom.com/share/f1a0fde043f74143b7caad78ae806be6 )
I assume you are using a simple onEdit trigger? Simple triggers run without authorisation from the user and are subject to several restrictions. Instead ty using an installable onEdit trigger and see if that helps?
I have a template google sheet that our company duplicates for every project.
The final result I want is to have an onEdit trigger that does a POST request. I actually already have this working. The issue stems from this: This trigger cannot be a simple trigger, it must be an installable trigger because according to Google's restrictions, simple triggers cannot make external requests, but installable triggers can. Again, all of that works, I just have to set up the installable trigger manually.
As I mentioned, this is a template document, so creating a new installable trigger manually every time it is duplicated is a real pain. I want to improve my UX by having it done automatically. Now the API documentation DOES detail how you can do this programmatically right here. So it definitely should be possible if I'm reading this correctly.
In order to have this trigger automatically created, I, of course, must use another trigger. In this case, it MUST be a simple trigger or we're back with the same issue.
So I added this onOpen() simple trigger function to my code: (convertAlias is the name of the function to run when the onEdit trigger activates)
function onOpen() {
if (ScriptApp.getProjectTriggers().length == 0) {
ScriptApp.newTrigger('convertAlias')
.forSpreadsheet(SpreadsheetApp.getActive().getSheetId())
.onEdit()
.create()
}
}
When I open the document though, I get no results. The stack logging shows the error as Exception: Action not allowed. The error specifically originates from the create() call.
I thought that maybe there was some issue with creating an installable trigger from a simple trigger so I went for the next best thing, a button. I added a button to my spreadsheet that was linked to the same code as a "generateTrigger()" function. Pushing that actually DID bring up the Authorization screen, which is what is expected. But then after authorizing my account, it then goes on to report the same Exception: Action not allowed. After that initial authorization, it does not ask me again and automatically goes to that error. I get the same results from running the function manually in the editor.
Is there any way I can make this installable trigger happen with any sort of automation? Is this an issue with my code, a permissions issue, or something else?
Thanks
It appears I should have just been passing the sheet and not the ID? Doesn't seem right but getting rid of getSheetId() made it work!
I want to run a Google App Script function every time a change is made to my Google Doc. I have found onChange and onEdit triggers which are only available for Google Spreadsheet. I am sure there must be an edit/change trigger for Google Doc as well.
Unfortunately there are no onEdit(), onChange() triggers for Google Docs.
The triggers available for Google Docs are the following:
onOpen() triggers both simple and installable;
time driven triggers;
onInstall() simple triggers.
What you can do instead is to use a time driven trigger so in this way even though the changes are not directly triggering the execution, the trigger will still run how often you want it to.
Last but not least, you can file a Feature Request on Issue Tracker where you specify the details needed.
Reference
Apps Script Trigger;
Google Issue Tracker.
I'm looking to call UrlFetchApp.fetch() based on user input on a sheet but nothing happens. No errors, just silently ignores the call. Same goes for MailApp.sendEmail() and GmailApp.sendEmail()
This is on a Google Apps domain, and only domain users are using the Google Sheet.
Simple triggers like onOpen or onEdit are not able to do anything that requires authorization like sending mails because they run silently and anonymously.
This is all explained in the documentation .
You should simply rename your onEdit function to something else - SpecialOnEdit for example ? - and create an installable trigger (documentation) from the script editor menu (ressources/triggers/create a new trigger...)
You can see an execution flow in view -> execution transcript in the script editor. Just change a cell value in spreadsheet then come back the script editor and check "execution transcript". It will show you an error if it happens.
I have a Google sheet that has an onEdit triggered function that works fine if I edit the doc myself.
But when the sheet is updated via the sheet API from an external app the event does't occur. Can you help me find a method to generate this update event following a write to the sheet from an external source,
thx
Some limitations of onEdit() are described in a previous answer.
If you install that function to trigger "on change", you should catch the event. Note that the event that's delivered in this case is not as rich as an edit trigger receives. See Understanding Events.