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'.
Related
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
this script stops just at the entry of the last function : addToDocument().
Until this point all is working. I presume a problem due to onEdit() and DocumentApp call?
Note that separately, my addToDocument() works perfectly.
function onEdit() {
// simple timestamp -- when a single "t" is entered in a cell, replace it with a timestamp
// see https://productforums.google.com/d/topic/docs/rC6MpQDC7n4/discussion
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = SpreadsheetApp.getActiveRange();
if (cell.getValue() == "t") {
cell.setValue(new Date());
}
formatDate() // Some Date formatting using : 'SpreadsheetApp' call
mefCond() // Some conditonnal formatting using : 'SpreadsheetApp' call
doCounts() // Some numéricals opérations, using : 'SpreadsheetApp' call
//At this point the scripts enter in the following function,
//and stops on the first line. Nothing being executed.
addToDocument() // Time stamp on a document using : 'DocumentApp' call
}
Any ideas ?
Thanks for reading,
Eric :-)
When OnEdit is run manually it runs with a different set of permissions but while Triggers themselves have specific restrictions as mentioned here. From that page see..
They can modify the file they are bound to, but cannot access other
files because that would require authorization.
Please refer to this for the authorization modes and what you can do under each of them. May be line 2 below is affecting you...
The solution for you I believe is to convert your simple trigger into an installable trigger. Here are details how to install a trigger for your spreadsheet. Nothing will changes with respect to your onEdit() function, you just have to run installation code snippet once to create an installable trigger.
function createSpreadsheetEditTrigger() {
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger('onEdit')
.forSpreadsheet(ss)
.onEdit()
.create();
}
And here are the details on permissions and other details. In here it clearly mentions that you can access other services..
For example, the installable open trigger for Google Sheets activates
whenever the spreadsheet is opened by any user who has edit access,
just like the simple onOpen() trigger. However, the installable
version can call services that require authorization. The installable
version runs with the authorization of the user who created the
trigger, even if another user with edit access opens the spreadsheet
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.
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
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.