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
Related
the following is the script that is triggered by any edit
function changeSpeadsheetName() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getActiveSheet().getRange(7, 1).getValue();
//assuming you want to add the string in A1 to your title
var name = sheet.rename(range);
}
It looks like you have an Installed trigger on this script
I can tell this because the name of the function is not onEdit. If it was the name of the function, it would likely be a Simple trigger, but it could also be an installed trigger.
In a nutshell, simple triggers can be copied with the sheet and it will work "out the box". However, they are very restricted and so, this is why your sheet probably has an installed one.
To install the trigger on the new sheet, you need to open the script editor. Once you are at the editing screen, to the left, click on the triggers icon.
Once there, you can:
It will ask you what function you want to run when the trigger is triggered, and what type of trigger you want it to be (choose onEdit) - then it will likely ask for authorization, which you should grant.
For more details:
Simple triggers
Installable triggers
Using Google Apps Script, is there a way to have a Google Documents file update automatically whenever a Google Sheets file is edited?
I've got a Google DocumentApp file with a script that gets data from a Google SpreadsheetApp file. I'm looking to create a script to automatically update the DocumentApp file whenever the SpreadsheetApp file is edited.
This is the code I'm using currently:
function updateDocumentOnEditTrigger() {
var ss = SpreadsheetApp.openById(SheetID);
ScriptApp.newTrigger('UpdateDocument')
.forSpreadsheet(ss)
.onEdit()
.create();
}
Running the updateDocumentOnEditTrigger function doesn't seem to trigger the UpdateDocument function, which works as it should when manually run.
Answer:
In order to run a DocumentApp script on edit of a Spreadsheet, the On edit installable trigger attached to the Spreadsheet must be used.
More Information:
As per the Simple Triggers documentation, there are some restrictions which need to be taken into account. In particular:
They can modify the file they are bound to, but cannot access other files because that would require authorization.
As a result, the onEdit(e) trigger function can not be used. There is however an installable trigger which can be created, with settings set up such that it can fire on edit.
Code:
With the following function in the script bound to the Spreadsheet file:
function updateDocument() {
var doc = DocumentApp.openById('DOCUMENT_ID');
// here you can put your code that edits the document in the way you want.
}
You can create an installable trigger which runs on the edit of the Spreadsheet. You will need to run the code at least once before setting up the trigger however - this is because DocumentApp needs authorisation and you need to grant it!
###Setting up an Installable Trigger:
With the code set up, you can create the installable trigger by completing the following steps:
From the Apps Script editor view for the bound Spreadsheet script, follow the path Edit > Current project's triggers. This will open the triggers for the project in a new tab or window.
In the bottom left, click on the + Add Trigger button, bringing up the Add Trigger modal. From here, set up the trigger with the following properties:
Choose which function to run: updateDocument
Choose which deployment should run: Head
Select event source: From spreadsheet
Select event type: On edit
And click Save. This will set up the trigger such that your Document editing function will run each time the Spreadsheet is edited.
References:
Google Apps Script - Simple Triggers
Simple Triggers - Restrictions
Google Apps Script - Installable Triggers
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'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.
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'.