Google Sheets onEdit(e) function keeps on running using the previous code even if I modify it or delete it totally or even if I rename it to onEdit_DISABLED(e).
Why can I not apply changes to the script code of onEdit(e) function or why does it keep on running even if I delete it totally [by deleting project], the spreadsheet still keeps on reacting to this function and the previous script keeps on running.
Go to https://myaccount.google.com/permissions?pli=1 and revoke access for your script.
Otherwise you might want to provide access to the spreadsheet (or a copy of it) so we can look into it in more detail.
Related
I have a custom function that auto updates when a change is made on the google spreadsheet, but when some other users edits the sheet the function doesn't work for them, how can i make it update for anyone that uses the sheet.
The function auto updates using this:
function onEdit(e) {
SpreadsheetApp.getActiveSheet().getRange('Z1').setValue(Math.random());
}
Then passing the parameter of Z1.
Explanation:
Just a couple of notes to make sure we are on the same page.
This is not a custom function but an onEdit trigger function.
As the name suggests it is triggered when user changes the value of any cell in a spreadsheet.
The function is only triggered when the change of the cell is made by a user and not by formulas or scripts.
Potential Issues:
Make sure the other users have edit permissions on the target cell. If they are not allowed to edit cell Z1 in the activeSheet which might be any sheet in the spreadsheet file, then the script won't work for them. You can either give them access, or make the script work only under particular sheets when you can give them access.
Some users sometimes have experienced issues when tried to chain with active methods directly. See reference links here and here although I can't reproduce that issue, it might be the cause in your case.
Potential Solution:
Please take care of the first issue.
Regarding the second issue, modify your code as follows:
function onEdit(e) {
const active_sheet = e.range.getSheet();
active_sheet.getRange('Z1').setValue(Math.random());
}
This is anyway the recommended way to use an onEdit trigger as you should take advantage of the event object.
I created a Google Script for work (that I don't want to share here) and that works perfectly fine with one onOpen() trigger.
Since I was working on that script on a personal Spreadsheet for testing purposes, I eventually moved it to the real Google Sheet it was meant to run on from the beginning and while the onOpen() function still works properly by being called via "Run function" or even "Debug function", the trigger is not fired, and I'm not getting any log or debug info that could potentially help me.
I deleted and recreated the trigger several times as I saw that this was a known work-around for the issue, but it's not changing anything. When I then go check "Current triggers", I see that it hasn't fired even once and I'm clueless as to what's causing this issue...
While not wanting to give away the whole script since it works anyway, I'm providing the code for my triggered function, even though it didn't change since it last worked successfully on my test spreadsheet:
function onOpen()
{
var aSheet = SpreadsheetApp.getActiveSheet();
var emailRow = 2;
var emailColumn = 3;
for(; !aSheet.getRange(emailRow, emailColumn).isBlank(); emailRow++)
{
aSheet.getRange(emailRow, 19).setValue(matchNPandCIO(emailColumn, emailRow));
}
}
The only thing I can add and that's probably not related is that the spreadsheet on which I now have the script trying to run had an ownership change today and that I'm now its owner. I'm running out of ideas and things to try...
And yes, I also made sure that I gave the script the required authorizations it needs (done after creating the trigger).
Simple triggers like onOpen run anonymously so if matchNPandCIO calls methods that require authorization to run it will fail.
The alternative is to use an installable trigger.
Related
Google Doc onOpen will not fire when spreadsheet is accessed
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?
I have a google sheet with some script running behind it. I've had to separate some of the tabs out to individual workbooks, and have been updating the script accordingly. One of the functions I had was an onEdit(e) trigger, that was no longer running in the new workbook. As part of my testing, I've simplified it to just:
function onEdit(e) {
Logger.log("TEST");
}
No matter the changes I make in the sheet this is attached to, the Logger.log never writes.
I've done some research, especially into the simple trigger restrictions, and one of those relates to openByURL, which I am using elsewhere in the script.
var otherBook = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/etcetc/edit');
Even though the onEdit(e) doesn't directly relate, is the presence of the openByUrl within the script what is preventing the onEdit(e) from running? If so, are there ways to get around that, so that I can trigger actions based on an edit, but also be able to pull data from an alternate workbook?
I inserted the following into a new script and the onEdit(e) worked without an issue. To call functions from onEdit that are restricted for simple triggers, install the trigger instead and set it to fire on spreadsheet edit.
function onEdit(e) {
Logger.log("TEST2");
}
function test() {
var otherBook = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/etcetc/edit');
}
Regarding the Logger, can you try:
Logger.log(["TEST"]); instead of Logger.log("TEST");
Your login line does not work for me either but works with the brackets.
I have a script (Script by spreadsheet) who writes a series of data in a spreadsheet, but some parts of the spreadsheet are locked.
The users that are running this script from the spreadsheet are blocked in certain areas, it is possible that Script release these areas and then lock them again or run as adm of the Spreadsheet?
Thank you.
The user would need to have permission to edit sharing on the Spreadsheet, which would not be the case if it was locked.
Is the script an onEdit trigger? If so, you can add a trigger from the script editor that will run as yourself. Just make sure to rename onEdit() to something else so it doesn't try to run as the regular user as well.
If this isn't onEdit, I would suggest making the function they call add a specific value to a specific cell. Then you can have a timed trigger every minute running as yourself that watches this cell and does its work when the cell contains the specific word. Just make sure to clear the cell quickly so it doesn't run twice.