I'm using F# and Excel Interop to output data to an Excel spreadsheet. My first approach was to individually set each cell:
worksheet.Range(range1).Value2 <- "=sum(a1:a10)"
worksheet.Range(range2).Value2 <- "=min(a1:a10)"
worksheet.Range(range3).Value2 <- "=max(a1:a10)"
(* etc *)
However, this is too slow when there is a large number of formulas, so I switched to an array:
worksheet.Range(range).Value2 <- [| "=sum(a1:a10)"
"=min(a1:a10)"
"=max(a1:a10)" |]
or
worksheet.Range(range).Formula <- [| "=sum(a1:a10)"
"=min(a1:a10)"
"=max(a1:a10)" |]
However, now Excel just displays those strings in the cells, instead of calculating the value of the formula. I also tried:
worksheet.Range(range).FormulaArray <- [| "=sum(a1:a10)"
"=min(a1:a10)"
"=max(a1:a10)" |]
But that messed with the target ranges of each formula. I'm not sure what's going on there. (Instead of leaving it as "a1:a10"
, Excel translated the target ranges based on where the formula would be.)
Is there a better way to do this?
Update: I also tried using "=MIN($G$2:$G$5)", but that produced the same effect, where Excel is just displaying the string and not evaluating the formula.
For what it's worth, when if I enter the cell, erase and then re-add any character in the formula, when I hit "enter", Excel will evaluate it.
Update 2: I also tried the following:
worksheet.Range(range).FormulaR1C1 <- [| "=sum(R2C[0]:R4C[0])"
"=average(R2C[0]:R4C[0])"
"=min(R2C[0]:R4C[0])"
"=max(R2C[0]:R4C[0])" |]
But I get the same problem: Excel renders them as strings instead of evaluating the formula.
"=sum($a$1:$a$10)"
? – Daniel