In my question Is there any way to set different values for multiple ranges all at once?, I found out how to set different values across dis-contiguous cells all at once, and we developed a good answer for that.
Now I'd like to set multiple different formulas across dis-contiguous cells all at once, but there's an issue. Unlike setting values, the array containing the formulas replaces the values in and wipes out the whole target range.
My test data:
| B | C | D |
----------------------
2| 2| 5| 8|
----------------------
3| 3| 6| 9|
----------------------
4| 4| 7| 10|
----------------------
5| 9|kitty |puppy|
----------------------
6|fawn|duckling|joey |
Here's my test code:
var testSheet = spreadSheet.getSheetByName("Tests");
function testSetFormulas() {
var cell = testSheet.getRange("B5:D6");
var formulas = cell.getFormulas();
formulas[0][0] = "=SUM(B2:B4)";
formulas[1][1] = "=AVERAGE(C2:C4)";
cell.setFormulas(formulas);
}
If I put the string (not a formula) kitty
in cell C5
, this code blows that away.
I just want to set the formulas for B5
and C6
with no side effects.
I tried setting array member for C5
to the value "kitty"
. I also tried null
and undefined
and ""
.
formulas[0][1] = "kitty";
formulas[0][1] = null;
formulas[0][1] = undefined;
formulas[0][1] = "";
Each approach still blows away the contents of C5
if C5
contains just a value, not a formula. (It does respect existing formulas)
Is there a way to write multiple formulas at once without replacing existing values in cells that don't need formulas that I want to leave alone?
setformulas
script; thengetValues
for the data range and assign to a temporary array#2; then use the parameters from the temporary array#1 to update the relevant values in the temporary array#2; thensetValues
using the updated temporary array#2.