Hello everyone!
I am trying to write a macro to implement a basic goal seek to solve this issue. Despite many years of VB macros and some experience with js, I am struggling with even the most basic things and would need some help. I have boiled down my current issue to the fact that the macro doesn’t seem to “see” updated cell values while it runs. It actually looks like the macro runs entirely, then updates the sheet.
For example, if I have a sheet with an input value in A1 and an output in A2 which is set as a formula as being =10xA1
. if I run this simple code for instance :
(function()
{
var oWorksheet = Api.GetActiveSheet();
//var ActiveCell = oWorksheet.ActiveCell;
console.log("starting macro...");
var input = Number(oWorksheet.GetRange("A1").GetValue());
console.log("original input:"+input);
var output = Number(oWorksheet.GetRange("A2").GetValue());
console.log("original output:"+output);
let n = 0;
while (n < 3) {
n++;
input = input + 1;
console.log("-----");
//update input
oWorksheet.GetRange("A1").SetValue(input);
console.log("input:"+input);
console.log("A1 input:"+oWorksheet.GetRange("A1").GetValue());
Api.asc_calculate(Asc.c_oAscCalculateType.All);
//read updated value
output = Number(oWorksheet.GetRange("A2").GetValue());
console.log("A2 output:"+output);
}
Api.Save();
console.log("A2 output (after save):"+oWorksheet.GetRange("A2").GetValue());
})();
I get this:
starting macro...
VM379:11 original input:53
VM379:14 original output:530
VM379:20 -----
VM379:23 input:54
VM379:24 A1 input:54
VM379:29 A2 output:530
VM379:20 -----
VM379:23 input:55
VM379:24 A1 input:55
VM379:29 A2 output:530
VM379:20 -----
VM379:23 input:56
VM379:24 A1 input:56
VM379:29 A2 output:530
VM379:33 -----
VM379:34 A2 output (after save):530
This shows that the original output is 530 and while the macro updates the cell value in A1, it doesn’t “see” that the output value in A2 has changed based on the formula. It always stays at 530 as if A2 wasn’t being recalcuated until the macro is completed and the sheet then calculates the new value…
I have added the asc_calculate
piece as I thought that perhaps the macro doesn’t recalculate values on each iteration by default but it makes no difference. the other interesting thing is that I can see the values being updated in A1 and A2 before the spreadsheet is saved. However, the output of the A2 value after save it still the old one.
Am I doing something wrong? Ideally I need to run the while loop on the output value and stop iterating once it has reached a defined target but right now, the output doesn’t change so the loop runs forever…
version: ONLYOFFICE Desktop Editors version 7.2.1.36 (flatpak package)
OS: Ubuntu 22.10
On a side note, oWorksheet.GetRangeByNumber
starting at 0,0, for A1 is pretty mind boggling. I get that lists normally start at 0 but this is pretty counter intuituve in my view, especially as it is described as GetRangeByNumber(nRow,nCol)
…