Google Script onFormSubmit() - google-apps-script

I would like to autorun a script in the responses file for a Google Form whenever there is a submission to a Google Form. It works if I run it manually but it cannot be run upon new submission. Anyone advice?
function onOpen(e) {
var ss = SpreadsheetApp.getActive();
var form = FormApp.openById('1xuCZFmUEhbkZtUUU0bRS7HOHCerRfd2ebqc3OWPWr5Q');
ScriptApp.newTrigger('onFormSubmit').forSpreadsheet(ss).onFormSubmit()
.forForm(form)
.create();
}

You don't need to create new trigger every time you open the spreadsheet. Maybe try to manually configure the installable trigger instead.
You will find step-by-step instructions Here
But be aware of the limitations

Related

Having "You do not have permission to call ScriptApp.newTrigger" Error in Google Sheets Script Editor and "callById()"

I am having problems executing scripts in Google Sheets.
I originally had installed triggers for my functions but I wanted to make copies but the triggers are gone. I set up the installable triggers in G Suite Developer Hub and now I am trying to create triggers in Script Editor of Google Sheets but I am having permission issues.
I know that I can edit auth scope in manifest file but it also didn't work.
function onOpen(e){
var ssId = SpreadsheetApp.getActiveSpreadsheet().getId();
ScriptApp.newTrigger('makeDropDownMenu')
.forSpreadsheet(ssId)
.onOpen()
.create();
}
Above is the code I want to execute and it says "You do not have permission to call ScriptApp.newTrigger. Required permissions: https://www.googleapis.com/auth/script.scriptapp
at createTimeDrivenTriggers"
What should I do to install triggers in script?
That type of errors are related to the oauth scopes you have. For almost all scopes, Apps script sets automatically the oauth scopes you need if you don't manually set the "oauthScopes" field in the manifest file [1]. You can either try deleting the "oauthScopes" field from the manifest and Apps Script will probably add the scopes you need, or manually add them in the manifest like this:
"oauthScopes": [
"https://www.googleapis.com/auth/spreadsheets", //This one you should already have it
"https://www.googleapis.com/auth/script.scriptapp" //Thi is the one that was missing
]
[1] https://developers.google.com/apps-script/manifest/
You can install trigger with this. When you open the spreads go to "My Menu" and click on "Create Trigger".
function onOpen(){
SpreadsheetApp.getUi().createMenu("My Menu")
.addItem("Create Trigger","createTrigger")
.addToUi();
}
function createTrigger() {
var ssId = SpreadsheetApp.getActiveSpreadsheet().getId();
ScriptApp.newTrigger('makeDropDownMenu')
.forSpreadsheet(ssId)
.onOpen()
.create();
}
But if you just creating a menu and nothing more then you can just do that in an onOpen() simple trigger like I did above. If I'm missing your point please provide a little more direction.
The fix is to run the onOpen function manually once in the script editor.
Since that function would only get executed when an user opens the file, it does not get executed by your main script right away, so the permission for THAT doesn't exist yet by the time the user opens the file.
So by manually running that function on the script editor (even if it errors out), it will force a permissions prompt requesting what is needed and attach it to the script's project.
My contribution is:
function onOpen() {
var spreadsheet = SpreadsheetApp.getActive();
var sCelControlTrigger = "P3";
createTimeDrivenTriggers(sCelControlTrigger);
}
function createTimeDrivenTriggers(sCelCtlTrigger) {
var spreadsheet = SpreadsheetApp.getActive();
var nAcionadores = 0;
// Checks if triggers is created. If not, creates the triggers.
nAcionadores = ScriptApp.getProjectTriggers().length;
if (nAcionadores === 0) {
// Trigger every day 7-8 hours.
ScriptApp.newTrigger('Envia_Situacao_Saldo_Bancario')
.timeBased()
.atHour(7)
.everyDays(1)
.create();
// Trigger every day 6-7 hours.
ScriptApp.newTrigger('Gera_Programacao_Pagamentos_Automatica')
.timeBased()
.atHour(6)
.everyDays(1)
.create();
// Trigger every day 5-6 hours.
ScriptApp.newTrigger('Gera_Programacao_Pagamentos_Variaveis_Automatica')
.timeBased()
.atHour(5)
.everyDays(1)
.create();
//
spreadsheet.getRange(sCelCtlTrigger).setValue("Rotinas Automáticas Criadas");
}
}
How you can see, triggers was created if not exists any one. Triggers have specific roles to execution based in time.

Google Sheet script to sort timestamp on form submit not working

I have a google sheet that needs to auto sort by the timestamp whenever a user submits a form response. I found a script that will do this but it only works when I run the script manually. The script is:
function getSpreadSheet(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("New");
sheet.sort(1, false);
}
There are two types of Google Apps Script triggers. Simple triggers are those that are run based on the function name and are limited to onOpen(), onEdit(), onInstall(), and doGet() and doPost(). For an On Form Submit trigger, you need to make your function an Installable Trigger by choosing Edit->Current project's triggers. From there you want to get to click the link if there are no triggers and then select your function int he first drop down item, set the event to From spreadsheet and the next option to On form submit.

clearContent does not run onChange in Google Sheets script

I have written a script to clear a range of cells (B4:B8) when a specified cell (C1) is changed. However, the cells are not cleared when I edit the specified cell. Here is the script:
function onChange() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Quality Control').getRange('C1');
ScriptApp.newTrigger("clearRange")
.forSpreadsheet(sheet)
.onChange()
.create();
}
function clearRange() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Quality Control');
sheet.getRange('B4:B8').clearContent();
}
The function onChange is set to run onOpen (trigger).
Any idea why this script isn't working?
Short answer
Instead of creating a trigger by code create it manually. Set your trigger to call the clearRange() directly.
Explanation
The script in the question is not working because there is a bug but rather than fixing the bug could be better to set a trigger manually otherwise a new trigger will be created every time that the spreadsheet be opened.
To create a trigger manually, follow the instructions from https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_manually
Managing triggers manually
To manually create an installable trigger through a dialog in the
script editor, follow these steps:
From the script editor, choose Resources > Current project's triggers.
Click the link that says: No triggers set up. Click here to add one now.
Under Run, select the name of function you want to trigger.
Under Events, select either Time-driven or the Google App that the script is bound to (for example, From spreadsheet).
Select and configure the type of trigger you want to create (for example, an Hour timer that runs Every hour or an On open trigger).
Optionally, click Notifications to configure how and when you will be contacted by email if your triggered function fails.
Click Save.
Comments about the code on the question
.forSpreadsheet(sheet) requires that sheet be a spreadsheet object but your code assigns a range to the sheet variable.
From https://developers.google.com/apps-script/reference/script/spreadsheet-trigger-builder#onchange
var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger("myFunction")
.forSpreadsheet(sheet)
.onChange()
.create();
See https://developers.google.com/apps-script/troubleshooting to learn how to use the debugging tools on the Google Apps Script Editor

How to run or link an OnEdit standalone trigger in a google spreadsheet

I'm developing new applications in google.
I have a function that is running without any problem if it's bound in the spreadsheet but when it is offline it doesn't work. That's why I saved this script in google drive in order to be called from my google spreadsheet even if it's offline.
I don't know how I link this function to my spreadsheet in order to run both offline.
function onEdit(event)
{
var DistSheet = event.source.getActiveSheet();
...
}
If you want to use a simple trigger like onEdit, it is unfortunately not possible (see the documentation: onEdit is a Simple Trigger, which is restricted to Bound scripts).
However, you can accomplish this with Installable Triggers.
Code for stand alone script
ScriptApp.newTrigger('myFunction')
// insert the file ID for the spreadsheet you'd like to edit
.forSpreadsheet('1234567890abcdefghijklmnopqrstuvwxyz')
.onEdit()
.create();
function myFunction() {
var DistSheet = event.source.getActiveSheet();
// continue script
}
Once you've added this to your script, click the right arrow ("play") icon. It will ask for the proper authorization and then should work after that.
You can see the documentation on Installable Triggers for more information.

Trigger script on google spreadsheet when a new row is inserted

I'm running a script on a google spreadsheet, that should be triggered by the onChange() event (when a new row is inserted in the spreadsheet) by a zap from Zapier (www.zapier.com). I can see the new info being created in the spreadsheet, but the trigger is not triggered. I already tested with the onEdit() event, but it's not working. It should not be a time-based trigger.
Any suggestions?
I'm getting data from external api (in my case IFTTT) and by using below function, I'm triggering a url fetch function to send data (the last Row) to another external api ( my own website).
Working perfectly.
function onChange(e) {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('name of the function you need to trigger')
.forSpreadsheet(ss)
.onChange()
.create();
}
If you have not set up the trigger in the script:
Go to resources menu, and select 'current project triggers'. you should be able to set up your script to run 'on change'.