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

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.

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

How do I create a function that returns the last time a cell was modified?

I'm looking to write a function for Google Sheets Script that accepts a cell reference and outputs the date that cell was last modified. I'm not sure how to get started.
There's no direct approach to get the exact date of a cell's edit.
Method 1
Retrieve the modifiedTime using the Drive API's Files:get which will return, according to the documentation:
The last time the file was modified by anyone (RFC 3339 date-time).
Method 2
What you can do instead is to make use of the onEdit(e) trigger and monitor each change from the particular cell.
So for example, considering that the cell you want to check is A1, you can use this snippet:
Snippet
function onEdit(e) {
if (e.range.getRow() == 1 && e.range.getColumn() == 1) {
var date = new Date();
console.log(date);
}
}
Explanation
The above function is an onEdit trigger which will fire every time there's an edit made on the sheet. Since you want to check this for a particular cell, the event object e has been used in order to get the row and the column of the cell which has been edited. Therefore, the if condition in this situation checks if the row is equal to 1 and the column is equal to 1 (range corresponding to the A1 cell) and based on that, it returns the current date which corresponds in this situation to the edit that has been made.
To track all the edits for a cell, you can store the date variables resulted from the edit to an array and eventually use it afterwards to your liking.
Note
Taking these into account, you won't be able to get the time of a cell which has been edited before having the trigger into place.
Reference
Drive API v3 Files:get;
Drive API v3 Files Resource;
Apps Script Event Objects;
Apps Script Simple Triggers.

Is it possible to set Notes with automatically inserted values using google sheets?

I was trying to figure out a way to insert Notes automatically, when i enter something in a specific cell. This thread was suggested to me and tried it out: https://support.google.com/docs/thread/13317657?hl=en. I changed the function mentioned a littlebit, to fit my project:
function onEdit(e) {
if(e.source.getActiveSheet().getName() === 'Testsheet') {
if(e.range.columnStart === 8 && (e.range.rowStart > 12 && e.range.rowStart < 21)) {
return e.range.offset(0, -6).setNote(e.value);
}
}
}
This way when I enter values in the range H13:H20, the same values get inserted as Notes on the corresponding (0, -6) offset cells. Which is a step in the right direction, but not yet what I'm truly trying to achieve. The problem is, this only works, when the values are entered manually. But for my project, I need the notes also to be set, when values are filled in automatically, using a function that fills in certain information in a row, depending on the selected dropdown choice.
In my sheet for example:
=if(B17="Test1",Testsheet!C10:Testsheet!H10)
When the Information is filled in that way, it doesn't give me the Note in the offset cell. Is there a way, so that it does?
Here is a link to my Testsheet, where you can see what I'm trying to do : https://docs.google.com/spreadsheets/d/16c2hVDM_FTnHWerKjnOauCopwVZSnKCwAMVi6-Z8vBo/edit?usp=sharing
Thanks for any tips and help in advance!
As you can see in the documentation:
Script executions and API requests do not cause triggers to run. For
example, calling Range.setValue() to edit a cell does not cause the
spreadsheet's onEdit trigger to run.
The only option would be to directly run the notes function with the values as parameter instead of inserting the "row 16" values, as updating the sheet by a script or formula won't trigger the function onEdit().

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.

How to trigger the onEdit event of a referenced cell?

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.