- You want to recalculate the custom function of
=findRate()
, when the cells of the sheet name of Rates
are edited.
If my understanding is correct, how about adding the following sample script? Please think of this as just one of several answers.
Solution:
In order to recalculate the custom function, in this answer, the formula of =findRate()
is overwritten by the script running with the OnEdit event trigger (in this case, it's the simple trigger.). By this, the recalculate is executed. But, when the formula is directly replaced by the same formula, the recalculate is not executed. So I used the following flow.
- Retrieve all ranges of cells which have the formula of
=findRate()
from the sheet of "Projected Revenue".
- Clear the formulas of the ranges.
- Put the formulas to the ranges.
By this flow, when the cell of the sheet of "Rates" is edited, the custom function of =findRate()
is recalculated by automatically running onEdit()
.
Sample script:
Please copy and paste the following script to the script editor. Then, please edit the cells of sheet name of Rates
. By this, onEdit()
is automatically run by the OnEdit event trigger.
function onEdit(e) {
var range = e.range;
if (range.getSheet().getSheetName() == "Rates" && range.rowStart > 1 && range.columnStart > 1) {
var sheetName = "Projected Revenue"; // If you want to change the sheet name, please modify this.
var formula = "=findRate()";// If you want to change the function name, please modify this.
var sheet = e.source.getSheetByName(sheetName);
var ranges = sheet.createTextFinder(formula).matchFormulaText(true).findAll().map(function(e) {return e.getA1Notation()});
sheet.getRangeList(ranges).clearContent();
SpreadsheetApp.flush();
sheet.getRangeList(ranges).setFormula(formula);
}
}
Note:
onEdit(e)
is run by the OnEdit event trigger. So when you directly run onEdit(e)
, an error occurs. Please be careful this.
- In this sample script, as a sample, even when the row 1 and column "A" of the sheet of "Rates" are edited, the custom function is not recalculated. If you want to modify this and give the limitation of range you want to edit, please modify the above script.
References:
If I misunderstood your question and this was not the result you want, I apologize.
Added:
The proposal from TheMaster's comment was reflected to the script. When sheet.createTextFinder(formula).matchFormulaText(true).replaceAllWith(formula)
can be used, also I think that the process cost will be much reduced. But in my environment, it seemed that the formulas are required to be cleared once to refresh the custom function, even if flush()
is used. So I have proposed above flow.
But, now I could notice a workaround using replaceAllWith()
of TextFinder. So I would like to add it. The flow of this workaround is as follows.
- Replace all values of
=findRate()
to a value in the sheet of Projected Revenue
using replaceAllWith()
..
- In this case, as a test case, the formulas are replaced to
sample
.
- Replace
sample
to =findRate()
using replaceAllWith()
.
By this flow, I could confirm that =findRate()
is recalculated. And also, it seems that flush()
is not required for this situation.
Sample script:
Please copy and paste the following script to the script editor. Then, please edit the cells of sheet name of Rates
. By this, onEdit()
is automatically run by the OnEdit event trigger.
function onEdit(e) {
var range = e.range;
if (range.getSheet().getSheetName() == "Rates" && range.rowStart > 1 && range.columnStart > 1) {
var sheetName = "Projected Revenue";
var formula = "=findRate()";
var tempValue = "sample";
var sheet = e.source.getSheetByName(sheetName);
sheet.createTextFinder(formula).matchFormulaText(true).replaceAllWith(tempValue);
sheet.createTextFinder(tempValue).matchFormulaText(true).replaceAllWith(formula);
}
}
C4: =FINDRATE(A4)
. Then when A4 changes, this formula in C4 will too. – tehhowch