onEdit Installable Trigger not firing - google-apps-script

I'm trying to create a script in Google Sheets that will send me an automated email every time that a value within a given column ("Column H") drops below a certain threshold. I've currently got the following script written, which includes an installable onEdit trigger. However, the script is not running when the spreadsheet is edited:
function StationeryEdited(e){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getRange(2, 8, 200, 1).getValues();
var newValue = e.value;
if (newValue < "10"){
MailApp.sendEmail("test#test.co", "TEST", "TEST");
}
function createSpreadsheetEditTrigger(){
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger(StationeryEdited)
.forSpreadsheet(ss)
.onEdit()
.create();
}
}

Have you tried putting function name in quotation marks like this?
ScriptApp.newTrigger('StationeryEdited')
Also, you can simply go to Edit -> Current project triggers and configure the trigger for your function manually select the following options for the 'StationeryEdited' function

Try this and set the installable trigger through the script editor:
function onEdit(e) {
var editColumn=e.range.getSheet().getActiveCell().getColumn()
if(editColumn==8 && e.value>10){
MailApp.sendEmail("test#test.co", "TEST", "TEST");
}}

Related

Automatically adding default text to column when new row is added google sheets

I'm trying to create a script that will trigger whenever a new row is added to my sheet, and add a text to column f in the new row only. I have the following script, but it didn't work. Would love your help. Thanks so much!
function myFunction(e) {
var sh = SpreadsheetApp.getActiveSheet();
if(e.changeType === 'INSERT_ROW') {
var row = sh.getActiveRange().getRow();
sh.getRange(row, 6).setValue('defaultValue')
}
}
The code is excellent, I was able to make it work, however I notice that in order to make it work you need to make sure to add a trigger.
In this case "onChange". On App Script Make sure to go add a trigger for this function to work over the spreadsheet
You can add triggers programmatically
var ss = SpreadsheetApp.getActive();
function createSpreadsheetChangeTrigger() {
ScriptApp.newTrigger('onChange')
.forSpreadsheet(ss)
.onChange()
.create();
}
function onChange(e) {
if (e.changeType === 'INSERT_ROW') {
var row = ss.getActiveRange().getRow();
ss.getRange(row, 6).setValue('defaultValue')
}
}
That's because the onChange trigger is installable and you can do it from the user interface or with the code above. The formSubmit and onEdit work in the same way.

Function based trigger using Sheet instead of Spreadsheet failing

I have two form feeding into the same sheet and I need different triggers for each sheet.
Tried to trigger using function based triggers but that is not working.
This function works:
function testTrigger() {
var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger("myFunction")
.forSpreadsheet(sheet)
.onChange()
.create();
}
This one fails:
function testTriggerTwo() {
var sheet = SpreadsheetApp.openById("...").getSheetByName("Responses");
ScriptApp.newTrigger("myFunction")
.forSpreadsheet(sheet)
.onChange()
.create();
}
with
"Exception: The parameters (SpreadsheetApp.Sheet) don't match the method signature for ScriptApp.TriggerBuilder.forSpreadsheet."
Any ideas?
function testTrigger() {
var sheet = SpreadsheetApp.getActive();
ScriptApp.newTrigger("myFunction")
.forSpreadsheet(sheet)//this sheet is a class Spreadsheet
.create();
}
This one fails:
function testTriggerTwo() {
var sheet = SpreadsheetApp.openById("...").getSheetByName("Responses");
ScriptApp.newTrigger("myFunction")
.forSpreadsheet(sheet)//this sheet is a class Sheet
.onChange()
.create();
}
One of the problems in dealing with Google Apps Script and Google in general is that the people that sell products decided to begin to use the term Sheet as being synonymous with Spreadsheet but in the Apps Documentation Sheet refers to Excel like worksheet and I guess Spreadsheet refers to the excel like workbook. One is the file and the other is a tab. In the second version of the function you are using a Class Sheet when you should be using a Class Spreadsheet.
I would recommend using something like this for trigger creation to avoid the possibility of creating more that one trigger for any given function which can produce issues that are hard to debug.
function testTrigger() {
const ss = SpreadsheetApp.getActive();
if (ScriptApp.getProjectTriggers().filter(t => t.getHandlerFunction() == "myFunction").length == 0) {
ScriptApp.newTrigger("myFunction")
.forSpreadsheet(ss)
.onChange()
.create();
}
}
By checking that none of the project project triggers have a handler with the same function name you can insure that you don't have two triggers for the same function.
You can control it by getting the the sheet name (where the response is landing)
function onFormSubmit(e) {
var sheet = e.range.getSheet().getName();
//do whatever you want by comparing the sheet name
}
You can have only one onFormSubmit Trigger.
But, you can do different things by an if statement

How to trigger a google spreadsheet function onEdit?

function oneWeek() {
var sheetURL = SpreadsheetApp.getActive().getUrl()
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
var sheet = spreadsheet.getActiveSheet();
// do stuff with sheet
}
How do I get this function to trigger onEdit?
I've tried this for the sheet,
function onEdit(event) {
var sheet = event.source.getSheetByName('x');
// do stuff with sheet
}
EDIT: I forgot to mention that my script did https request so that's the reason onEdit didn't work. The solution is here: https://productforums.google.com/forum/#!topic/docs/0nxLYWXVo6Y
Because this script does other things, like a HTTP request, it cannot be assigned to the default onEdit() functions, you need to create it as a custom function and assign it in the Edit menu. Edit->Current Project Triggers and create a new one for onEdit. Just point it at your function and you’re done.
function onEdit(e){
if(e.range.getSheet().getName()!='x'){return;}
//do stuff on sheet x here
}

Set trigger in script in Google Sheets programmatically

I am trying to get a Google Apps Script to trigger on edit of a particular cell. Right now I have achieved this using a run-time trigger option built in Google Sheets. However, I lose the run-time trigger when I make a copy.
Hence, I have managed to find a programmable trigger from various sources and patched something together, but it is not working. I have no programming knowledge, so I am unable to understand where I am going wrong.
My objective is to run the script when user edits the Named Range "monthfilter".
With script trigger creation
function HideColumns() {
//open the current spreadsheet and get the value of the named range and trigger project
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger("monthfilter").forSpreadsheet(ss).onedit()
var name = ss.getRangeByName("monthfilter");
var namevalue = name.getValue();
My previous function:
function HideColumns() {
//open the current spreadsheet and get the value of the named range
var ss = SpreadsheetApp.getActive();
var name = ss.getRangeByName("monthfilter");
var namevalue = name.getValue();
//show or hide February
if (namevalue == "February") {
var sheet = ss.getSheetByName("MIS");
sheet.hideColumns(30);
/** other stuff */
Not sure what is the 'runtime trigger' you mean, there are three kinds of trigger:
Simple trigger
Installable trigger
Time driven trigger
onEdit(e) is just a Simple trigger.
Let's take a look to the following situation:
yourMainSpreadsheet - (You are the owner)
|
binding script functions: - 1. function makeCopy( ) {...}
2. function monthFilter(e) {...}
3. function onEdit(e) { monthFilter(e); }
|
| After copying, new spreadsheet remains all functions and simple trigger.
V
newSpreadsheet- (You are the owner, maybe share with other editors.)
|
binding script functions: - 1. function makeCopy() {...}
2. function monthFilter(e) {...}
3. function onEdit(e) { monthFilter(e); }
No matter what kind of trigger you use or just manually run makeCopy() in yourMainSpreadsheet, onEdit(e) would be copied to new spreadsheet too, and it works when you or other editors make edits, you don't need to do anything:
function makeCopy(){
var mainSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var newSpreadsheet = mainSpreadsheet.copy('newSheet');
}
function monthFilter(e) {
var thisSheet = e.source.getActiveSheet();
var name = thisSheet.getRangeByName("rangeMonthfilter");
var nameValue = name.getValue();
//...
}
function onEdit(e) {
monthFilter(e);
}
What you have done is set a installable trigger to new spreadsheet.
Highlight of documentation about difference between them.
Installable triggers, however, offer more flexibility than simple
triggers: they can call services that require authorization ...
For example:
function monthFilter(e) {
var thisSheet = e.source.getActiveSheet();
var name = thisSheet.getRangeByName("rangeMonthfilter");
var nameValue = name.getValue();
// If you have operation like:
var onlyYouHavePermission = SpreadsheetApp.openById('xxxxxxxxx');
/* This is not allowed if you just use Simpler trigger onEdit(e) to
drive monthFilter(). */
}
For this, now is the time to use installable trigger:
function makeCopy(){
var mainSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var newSpreadsheet = mainSpreadsheet.copy('newSheet');
ScriptApp.newTrigger('monthFilter')
.forSpreadsheet(newSpreadsheet)
.onEdit()
.create();
/* The host of this trigger is you, it's like you
deploy a trigger to the newSpreadsheet, and make other editors
available to do something on "onlyYouHavePermission" through it.*/
}
Update
If all you want to do is just in HideColumns() you provide, you just need to add a new function which is a simpler trigger in script editor:
function onEdit(e) {
var ss = SpreadsheetApp.getActive();
var name = ss.getRangeByName("monthfilter");
var namevalue = name.getValue();
if (namevalue == "February") {
/* If you copy the spreadsheet, of course including the sheets
inside it, and you share the permission of the new spreadsheet to
other editors, because they own the permission, authorization
is not required here.
*/
var sheet = ss.getSheetByName("MIS");
sheet.hideColumns(30);
}
}

onEdit Email Notification in Google Sheets

I'm looking for a way to trigger an automatic email notification to myself when any value within a column of data ("Column H") drops below a certain threshold. Ideally, the notification would include the name associated with the value, which is stored in a separate column of the spreadsheet ("Column A").
My best guess so far is that I need to use an onEdit function, which has a condition that checks the values within the specific column and then sends a email when a value meets that condition.
Here's what I have so far, but I'm having issues with completing this.
function onEdit(e){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange("H1:H200").getValues();
var newValue = e.value;
}
if (newValue < "10"){
MailApp.sendEmail("test#test.com","TEST", "TEST");
}
Any help would be appreciated. Thanks!
Update: Here's what I currently have, following the creation of an installable trigger. I'm getting a 'value' undefined error at the moment and think that possibly syntax/formatting are to blame.
function createSpreadsheetEditTrigger(){
var ss = SpreadsheetApp.getActive();
ScriptApp.newTrigger(StationeryEdited)
.forSpreadsheet(ss)
.onEdit()
.create();
}
function StationeryEdited(e){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getRange(2, 8, 200, 1).getValues();
var newValue = e.value;
if (newValue < "10"){
MailApp.sendEmail("justin#bond.co", "TEST", "TEST");
}
}
Does this feature doesnt help you?
Tools >> Notification Rules