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.
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 have a spreedsheet, with a sheet feed by a Google Form. unfortunetly, I need special features, like conditional formatting, and data added by google form are added in a new line without format.
So, I did an other sheet, with the same columns, and I had to copy paste from the first one to the second one. I'd like this copy to be automated. So I tried to do it using Google App Script. But it seems like I did something wrong with the trigger.
Here is the code :
function creerTrigger()
{
ScriptApp.newTrigger('deplacerCommande').forSpreadsheet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").onFormSubmit().create();
}
function deplacerCommande(e)
{
var id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var feuille = SpreadsheetApp.openById(id).getSheetByName("Commande");
feuille.appendRow(e.namedValues);
}
When I run deplacerCommande() from Google App Script, the messageBox is displayed, but not when a form is submitted, so I don't think the trigger works.
Yes, I created my trigger. I did it by running the function creerTrigger() from Google App Script. I also tried deleting it, then changing the version, then recreating it, because I read it can solve the problem, but it didn't.
list of trigger
It's in French, but I'll translate the value for you :
deplacerCommande is the name of the function called by the trigger.
the second value means From the spreedsheet.
the third means sending Form
The solution was simple. The trigger triggered, but the appendRow function raised an Exception because namedValue is an object, not an arrow. using e.values instead of e.namedvalues solved the problem!
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.
I'm using this code to use data in a Google Apps Script:
function getCurrentRow() {
var currentRow = SpreadsheetApp.getActiveSheet().getActiveSelection().getRowIndex();
return currentRow;
}
But when I use other sheet than the first one (number "gid=0"), my function remains getting data from that first sheet and not from my current active sheet. Since I'm using the method .getActiveSheet() how can I fix that?
PS - I'm not calling the code from a cell. I'm using this code:
http://opensourcehacker.com/2013/01/21/script-for-generating-google-documents-from-google-spreadsheet-data-source/
I created from scratch a brand new spreadsheet, within which, I created a few tabs/sheets with some data within each; and then in google apps script in that spreadsheet I installed the following code:
function getCurrentRow() {
var currentSelection = SpreadsheetApp.getActiveSheet().getActiveSelection()
var currentValue = currentSelection.getValue();
var currentRow = currentSelection.getRowIndex();
Logger.log(currentValue);
Logger.log(currentRow);
return currentRow;
}
I ran the script, and it gave me the correct results for the sheet that was open/which cell was selected. So I would say that this code pretty much works as you expect.
In your case, may I suggest that the quickest way to get more info about the error, or to see if the error persists, is for you start from scratch too, with a new spreadsheet, and paste in the code above, and then test to prove that at least that much works for you too. Then, only after this, paste in the greater code (that you have linked to), and see if it still/stops working.
I'm having the same problem while developing in the Script Editor -- the Script Editor instance/window becomes 'disconnected' from the Sheets instance/window and just has the first sheet / A1 etc as the 'actives'.
What worked for me:
Closing the Script Editor window and re-opening from Sheet > Tools > Script editor. Voila, .getActive...()s are working again.
Also:
As implied by some of the other answers, triggering the execution from the Sheets window/instance (probably always) also works. One of the answers calls a function from a cell, which means it's going to be triggered by the Sheet. Another option would be to add a .UI menu and menu-option and trigger it there.
Do you use your function as a formula?
Maybe I'm not understanding well the case, but using your code as a formula, it works as expected.
In Sheet1 (Active) apply the formula:
Then manually change the Sheet2 (Active) and apply the formula again:
Its because you are calling it from a custom function in a cell. You cant do that because custom functions must be deterministic. The answer from wchiquito works because he is using a different cell to try the 2nd case. If you use the same cell it will show the old cached result. Search s.o. for the issues about dedeterministic functions. Ive provided workarrounds in those (basically pass a second param =now()
create a spreadsheet in google drive and save it.
In the spreadsheet go to tools menu and script editor
and type the function.