Google apps script authorization - have to call scripts twice? - google-apps-script

I was wondering if there was a way to run a script requiring authorization without having to call it twice. For example, if I have a link on my google site that calls a script, I have to click on it two times - once to begin the authorization process, and once to actually run the script afterwards. I feel like this would be very confusing to users.
Thanks!
Jeff

There is no way to avoid this but I've found that it was generally less confusing to create a dummy function (that I usually call 'Authorize') that has to be called once and that does nothing else than trigger the authorization process and show a small explanation text. This makes things probably more clear for a 'normal' user as it is a separate process and not a repetition that gives the feeling something is wrong...

Related

Google Apps Script problems running as different user

I am having problems with script execution from within my company's Google Workspace account.
I am the developer and I have no problem executing any of the scripts with my account; all my scripts run all the way to the end.
Now, there is this one script through which I present the user with a form in a sidebar for him/her to input some data, and then I do some stuff with that data. As mentioned, no problems for me. However, when I try to run it as another person from the company (which isn't just another editor but the superadmin account for the company's Google Workspace), after the initial script permission-granting dialogues are done and dusted, the script only runs halfway through and hangs. It actually gets the sidebar up, allows the user to input data and also to submit some data (I need to check if it submits all of the data). I know for a fact that some of the data does get submitted since one of the things the script does is fill up a particular cell with the data of one of the inputted fields, but it hangs there forever... How could that happen? I thought it might be a problem with the HTMLService, but as some of the inputted data does go through and gets added to the right cell..., I'm at a loss.
Has anyone encountered anything alike?
As it turned out, after I banged my head around for hours, it was a matter of dealing with protected cells.
Even though the script is set to execute as me, as soon as the script attempted to write into a protected cell whenever a different user executed it, and where I had set myself as the only user with write access, it failed.
Thanks go to #Kessy for having a look into it and suggesting a possible way to see the problem.

How to use a second (minute or hour is to long for immediate response) based trigger?

I am writing an immediate response code in google script which must monitor inbox at current time and if it detects a new e-mail with given subject then it runs the code and replies to the e-mail and at the end it marks the e-mail as read.
The whole stuff works fine, however I want it to get started more frequently than once in a minute. Thus I guess I can't use trigger option from the scripts interface. Will it work if I won't specify a trigger? How can I organize all this? I am using this stuff with google free quota for my personal use only.
thanks beforehand
Its not advisable to do what you want because you will run out of quota.
But its possible. Not with triggers as they run at most once a minute.
If you must, write a htmlservice app with a setInterval. Call a server method to do your work. Leave the webapp open all day.

Update Google Docs trigger - once and for all docs

I have a lot of similar spreadsheets in GoogleDocs. There's an onEdit trigger in each of them that does some complex validation. Its code is the same for each document, and sometimes I need to update it for all the spreadsheets at one time.
This is what I already tried:
Install a trigger from one "mother" spreadsheet, i.e.
ScriptApp.newTrigger('f_proc').forSpreadsheet(iCurSh).onEdit().create();
This gives us ability to updatability, but also lots of collisions when executed by several users simultaneously.
Calling the validation method from library. This gives no collisions, but in case of update I have to change library version manually in each spreadsheet.
Are there any other ways?
Thanks in advance!
As one comment says above, use libraries in development mode so they always use the latest code without worrying about versions. You can also keep the code somewhere else like a google doc, get it with documentApp and inject it programmatically with 'eval' but might be slower than eval even if you cache it with cache service (I used to do this before gas had libraries)

Google Apps Script Authorization gets lost when changing Script - Upgrade path?

I have been working quite a lot with Google Apps Script lately, but there is one thing that still is very unclear to me and the docs do not hint about it at all:
When publishing a script as a WebApp (access: Anyone, as: User accessing the WebApp) and asking for permissions (e.g. GMailApp access, UserProperties and Trigger) and then afterwards changing that script (but not asking for any additional permissions, just changing code) and publishing it again, it seems as if triggers being run by Scripts priorly authorized by users lose their authorization (e.g. the user gets an email with a failure message: Authorization is required to perform that action. from that script).
I read about libraries being independent based on their version, but accessing GMailApp from within a library or a Trigger within a library is not possible as it needs the active user? Is there any way around this? What is the suggested upgrade path, e.g. how can I make (code) changes to the script without making it fail for existing users?
Some services - gmailApp and mailApp for sure- are considered as sensitive matters by Google and therefor any modification in the code, even a very minor change, implies a renewal of the authorization. I can't remember exactly right now where I read that info but I'm pretty sure I read it (!) and I saw it also as a Googler answer somewhere in this forum. Forgive me for not being accurate concerning references.
Anyway ... that explains why you have these authorization issues with your script and AFAIK there is no way to avoid this process.
That said, your users should get an authorization screen, not an error message for services that they use in your app.
If you use triggers in your app (that you set yourself of course) then you should run these functions yourself manually to pass the authorization since the triggers are executed under the authority of the one that creates them, no matter how your webapp is published.
I hope I'm clear enough, if not refer to the doc about installable triggers and this doc also.

Google Apps Script Triggers not firing - but only sometimes

I have a few scripts that I want to run overnight so I've set up script triggers to execute these. Its all pretty straightforward stuff and they run fine. But occasionally, say once a month, one of them fails to execute. No error messages are sent to me by Google or anything like that.
Has anyone else experienced this? Has anyone got any ideas I can check for this error? Obviously if its the actual trigger that's not firing in the first place then the code won't actually run and therefore it won't be able to run any checks.
I'll also flag this with Google themselves in google-apps-script-issues forum.
I have a trigger set to fire every five minutes and a few times a month it will fail. Often it will say something about the function not being found.
You can be notified of an execution failure by setting up an "execution failure notice". In Resources > Current Script's Triggers, after each trigger will be a "notifications" link. Clicking it will open the "Execution failure notifications" window.
You have some choices about timing of the email notification of the failure.
Maybe: I suspect that, after I edit my code, sometimes the triggers miss fire. But I have never double checked this.
Suggestions:
1) add some logging and at the end of the script, grab the log and email it back to yourself.
2) Add a try catch around your code and email any errors that you catch back to yourself.
Google scripts are throttled in various ways and your script may be hitting up against one of these limits. There are limits on the number of emails you can send and limits on execution time of the script in addition to others.