Reputation: 55
I am trying to figure out how to actively recalculate cell values when using script editor function the same way as googlesheet does.
For example when we have number 2 in Cell A1 and number 2 in A2 then if we do in cell A3 "=A1+A2" and then we dive the result in cell A4, if we change eiher A1 or A2 both A3 and A4 will actively update.
So in script editor if we do:
Function sum_cells(){
var sheet=SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getRange("A1").setValue(2);
sheet.getRange("A2").setValue(2);
var cell1=sheet.getRange("A1").getValue();
var cell2=sheet.getRange("A2").getValue();
sheet.getRange("A3").setValue(cell1+cell2);
var cell3=sheet.getRange("A3").getValue();
sheet.getRange("A4").setValue(cell3/4)}
Is it possible to actively update the result in A4 and A3 if the cell values in A1 and A2 change?
Upvotes: 1
Views: 82
Reputation: 38346
Yes, it's possible by using a custom function. For details please read https://developers.google.com/apps-script/guides/sheets/functions
A very simple custom function that use the JavaScript +
operator and two operands look like this:
/**
*
* @customfunction
*/
function mySum(a,b){
return a + b;
}
NOTES:
Custom functions
When the spreadsheet be opened and every time that a
or b
change the custom function will be recalculated.
Also it could be possible by using an on edit simple or installable triggers but I think that using a custom function it's the more appropriate way.
For example when we have number 2 in Cell A1 and number 2 in A2 then if we do in cell A3 "=A1+A2" and then we dive the result in cell A4, if we change eiher A1 or A2 both A3 and A4 will actively update.
To use the above code example to do what was described in above quote from the question, do the following:
2
2
=mySum(A1,A2)
=A3
A3 and A4 will show 4
.
Then when the values entered in A1 or A2 change, the values in A3 and A4 will change.
2
to 3
A3 and A4 will show 5
2
to 1
A3 and A4 will show 4
Related
Upvotes: 1
Reputation: 2598
After reading your request, I understood the following:
In that case you can use the following example to fulfill your requests:
function cellUpdater(e) {
var sheet = e.source;
if (e.range.getA1Notation() == "A1" || e.range.getA1Notation() == "A2") {
var A3 = sheet.getRange("A1").getValue() + sheet.getRange("A2").getValue();
var A4 = A3 / 4;
sheet.getRange("A3").setValue(A3);
sheet.getRange("A4").setValue(A4);
}
}
That code will run at every modification in the sheet. If the modification is done in the A1 and A2 cells, it will update A3 and A4 based on your definition.
OnEdit()
type. You can add the installable trigger from the App Script editor, using the menu Edit 🠊 Current project's triggers
. There you must click + Add Trigger
and select the following parameters:
Please take this as one of the possible solutions to your issue, and don't hesitate to write me back with any additional doubts or requests to further clarifications.
Upvotes: 0
Reputation: 1148
if you write a function in your GS file like this:
function onEdit(e) {
//Logic Goes Here
}
it will trigger every time someone edits a cell, more importantly it will give you a reference object that you can use to determine where and when the change took place. use Logger.log(e) inside of the function to see what the change consists of by running the fuction and looking in view>logs
Upvotes: 0