onEdit range always cell A1 - google-apps-script

I've run into an issue with the onEdit simple trigger using the [Google Apps Script] 3 in a Google Doc Spreadsheet.
The range in the event object passed to onEdit always maps back to cell A1.
There are a few other issues I'm encountering with onEdit and the event object but this is a very narrow and specific example which is hopefully easy to ask a question about.
My Repro Steps:
Login to google docs
Create new spreadsheet
Tools->Script Editor (this will open in a new tab)
Replace contents of Code.gs with the following (C&P from [https://developers.google.com/apps-script/understanding_events#eventsDetails])
function onEdit(e) {
e.range.setComment("Edited at: " + new Date().toTimeString());
}
Save changes to Code.gs
Return to the spreadsheet tab
Enter the number 7 into cell C3 then hit enter
Note: What's entered doesn't appear to matter, just need to trigger the onEdit event
Wait for spreadsheet to save change to server
Cell A1 is updated with a comment that says, Edited at: [current time]
The expected behavior is that the comment is added to cell C3. As per the [documentation stating] 2;
[The example in step 4] is a function that is triggered when a
Spreadsheet is edited. It uses the e parameter to access the cell that
was edited and add a timestamp.
Having this basic example from documentation failing leaves me at a loss of what I can be doing incorrectly or how else to approach finding a solution.

With the release of new sheets, a few of the Google Apps Script features where broken, in particular the one that you mention.
You will find documentation on this and other current issues here: https://developers.google.com/apps-script/migration/sheets#onedit_triggers
Be sure that the team is working hard to resolve this and you can probably have a shot as asking a question or two on the subject in wednesday Google Apps Unscripted : https://plus.google.com/u/0/116174873209306927411/posts/MbXQhKusUZ3?cfem=1

Related

Google Sheet Script | Can I access a cell edit history via a script formula?

I'm working with google sheets and I'm trying to figure out a way of how to get the time when a cell is filled in? I would like that access this information via the google sheet script. Is that even possible?
Thank you for your time.
After reading your question, I assume the following:
You want to detect when a particular cell is modified.
When the modification is detected, you want to print a timestamp in another cell.
If my assumptions are correct, you can use the following example to achieve your requests:
CODE
function onEdit(e) {
if (e.range.getA1Notation() == "C4") {
var timestamp = new Date().toLocaleTimeString('it-IT');
SpreadsheetApp.getActive().getActiveSheet().getRange(4, 4).setValue(
timestamp);
}
}
BEHAVIOUR
The code will run when an edit is made on the spreadsheet by a user. It will check if the modified cell is the desired one (C4 in this example), and if it is a timestamp will be dropped in the time-keeping cell (D4 in this case).
OBSERVATIONS
This code works for every sheet of the spreadsheet. To make it exclusive to a sheet, you should use getSheet methods [as getSheetByName()].
Due to onEdit trigger limitations, this code won't run if the modification is made by a script (in opposition to a user edit).
The timestamp will be printed in the italian timezone because your name looks Italian to me. Please, forgive me if I mistook your timezone.
ALLUSIONS
onEdit(event object) simple trigger
Event object
toLocaleTimeString(locale)
Please, don't hesitate to write me back any additional doubts or request me further clarifications.

Send an e-mail if a cell value meets a given criterion

I'm working on a scoreboard automation process using Google Sheets, but I've come up with a little problem. I tried searching here but the question has been treated in a unclear way to me.
What am I trying to do is quite simple:
I want an automatic e-mail to be sent to a specific person IF cell value > X (threshold). I already know I need to use Google Apps Script for that, but I haven't found much interesting code lying around yet so I was wondering if you guys had an idea how that would work?
I tried something based on this thread with no success: How do I make a Google Sheet script send an email when a specific cell's value changes?
#Yvan1401,
You can use a script like this along with an installable trigger like onEdit or onChange to accomplish what you wish. To set up the installable trigger follow the steps below (found here):
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 From spreadsheet.
Select and configure the type of trigger you want to create (e.g. on
Change or on Edit trigger).
Optionally, click Notifications to configure how and when you will
be contacted by email if your triggered function fails.
Click Save.
Please see comments in code for portions to change.
function SendEmail() {
var ui = SpreadsheetApp.getUi();
var file = SpreadsheetApp.getActive();
var sheet = file.getSheetByName("Sheet1"); //Change as needed
if(sheet.getRange(5,1).getValue()>10){ //change row and column in get range to match what you need
MailApp.sendEmail("xxxxxcxxx#gmail.com", "subject", "message");
}
}
function sendMail() {
if (SpreadsheetApp.getActive().getSheetByName('Scoreboard').getRange('AL2:AL1000').getValue()<2.70) return;
MailApp.sendEmail("yvan#********.com", "******** new potential candidate available ! ", "Dear ****, a new candidate with strong potential is available for due dil !", {
name: "SOURCE NAME"
});
}
Here is an example of code that works the best and is the shortest. The thing is, I want it to scan only the cells that has changed (updated) within the column in question and not the old responses (data source is a form). What do you think i should do ? Because i don't want to erase the previous response within the same column.
edit: I use an "onChange" trigger.

How to make new sheet in google spreadsheet selected?

My script adds new sheet on a spreadsheet after some form submission. This sheet is used as a leaderboard. But to look on it I have to click on the new created tab. Is it any way to do it from script?
This picture is a screenshot after new sheet creation (Week 1 Lesson 2).
P.S.
I found that I have to reformulate the question. The problem is that I want to do it from another spreadsheet (this spreadsheet has onSubmit trigger and create and place info on other open spreadsheet (leaderboard). I check the setActiveSheet(sheet). It works only with the spreadsheet that runs the script. So I have to send some signal to leaderboard to activate sheet. But I can't understand how to do that
Straight from the developers guide
setActiveSheet(sheet)
As far as I know, there is no way to use Apps Script to change the active sheet on another open spreadsheet not running the script.
You could install a time-based trigger that ran every minute in the leaderboard sheet that checked for a new entry and then changed the active sheet as needed. This isn't instant but would take at most 60 seconds to update.

Spreadsheet event trigger on Google Apps script doesn't fire when the sheet is edited by another script

I wrote a small script bound to a Google spreadsheet that reads an email-address from a cell in the last row and sends an email to it.
The values are collected by a WebApp (not by Google form).
I tried all kind of triggers. The time driven triggers work, but the spreadsheet triggers don't work. I tried all of them. If I change manually some cell in the spreadsheet the onEdit trigger is working, but it doesn't fire up when the sheet was changed by another script.
Google's dev says : The onEdit() trigger runs automatically when a user changes the value of any cell in a spreadsheet. but it doesn't precise the change could be from script or not.
You'll find here a comment from HDCerberus :
I'm not at my PC to write a full response, but at a glance it could be the OnEdit trigger, which ONLY works when the sheet is edited manually, and NOT by a script. Are you expecting it to run when the sheet is edited manually or by a script?
Not every change triggers onEdit(). Please take a look on answer from topic Detect user inserting row or column in a google spreadsheet and reacting in a script - you will find registered bugs and one of them says that values written by scripts do not trigger onEdit().

How to get the current active sheet in my script?

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.