Why is Google Sheets' onEdit function not getting executed? - google-apps-script

I have a Google Sheets spreadsheet that's been working for months, and has no scripts associated with it. Recently I decided I'd like to log the date of change to a row, and that the onEdit() function could do that for me.
From the spreadsheet display, I selected Tools / Script Editor, which popped up an empty script with a myFunction() function.
I modified myFunction() to:
function onEdit(e) {
Logger.log("Something was edited.");
}
Then pressed Save and accepted the default progject name (Untitled project). I also tested this (Run) and it created a log entry.
Now when I edit any value in the sheet or add a new row, I would expect a log entry. But none appears.
Have I missed a step? What must one do in order to get a valid onEdit() to run?

Your logs will be available at View>Stackdriver logging. View>Logs will only show logs executed in the current session (by clicking "Run"). Using console class is preferred.

Disclaimer: I don't often work with the Script Editor in Google Sheets, nor Google Sheets itself, too often. There may be a better way to accomplish this.
In my experience, I've found you also have to explicitly add a trigger to your Script Editor project to get the events to fire correctly.
From your sheet, open your Apps Script project by selecting the Tools > Script Editor menu option.
From the resulting Apps Script project screen, select the Edit > Current Project's Triggers menu option.
From the resulting Triggers view, click the + Add Trigger button (appeared in the lower-right of the interface presented to me; your mileage may vary).
In the resulting modal, select the following options:
"Choose which function to run" - select the onEdit method to execute the method you've written.
"Choose which deployment should run" - I've selected the "Head" option, but I'd imagine your mileage will vary wildly if you've elected to use the more advanced deployment features here.
"Select event source" - set this to "From spreadsheet".
"Select event type" - set this to "On edit".
"Failure notification settings" - set this to whatever setting you'd like.
Finally, click the "Save" button.
Once your trigger is set up, the spreadsheet should now be running your method whenever an edit to the spreadsheet is made.

Answer:
Make sure you run the onEdit(e) function from the Apps Script UI using the play button (►) and authorise the script to run as you.

Related

onOpen "hide sheets" doesn't work for Viewers of a Google Sheet?

I have a "hide sheets" function. I've tried using it as an onOpen function, and also with a On Open trigger.
It works fine if an Editor opens the GS.
But if a Viewer opens the GS, it doesn't work at all.
If it's an On Open trigger, I get an error email that "Authorization is required to perform that action."
If it's called as an onOpen function, it just doesn't work, but also doesn't generate any errors.
Is there a way to make this work for Viewers?
Viewers can't execute scripts including those called by simple triggers like onOpen
One option among many others is to have one spreadsheet for editors and another for viewers then somehow send the changes made by the editors to spreadsheet shared with viewers.
Related
Google Spreadsheet - Show sheets depending on type of user
Getting all viewers of spreadsheet using App Script
Consideration
onOpen trigger should not perform sensitive actions, at most create a menu.
A simple trigger won't be able to run under this low privileged scope (viewer-only).
A better flow would be to show additional sheets to users who have sufficient permissions, not the other way around.
Reference
Triggers Restrictions

API works when running script, but not from associated spreadsheet

I am trying to apply conditional formatting rules on a google sheet using a google apps script. I found that this could be done with the API, so I enabled it and have it all working when ran from my script ... but when I trigger the script to run from my sheet, the API batchUpdate doesn't run. I am sure I'm missing something simple ...
The code is in the Themes.gs project of this spreadsheet: https://docs.google.com/spreadsheets/d/1Fs6B-OmlqfoAoon5PcoAYbHq4CO8w0qPqfSRy_OQhe0/copy
specifically this bit doesn't seem to run when the script is triggered from the sheet itself:
Sheets.Spreadsheets.batchUpdate(JSON.stringify(functionname), SpreadsheetApp.getActiveSpreadsheet().getId())
A simple solution is Installable Triggers.
Rename your onEdit() function to something else, like whenWeEdit().
Click the link that says: No triggers set up click here to add one now.
Under Run, select whenWeEdit (or whatever you renamed your function as).
Under Events, select From spreadsheet.
Next, instead of On open, select On edit.
Click save.
Edit the sheet. Your function now seems to work for me. What about you?

Apps script library automatic updation on client side (on doc refresh)?

I have
an Apps Script Library 'MyLib'.
template Google Spread Sheet ('MyGSSheet'). Through Script Editor I added the library 'MyLib' to 'MyGSSheet'. I set 'Development mode' 'on'.
users get a 'copy' of this template ('MyGSSheet').
How can I have a setup wherein any changes I make to 'MyLib' get picked up across these copies automatically (once the Spreadsheet is reloaded)?
I thought having 'Development mode' 'on' is all that's needed for this continuous update of the code in all the Spreadsheets.
However, I don't see this happening. The copies aren't picking the latest code.
I also granted 'edit' permission to all users within our company domain.
I am not able to comment so I hope I am contributing enough to justify an answer..
So I tried to reproduce this:
I created a standalone App Script 'MyLib' and wrote a single function:
function myFunction()
{
SpreadsheetApp.getUi().alert("TEST");
}
Next I created a spreadsheet and added a script to it via Tools. I'll call it "Spreadsheet Script".
In the Spreadsheet Script I added the MyLib as a library and turned the development mode 'on'.
Also I added two functions to Spreadsheet Script:
function onOpen(e)
{
myFunction();
}
function myFunction()
{
MyLib.myFunction();
}
Ok, now I shared the Spreadsheet and the MyLib - script to my colleague with edit rights. He opened the spreadsheets and got the alert "TEST".
Now when I modified the alert text from the MyLib-script's myFunction to "TEST 2 " and just saved the file (File/Save, did not save a new version), my colleague saw the changes in the spreadsheet. Then, I made him to take a copy of the spreadsheet( To test the effect of the spreadsheet owner change).
I changed the alert the text to "TEST 3". The change was reflected in his copy of the spreadsheet.
Are you able to reproduce this or does this approach fail to update for the other users? I am choosing this kind of simple example as sometimes the reason might reside in the code too...

Some methods/functions simply do not work in onEdit trigger

I'm looking to call UrlFetchApp.fetch() based on user input on a sheet but nothing happens. No errors, just silently ignores the call. Same goes for MailApp.sendEmail() and GmailApp.sendEmail()
This is on a Google Apps domain, and only domain users are using the Google Sheet.
Simple triggers like onOpen or onEdit are not able to do anything that requires authorization like sending mails because they run silently and anonymously.
This is all explained in the documentation .
You should simply rename your onEdit function to something else - SpecialOnEdit for example ? - and create an installable trigger (documentation) from the script editor menu (ressources/triggers/create a new trigger...)
You can see an execution flow in view -> execution transcript in the script editor. Just change a cell value in spreadsheet then come back the script editor and check "execution transcript". It will show you an error if it happens.

How to get onFormSubmit to trigger automatically?

The "fix" (test_onformsubmit) code you gave, I have to manually run it every time there is new data in the spreadsheet. I was wanting it to automatically send the pdf to email when Form is submitted. Is there a way? Because the manual way runs the code exactly like its supposed to, but I want this as an automatic event so I don't have to do anything.
See parent thread of original problem/question
Read Understanding Triggers. This function is an Installable Trigger, so you need to set it up to run when a form is submitted. It's easy - I would have thought a Forms tutorial would have walked through it.
In the Script Editor:
Choose Edit > Current project's triggers. You see a panel with the message No triggers set up. Click here to add one now.
Click the link.
Under Run, select the function you want executed by the trigger. (That's onFormSubmit(), in this case.)
Under Events, select From Spreadsheet.
From the next drop-down list, select On form submit.
Click Save.
From this point on, the function will be triggered whenever a form is submitted to the spreadsheet.
If you plan to share your script, each recipient will need to repeat these steps.
As an aside, you should change the email setting in your script, so it will work for ANYONE.
var email_address = Session.getActiveUser().getEmail();