GAS: deploy time based trigger app - google-apps-script

I wrote a custom GAS that reads some emails and manipulates them. It uses the time based trigger to run. I want to know if there is a way to "deploy" a specified version of this where the time based trigger still runs, but I can still work on new code changes in the apps script editor (running it manually) and then deploy that new version (that runs by time triggered) when it is ready?

You might want to look into add-ons:
https://developers.google.com/apps-script/add-ons/

Related

Faulty Installed Clock Triggers on an Add-On

I designed a rather simple Add-On to enable my local team to more easily automate newsletters and target updates to Slack.
The Add-On works well, aside from two issues:
Schedule a send functionality.
I'm using the ScriptApp's newTrigger() function to allow users to schedule automatic sends.
While it seemed to have worked in original tests, the triggers are now triggering at the wrong time (a trigger that should run daily from 11-12AM has been running at 5PM - Another that should run at 6PM has been running at 12AM - I've checked and the timezones appear to be correct GMT+2 for Paris)
GetActive() returning the wrong sheet
This is a minor problem as I've only noticed it twice, but when running a script with getActive() from the correct sheet, it accidentally ran the script using another spreadsheet.
I remain at your disposal should you need any more information, IDs or the likes.
Thank you for any help and have a fantastic day

How Do I create an Installable Trigger on a specific Deployment from Standalone Script With Event Source Spreadsheet?

Background:
I have been learning Google Apps script, and now have a working standalone Google Apps Script project, just for personal use, in which I have created an Installable Trigger that is configured with Event Source: "From Spreadsheet", creating it programmatically (I was unable to find a way to create this trigger through the web interface (Edit menu --> Current project's triggers) since the event source drop-down only shows "timed" and "From Calendar" as selections).
A few weeks back I reached the point where I split the spreadsheet & script project into "production" and "development versions, and I have so far just been copying/pasting the development code into the production project when I want to release new versions.
It seems, a much better way to do this would be to take advantage of the Deployments logic, so that I can support both environments from a single code base, with development using the HEAD deployment, and then create a separate Deployment for production.
The problem:
The problem I'm running into here is, after creating a new deployment, I see no way to associate my newly created "production" deployment to be able to take events from any spreadsheet. If I try to create the trigger manually through Edit --> Current project's triggers --> create trigger, the options available permit me to select which deployment I want to use for it, but don't allow me to select "From Spreadsheet" as an event source; and if I create the trigger programmatically, I can associate the event source correctly, but it creates the trigger associated with the HEAD deployment, and without any way I can see to specify a different deployment.
ScriptApp.newTrigger('ss_onEdit')
.forSpreadsheet(idSs)
.onEdit()
.create();
I had thought perhaps I could change which deployment the trigger goes against, post-creation, but the drop-down to select a different deployment is greyed-out.
Is it possible to do what I'm trying to do? Am I misunderstanding something about how this is supposed to work? At this point I don't really see the point of a versioned deployment if there is no way to associate it with a trigger.
The script needs to be bound to the spreadsheet(accessible from Tools> Script editor of the spreadsheet).
You need to publish and deploy a dummy web-app/api. In the editor,
Publish> Deploy as Web-app> Select a version and deploy
You can now select any of the deployed versions in the web interface(Edit> Current project triggers) to add trigger and select a version.

How to enable Execution Transcript when this script runs?

I am using a script which is found here: https://moz.com/ugc/updated-tool-seomoz-api-data-for-google-docs
I would like to trigger this to run once an hour (as a test, later once a month); to facilitate this I set up a time driven trigger in the script editor UI.
I need to test that the trigger is actually working. The issue is that I do not expect any of the data to change when the script runs (might change once a month max)...
So I looked to the script editor and found View > Execution Transcript. Perfect!
Except that I get this message:
No execution transcripts found. Please run your script and try again.
This doesn't seem to be consistent with the expected behaviour after reading the Google Apps Script documentation page, which says:
Every time you run a script, Google Apps Script records an execution
transcript, which is a record of each call to a Google Apps Script
service that is made while the script runs.
(Nothing in the logs either by the ways, though maybe this is something that needs to be turned on by the script itself; not sure).
So how can I test to see that my time driven trigger works, and that the script is being run each hour?
Final note: The script is executed by calling a function in the spreadsheet, as explained in the link at the top of the question.
If you have not yet run it manually, there may be an issue with it being given the proper permissions. Your first step would be to run the code manually using the play button. Once you know that the code itself will run successfully and has the right permissions, you can move to triggering it automatically.
Your next step is to shorten your trigger time so that it executes more frequently giving you more instances to check the function. Depending on the structure of your code, you can add Logger.log() calls at different steps to make sure that all portions are being called that need to be. You can then check you logs instead of the execution transcript as well.
These Logger.log() calls could include the time at which is was called to be able to verify that it was at the time/ frequency you need.
Ok, I don't know how great this answer is... but:
For me (as per my comment to MasterCrander), running the script by means of using the function inside the spreadsheet didn't actually affect the execution transcript, but when it ran via the time driven trigger it made it into the transcript.
Problem solved.

Google Sheets Add-on timed-trigger for a large data set

According to Google app script time-trigger documentation https://developers.google.com/apps-script/guides/triggers/installable#limitations,
Time-driven triggers cannot run more frequently than once per hour. How can I use app script to write an add-on that process large amount of data?
If I run a trigger, it will only allow me to trigger once. I can't chain the triggers so it can continuously run.
You cannot do so using triggers because they would run at most once per hour in the context of an add on.
What you can do is to initiate such processing client-side while possibly showing progress on a sidebar. The client js in the sidebar can chain server calls.Of course being all client-side means the user must have opened the file and if they close the browser tab or navigate away, the chain calls will stop. That can be handled by alerting the user and making them aware that a "sync" happens and sometimes takes a while. Currently there is no other way to handle this unless your client call and time trigger call another server which does the processing, like appengine or gce.

Make spreadsheet only editable through script

I have a script which adds new rows and changes the sheet in some ways.
I'd like to lock this sheet so it cannot be edited manually, but allow the script to be run.
Is there a way to do this?
Yes, it's not the most easier thing to do, but it's possible.
But before we start it's important to understand how Google spreadsheet/sheet protection works.
There's no way to protect a sheet or range from the file owner. So, if you also want to protect a file from yourself (assuming you're the owner). Then the only solution available now (I do hope they change this in the future) is to change the ownership of the file to another account. What most people do is use another Google account that they don't use actively (you can create a bogus easily if you don't already have one).
Then, after you have transferred the file to another account, you can easily lock changes to ranges, sheets or the whole spreadsheet using the regular GUI.
Ok, now to the script. When a script runs, it's always using the authorization of a specific account, and it can do everything that this account can. For example, when someone runs a script directly from the script editor, or clicking on a custom menu or image: it runs under the account of who's clicking. But if you set a installable trigger, then the script always runs under the account of who setup the trigger in the first place (not who's performing the action that actually triggers it). Simple event triggers run as who's triggering the action (you can think anonymously), but they can't do much, it's a security feature (read the link for a better understanding).
Lastly, when one is publishing a script as a web-app, then there's a selection box to choose if the script runs as the developer or the user. Very simple.
Back to the problem. Since we have locked out everybody from the desired sheet/range. For a script to make changes to this locked area, it must run as the file owner!
If the changes you do are automatic, e.g. via a installable trigger, then you're good. Just setup the trigger using the file owner account and that's it. Also, if your users are accessing the script "externally", that is, via a web-app. Then that's easy to, just setup the web-app to run as the developer (the file owner).
The most complicated scenario is if you need to run the script from a custom menu, which will then run under the privileges of who's clicking on it, which themselves can not make changes to the protected area. The solution to this is to deploy the script as web-app running as the developer, and have the function running from the button click to call the deployed url using UrlFetch, possibly passing some parameters to designate what needs to be done. Then, since the webapp runs as the developer (which is the file owner), it can make any changes required, and return (if there's something to return) any value (usually a JSON) to the calling function (which is running as the user and is associated with his session), so you can show him a message on a popup or toaster, etc.
It's kind of tricky but works great, and you can do all that on the same script contained in the spreadsheet. But if you're concerned that your users may access the script editor and change the code (which is possible), than you should separate the web-app part on a different file, that you don't need to share with them.
Yes but only for others not for the script owner. Just use 2 accounts one as the owner and the other one as the reader.