Why doesnt this macro add 1 to the A1 cell?

Do you want to: Ask a how-to question
OS version: windows 11
App version: last stable x64 .exe
Downloaded from: ONLYOFFICE website.
Additional information:
this is the macro that is not adding 1 to the assigned cell A1:

function add1() {
    var sheet = Api.GetActiveSheet();
    var cell = sheet.GetRange("A1");
    var currentValue = cell.GetValue();
    cell.SetValue(currentValue + 1);
}

Hello @bisqunours

GetValue() returns number as text string instead of actual number. Trying to add up another value to this string concatenates these strings.

I can suggest following workarounds:

  1. Using function to calculate values:
var oWorksheet = Api.GetActiveSheet();
var oCell = oWorksheet.GetRange("A1");
var oVal = oCell.GetValue();
oCell.SetValue("="+oVal+"+1");
  1. If you don’t want to use functions, then you can convert value returned by GetValue() with Number() method:
var oWorksheet = Api.GetActiveSheet();
var oCell = oWorksheet.GetRange("A1");
var oVal = oCell.GetValue();
var nVal = Number(oVal);
oCell.SetValue(nVal+1);

Both of these samples will result in adding up 1 to the previous value of the specified cell.
I hope it helps.