I've been playing around with google scriptapp for a while and even created a test deployment for the cards I built.
I'm wandering if it's possible to create installable triggers for users, using scriptApp.newTrigger() method. How would users install this Trigger when they install my Addon? How do I test it using the test deployment method?
I have tried creating this trigger programmatically. However, since I didn't know how to install it I tested it by running it in the appscript terminal.
I expect that the function with the trigger gets registered when the user installs my addon. How do I go about this?
I have found the perfect solution to my problem. Since Installable triggers need to be installed once, I found universalActions a very suitable use case.
For instance, this code below will attach a universalAction that'll install the trigger once the user clicks on it.
"universalActions": [
{
"label": "Invoice Reminder",
"runFunction": "createTrigger"
}
]
Image with universal action
Related
I have a google sheet that has a Google Apps Script connected to it. This script needs installable triggers to function properly. These triggers are installed with the function installTriggers(). This function currently runs when a user manually clicks on a SpreadsheetApp.getUi().createMenu menu.
function installTriggers() {
if (ScriptApp.getProjectTriggers().length != 3) {
ScriptApp.newTrigger("sheetEdit").forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet()).onEdit().create();
ScriptApp.newTrigger("updateNotifications").timeBased().everyDays(1).create();
ScriptApp.newTrigger("updateLessons").timeBased().everyDays(1).create();
}
else {
SpreadsheetApp.getUi().alert("Triggers have already been installed.");
}
}
When these triggers are not installed, certain script functionalities do not work properly. Hence I would like to check if these triggers have already been installed every time a user (might be someone else than the google sheets owner, but also the owner himself) opens the sheet. If there are 0 triggers installed, I would like to give a notification to the user that opened the google sheet through use of an alert popup. It would be nice if it's possible to automatically install the 3 triggers using installTriggers() if these are not yet installed, right after clicking away the alert popup.
If all of the 3 necessary triggers have been installed, nothing should happen.
I was planning on using ScriptApp.getProjectTriggers() in an onOpen function to read the amount of triggers already installed.
However, I can't use a simple onOpen trigger, as this trigger would not have the authorization / permission scope to run ScriptApp.getProjectTriggers(). I tried this and it gave me the following error in the error log of the GAS editor:
"You have no rights to call ScriptApp.getProjectTriggers. Required rights: https://www.googleapis.com/auth/script.scriptapp at onOpen(Code:630:32)
This would mean I would have to use an installable trigger to run ScriptApp.getProjectTriggers(). However, this is not at all a logical thing to do, because for this installable trigger to run, I would first have to install it. While I am trying to detect sheets that have no triggers installed yet.
My Question:
Does anyone know how I can notify users that no (0) triggers have been installed (and that they're missing out on certain trigger functionalities without knowing it), without using an installable trigger?
Missing a trigger
function checkForTheseTriggers() {
const rA = ["sheetEdit","updateNotifications","updateLessons"];
const tA = ScriptApp.getProjectTriggers().map(t => t.getHandlerFunction());
if(!rA.every(e => tA.includes(e))) {
SpreadsheetApp.getUi().alert("You are missing atleast one essential trigger")
}
}
As far as I've read google app script is supposed to show you a prompt to review permissions automatically if you have not authorized your app yet. However this is not happening and I also cannot find a way to manually trigger such a prompt that can be used within onOpen(e).
What I would like to achieve is my app prompting users to review permissions when they have not authorized the app yet.
What I have tried:
Letting GAS automatically detect used scopes.
Manually setting scopes using the appscript.json file.
Using Ui.showModalDialog() and ScriptApp.getAuthorizationInfo().getAuthorizationUrl() to manually guide the user to permissions. However, this results in the error:
Google Apps Script: Exception: You do not have permission to call Ui.showModalDialog.
Required permissions: https://www.googleapis.com/auth/script.container.ui
Issue:
You are using a simple trigger (onOpen). Simple triggers cannot access services that require authorization.
Solution:
Install your trigger, either manually, following these steps, or programmatically, executing this:
function installTrigger() {
ScriptApp.newTrigger("yourOnOpenFunction") // Don't call it onOpen
.forSpreadsheet(SpreadsheetApp.getActive())
.onOpen()
.create();
}
The authorization is given when installing the trigger.
Note:
The installed trigger will run under the account who installed it. Therefore, this is not a good approach if the actions made by the trigger depend on who is executing it, and I'd suggest displaying the dialog through other means, like clicking a button, or similar.
Reference:
Simple triggers: Restrictions
I have recently published a Google Forms add-on that access a Classroom Course roster right after the Form is submitted.
I manually installed the onSubmit trigger in my script and it worked fine. I copied the code to another Form and worked too, with GSuite and non-GSuite accounts.
The problem is that now that the add-on was published, I tested it and it doesn't work. Searching the GCP, in the Logs viewer I found a "The caller does not have permission at onSubmit(Code:40)" error from the API when calling the classroom method.
So, the trigger is working, but for some reason the permissions fail. I checked the scopes and they correctly request the appropriate permissions for the classroom method the onSubmit function calls. I'm wondering if the problem is that I manually installed the trigger and I should have it programmatically installed within the script.
Thoughts?
It turns out the answer depends on whether the trigger was manually installed or programmatically installed. If it's the former, then the "caller" is whoever installed the trigger (typically the developer) and if it's the later, then the "caller" is the user who installs the add-on (which was my intention).
Thanks Alan!
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.
I installed a form submit trigger for my project using ScriptApp around the beginning of May. The trigger has worked just fine however I now wish to uninstall the trigger and am running into problems. When I try to view all my current project triggers (Resources > Current project triggers), it returns that there are no associated triggers. Also when I try to run (Resources > All your triggers) it returns that there are no triggers shown.
I'd like to believe that this is the case, that the trigger in question has been somehow deleted but I don't believe that it is. To make sure the trigger didn't run, I de-authorized the script to run from my google account settings.
My suspicions that the trigger was not deleted were confirmed when I received a "Summary of failures for Google Apps Script: Form Processing Script" which told me my script had failed 3 times because, "Authorization is required to perform that action."
These failure e-mails also contain a statement, "To configure the triggers for this script, or change your setting for receiving future failure notifications, click here." When I click on the link, there are no triggers shown...
Here's a look at the code i used to install the trigger:
function formSubmitTriggerInstall() {
var formID = '1rOikLDUAqMWCB0ktjWzFE0oB6LmOHvXuzPAqyq0XLwE';
ScriptApp.newTrigger('processForm')
.forForm(formID)
.onFormSubmit()
.create();
}
I have also tried to locate the trigger using ScriptApp but to no avail.
Does anyone have any suggestions on how I can eliminate this trigger or get the associated trigger ID?
One other note, since I installed the trigger, the google developer's website and documentation went through a major face lift and some of the page content appears to be updated as well. Not sure that would affect my triggers though.
It was confirmed that this was a big and it was fixed by the apps script team.