How to use versioned deployments when programmatically creating triggers? - google-apps-script

When creating a trigger programmatically, i.e. through ScriptApp.newTrigger(), does it always point to the "head" deployment, or the most recent published (versioned) deployment? (See documentation on deployment types here.)
For context, I'm distributing a script in a multi-user environment which relies on triggers to run every few minutes. I'm trying to ensure that changes made in development (i.e. before publishing those changes) are not picked up immediately by all the clients of the script, as this effectively means any code updates in dev are instantly live for all users.
If triggers can only point to head deployments, what are best practices for pointing them at code which can be safely updated in dev (head version)?

You want to know that when a trigger is installed by ScriptApp.newTrigger(), which version of script is used.
You want to know the method for installing a trigger by selecting version of script.
I understand your question like above. If my understanding is correct, how about this answer? Please think of this as one of several answers.
Answer for Q1:
When a trigger is installed by ScriptApp.newTrigger(), although the value of "Deployment" is not shown in "Current project's triggers", the latest script is run by the installed trigger. This situation is independent of the version of script. The current latest script is used.
If the trigger is manually installed, you can select the deployment version at "Choose which deployment should run".
Unfortunately, in the current stage, this cannot be achieved by the script yet. I think that this might be achieved by the future update.
Answer for Q2:
In order to install a trigger by selecting version of script, as a workaround, I would like to propose to use a library. The flow of this workaround is as follows. This is a sample flow.
Create 2 standalone projects.
One is used as a library.
Another is used as the project to use the library.
In the project to use the library, for example, function myFunction() {lib.myFunction()} is put. This script runs the function of the library.
Install the library to this project.
You can select the script, that you want to make users run, by selecting the version of library.
Trigger is installed to this project.
The project of the library is the project for developing. It manages the version of developing script by "Manage version" of the script editor.
When the trigger is installed, it runs ScriptApp.newTrigger("myFunction") in the project to use the library. At this time, when the trigger is run, the selected version of library is used. By this, the latest script of library is not used. Users can use the stable script of the version you selected.
Note:
You can also see the version of library at Manifests file (appsscript.json). This file can be modified by the script. By this, you can also modify the version of script using by the trigger by the script.
References:
Libraries
Manifests
If I misunderstand your question, I'm sorry.

Yes, it always point to the "head" as versioned deployments are only available for Gmail Add-ons.
The above is based on that the only the guide to publish a Gmail add-on includes as step the creation of a versioned deployment -> https://developers.google.com/gmail/add-ons/how-tos/publish#step_1_create_a_versioned_deployment

Related

How to deploy a library version by replacing the old one in Google Apps Script?

I have a Google Apps script file containing some functions. A certain version of this script is assigned to many projects as a library. When I do some changes in the script file I want to deploy this new version to all the projects easily without having to change the assigned script version in every project. A similar question ("Google Apps Script version deployment" change - More than one active deployment - how to update an active deployment?) has an accepted answer that doesn't seem to work: When I follow this instructions it is still the former version of the script active and the one I've edited is then listed as archived.
What do I have to do in order to deploy changes?

Which deployment of Apps Script do Sheets custom functions use?

I have a Google sheet with some code in a bound Apps Script. The code contains some functions that are used in a nightly trigger, and some custom functions used in sheet formulas.
I'm trying to have a sane development process for this, and to decouple saving code from releasing it - so I have a "production" deployment, and the trigger runs code from that deployment - when the code is ready, I update the production deployment with the new version (I use clasp to manage this process). This works fine.
However, I don't know which deployment of my Apps Script code does Google Sheets use when running custom functions? Does it always use code from the #HEAD deployment? Is there a way to control this?
On bounded scripts custom functions use the head deployment, on add-ons use the version specified in the Google Workspace Marketplace Configuration page..

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.

App script web change version for each deploy

I created a web app for my google sheet script.
I deployed it with version 1 and and it works
Now I want to make on going changes and deploy my changes,
Do I need to increase the version for each change I deploy? or can I update on current version?
Also, is there a way to get the current version programmaticaly?
For your situation, I would like to introduce because I had a workaround that I also use.
For question 1 :
In generally, when the script of Web Apps was updated, the project version is required to be increased and Web Apps is redeployed with the new version.
But if you want to deploy Web Apps without increasing the version, as a workaround, you can achieve it by using a library. The flow of for this workaround is as follows.
Create 2 projects.
One is the project that Web Apps is deployed.
It supposes that this is the project that you are using now.
Another project is used as a library. Please create this as a standalone project.
Please create a version.
On script editor
File -> Manage versions...
After input description, please click "Save new version".
Please copy this script ID. This is used for installing the library.
For the project that Web Apps is deployed.
Please install the library project as a library.
On script editor
Resources -> Libraries...
For "Add a library", please input the file ID of library project.
Turn on "Development mode".
Set the version to the saved version.
In this sample, please set "Identifier" to "lib".
Please copy and paste the following function. In this project, the script is only this. And please save the project after copy and paste it.
function doGet(e) {return lib.main(e)}
Please deploy Web Apps as a new version.
After this setting, you are not required to modify this project.
For the project which is used as a library.
Please copy and paste the following function.
function main(e) {return yourfunction(e)}
yourfunction(e) is the function in the project that you are using.
In this workaround, when you want to modify the script, please modify the library project. By this, when the library is updated, the project which deployed Web Apps is automatically updated because "Development mode" is turned on when the library is installed. This update is also reflected to Web Apps. So the script of Web Apps can be updated without increasing the version.
As a sample, the scripts for both project after making the settings described above are as follows:
For the project that Web Apps is deployed.
function doGet(e) {
return lib.main(e);
}
For the project which is used as a library.
function main(e) {
return yourfunction(e);
}
function yourfunction(e) {
do something: The script which is run when users accessed to Web Apps.
return something
}
For question 2 :
In order to retrieve the current version of the project, you can use Apps Script API. This is also answered by tehhowch. In a recent update, users got to be able to retrieve and create the project versions. By using this, you can achieve this. The required scope is https://www.googleapis.com/auth/script.projects or https://www.googleapis.com/auth/script.projects.readonly.
The endpoint for retrieving the version list is as follows.
GET https://script.googleapis.com/v1/projects/### scriptId ###/versions
If you want to create the new version using script, you can use the following endpont.
POST https://script.googleapis.com/v1/projects/### scriptId ###/versions
Request body is
{
"versionNumber": 3,
"description": "sample description"
}
References :
Web Apps
Libraries
Google Apps Script API
Method: projects.versions.list
If this was not useful for you, I'm sorry.
When creating a web app for apps script, two links are given:
The one ending exec will only load the specified published version of the code.
The one ending dev will load the latest saved code (allowing for testing of code prior to making a new version).
Programmatic control of the deployed version is available via the Apps Script API, and to a Dev environment via clasp.
Getting versions
Creating versions
You need to increase the version for each change, unless you use the "dev" version of the app when it will always display the latest version. This will only be visible when you are logged into your account though.
It's much easier than everyone makes it sound
Go to Deploy:
Click "Manage deployments"
Under Configuration click the edit pencil
Under "Version" click "New Version"
Click "Deploy" it will auto increment the version for you

Can a library be used to force all copies of a spreadsheet to reference the most current version of a script?

I've created a Google Sheets spreadsheet template with a script that my users can access via a menu option. This "template" is new and undergoes frequent enhancements and bug fixes.
I'd like to find a way to ensure that all copies that are made of the template access the most recent version of the script.
I read this: https://developers.google.com/apps-script/guides/libraries#testAndDebug
...but the first warning at the top of the page says not to use libraries for long-running scripts. Mine takes 3 - 30 minutes to run, depending upon the amount of input data.
If I do use a library, do I have to put it in development mode in order for copies of the template to reference the latest version of the script?
Sorry. I hope this is making sense. I'd appreciate any advice you might have.