How to trigger the onEdit event of a referenced cell? - google-apps-script

I am writing Google app script of Google spreadsheet, but I encounter some problem. I try to trigger the "onEdit" function by a cell whose value is referenced by other cell. However, it doesn't seem to work well. For example, the value of A cell is referenced by B cell(the value of A is "='B'"), and if I changed the value of B cell, A cell will change accordingly, but fail to trigger the onEdit function of A cell. I am thinking if it is because the change made automatically by spreadsheet is not an event that will trigger the onEdit function.
Does anybody know other solutions to solve this problem?
Thanks in advance!

onEdit will run any time a cell is updated (as you assumed). Using your logic above, would it work to set a check in the onEdit event to see what cell was changed, and if it was B, could you make the logical assumption that A changed as well?
function onEdit(event)
{
// 'event' contains information about the edit that took place
// Here, we read the 'source' and get the active range (our changed cell)
// We then pull out the string form of the notation to get the cell that changed
var changedCell= event.source.getActiveRange().getA1Notation();
Browser.msgBox(changedCell)
if (changedCell == 'B1') {
// If the changed cell is the one that affects A, do something
Browser.msgBox('You changed B1, and this will affect A');
}
}

As you've mentioned, the onEdit would be triggered for cell B not cell A. You can use this to read off cell A or you can have a function triggered every minute to read cell A.

Related

How to round a cell after user input in Google Sheets?

It should be a very simple function but I can't find anything that functions the way I need.
I want to have a cell where a number can be entered, then after its entered it is replaced by that number rounded to its nearest 0.25
For example:
I enter 5.26 into cell A1, after i press enter the cell now says 5.25
This should only happen with cell A1, so if i enter 5.26 elsewhere it will stay as 5.26
Any help? Thanks in advance and sorry if this a common question.
This can be achieve by using Google Apps Script and onEdit Trigger.
Follow these steps to achieve the desired goal.
In your Google Sheet, go to Tools -> Script editor.
Delete any code in the script editor and paste code provided below.
Click Save.
Go back to your Google Sheet and type numbers in cell A1.
Press enter and wait the value to change.
Code:
function onEdit(e) {
var range = e.range;
var input = e.value;
if(range.getA1Notation() == "A1" && !isNaN(input)){
var number = (Math.round(input * 4) / 4).toFixed(2);
range.setValue(number);
}
}
Demo:
Triggers let Apps Script run a function automatically when a certain
event, like opening a document, occurs. In your case, we need to use
onEdit since it runs automatically when a user changes the value of
any cell in a spreadsheet. Apps Script passes the
triggered function an event object that contains information about the
context in which the event occurred (usually named e).
In the demo above, I created a condition that will check if the edited cell is A1 and if the inputted value is number. If both satisfy the condition, the script will calculate the value and use range.setValue() to change the value of A1.
References:
Class Range
Range.setValue(value)
Simple Triggers
Event Object

Google script onEdit function not triggers if same value entered in cell

I'd like to run onEdit function in a google script. There is a cell, where all entered value summarized. But if the same value entered in a cell, it is not triggering.
e.g: cell value : 300. When 100 entered, new cell value will be 400. It works. But, if cell value: 300, when 300 entered again (to let it be 300+300=600), onEdit not triggers.
(a time ago i had a workaround: i wrote a postfix after the number from my script, and a user only write a number into a cell. But with that workaround, buit in functions which works with numbers not worked.)
Do you have any tips how to trigger onEdit in this situation?
Thanks in advance!
Example:
function onEdit(e) {
Browser.msgBox("onEdit fired!");
}
A3 cell now contains value= 200. If i write in cell A3 200 again, message box not show up -> onEdit function not fired. If i write any other value than 200, it works.
As per the official documentation:
The onEdit(e) trigger runs automatically when a user changes the
value of any cell in a spreadsheet.
In your case, no value is changed since you are replacing 200 with 200 therefore onEdit is not executed/triggered. This is an intended behaviour and not a bug or anything like that.
A different approach would be to delete the value in the cell and type again 200 or use onSelectionChange(e) but this should be used carefully since a code is going to be executed every time you change your selection.

Using onEdit(e) in google sheets to automattically add a value to a specific cell

I want to use the onEdit function to automatically add a value to an empty cell, I can't get the onEdit function to work however. How do you correctly use onEdit?
This sheet is connected to a form, if that form adds data to the sheet, lets say column 5, I want it to add a value to the cell G5.
How can I find out which column has been edited?
Please point it out if these questions have been answered before, I could not find anything about this topic.

In google sheets - how do I apply an onEdit trigger to run on just one cell on one sheet?

Thanks in advance for your help ...
I need to run a script upon editing just a single cell in a specific sheet.
A new row is first appended to the bottom of the dataRange.
The user first selects a date from a datePicker in Col A and then selects an entry from a list (using Data Validation) in Col B.
The idea is to use the textFinder to find the first previous row that matches the selected list item in Col B so that other cells on the new row can be populated from the found matched row.
All is working well except for the trigger.
Any ideas on how to trigger the script would be gratefully received!
Answer:
You can use an onEdit(e) trigger containing a conditional which only executes if a certain cell has been edited.
Code:
You can run whatever script you like on the edit of a specific cell by first checking the range of the edit using the event object:
function onEdit(e) {
if (e.range == 'A1') {
// put the code you want to run here
}
// you can put additional else if statements here if you want other code to
// execute on other cells being edited
else {
return;
}
}
Make sure to run the script manually one time first so that you can authenticate!
References:
Simple Triggers
Event Objects

How to fire a Google Script function onEdit of just one particular cell in a sheet?

I have a Google Sheet with a custom script containing a function which I want to fire ONLY when cell A1 is edited. I've got it so that my function runs every time anything in the sheet is edited, but I want it to run only when cell A1 changes (and, ideally, only when it changes to something other than empty or null). Is there a way to do this? This is what I've got at the moment:
function inventoryAdd() {
// ...custom function stuff...
}
function onEdit(event) {
inventoryAdd();
}
there are many ways to do it like inspecting the changed range in the "e" parameter. the easiest way is to simply remember (in script properties service) the last value you saw of that cell (from the previous onEdit call) and compare it with the current value.