Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9628

Is there any way to set different formulas for multiple ranges all at once?

$
0
0

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?


Viewing all articles
Browse latest Browse all 9628

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>