Contact

ONLYOFFICE Spreadsheet Editor · Workbook workflows · Intermediate

Write and Test a Bounded JavaScript Macro in ONLYOFFICE

Write four explicit line-total formulas with a small desktop macro, check its target guard, and test reruns without touching source data.

Documentation-qualified; JavaScript control flow checked with a mock API; native API execution and file persistence unverified · Editorial standards

Quick answer

Open View → Macros in Desktop Editors and run a reviewed JavaScript macro only against a disposable practice sheet. This example writes formulas to exactly D2:D5. It does not discover your real data range, inspect a selection, or make a backup for you.

Overwrite warning: Each run replaces the contents of those four cells. Use a new workbook containing only the fixture below. A sheet-name check reduces accidental targeting; it does not identify a workbook uniquely or protect valuable data already in the output cells.

Free desktop scope and API boundary

ONLYOFFICE Desktop Editors is free for personal and professional use. Its desktop macro documentation links to the getting-started procedure, which explicitly starts in Desktop Editors and opens View → Macros. These instructions target Desktop Editors 10.0.0 with English controls, using documentation reviewed on 21 October 2026. No cloud account, paid server, AI provider or extra installation is part of the example.

ONLYOFFICE macros use JavaScript and the editor’s Office API, not Excel VBA. Copying a VBA procedure into this editor is not conversion. The conversion guide describes rewriting the logic; automated conversion and its provider setup are outside this walkthrough.

A subtle boundary matters: the current SetFormula reference labels that method as paid ONLYOFFICE Docs functionality. This example does not use it. The SetValue reference instead documents writing a formula string such as =B1*B2 and carries no corresponding paid-edition note. That documentation is not a native execution test of your installation.

Prepare the disposable workbook

Create and save Macro-Practice.xlsx locally. Rename its first sheet to exactly MacroPractice. Enter the following in A1:D5, leaving D2:D5 genuinely empty:

Row A: Item B: Units C: Unit price D: Line total
1 Item Units Unit price Line total
2 Labels 2 4.5
3 Pads 3 7
4 Clips 0 8
5 Return -1 4.5

The negative quantity models one returned unit; it is intentional, not a missing-value replacement. Enter the prices as numbers. Put =SUM(D2:D5) in F2 as a separate check. F2 is not a macro output.

Add a second empty sheet named OtherSheet for a target-guard test. Keep the new workbook separate from production files and record its path. Review the code before using it; do not enable automatic execution of every macro as a troubleshooting shortcut.

Add the complete macro

Open View → Macros. Create a macro entry if the editor does not already provide an empty one, replace its example contents with this entire script, and rename it PracticeLineTotals through the entry’s rename control.

(function () {
    var sheet = Api.GetActiveSheet();
    if (!sheet || sheet.GetName() !== "MacroPractice") {
        return;
    }
    for (var row = 2; row <= 5; row += 1) {
        var target = sheet.GetRange("D" + row);
        if (!target || target.SetValue("=B" + row + "*C" + row) === false) {
            throw new Error("Stopped while writing practice row " + row);
        }
    }
})();

The GetActiveSheet, GetName, GetRange and SetValue references establish the four API calls. The loop deliberately has a fixed first and last row. There is no file access, network request, event subscription or whole-sheet operation in this code.

Test the guard, then run deliberately

  1. Activate OtherSheet, open the macro editor, select PracticeLineTotals and use its run control. The intended outcome is no changes. The guard returns silently; no success dialog is implemented.
  2. Inspect OtherSheet and MacroPractice’s D2:D5. They should remain empty. A successful guard test is not evidence that the calculation branch ran.
  3. Activate MacroPractice. Verify the workbook path and that D2:D5 are expendable. Run the same macro.
  4. Inspect each formula, not just its displayed result:
Cell Expected formula Expected value
D2 =B2*C2 9
D3 =B3*C3 21
D4 =B4*C4 0
D5 =B5*C5 -4.5

F2 should total 25.5. Columns A:C, the header D1, F2’s formula and all other cells should be unchanged. The zero result is valid because Units is explicitly zero; blank or text inputs have not been validated by the macro.

Rerun and change one input

Run the macro again with the same sheet active. It should write the same four formulas, not append records or double the values. This is an idempotent write pattern, not proof that every macro is safe to rerun.

Change B3 from 3 to 4. D3 should calculate 28, and F2 should become 32.5, without needing a new macro run: the script wrote formulas rather than fixed answer values. Inspect that D3 still refers to B3 and C3. Restore B3=3 and expect 25.5 again.

Do not extend the loop for new rows until you have identified the intended output range and confirmed every affected cell may be overwritten. This four-row example is a controlled introduction, not a general production automation.

Handle partial failures and save evidence honestly

If execution stops, inspect D2:D5 before trying again. The loop is not a transaction: earlier cells may already contain formulas when a later write fails. Correct a simple targeting or input problem only in the disposable workbook, then rerun deliberately. If macros are disabled by your organization’s policy, stop and follow that policy; do not weaken security settings to complete the exercise.

Save and reopen the exact local XLSX. Check the four formulas and total. Inspect whether PracticeLineTotals is available and unchanged in the macro editor before running it again. Macro persistence, export to another editor and compatibility with VBA are separate questions; the source script above remains the reproducible reference, not a guarantee of cross-application portability.

Verification: The script’s loop, wrong-sheet return, exact output addresses, repeated writes and stop-on-failed-write branch were tested with a local mock API. That test does not execute ONLYOFFICE’s API or its spreadsheet engine. Expected values were independently calculated. No native macro run, permission change, screenshot or save/reopen test was performed.