You can create macros in ONLYOFFICE Docs by opening the Plugins tab, selecting Macros, and writing JavaScript code that uses the ONLYOFFICE API to manipulate the document. This guide explains the full process, from opening the macro editor to running and saving your script, along with practical examples and common pitfalls.
What Are ONLYOFFICE Macros?
ONLYOFFICE Docs (the online editors for documents, spreadsheets, and presentations) supports macros written in JavaScript. A macro is a small program that automates repetitive tasks—formatting text, inserting tables, calculating values, or even generating entire reports. Unlike Excel’s VBA, ONLYOFFICE uses JavaScript and its own Document Builder API, so if you know basic JavaScript you can already write useful macros.
Macros run inside the editor’s web interface (desktop and mobile apps also support them). They can access the current document, selection, and many editor functions. The macro system is built into the editor, so no external tools are required—just the Plugins tab and the built-in code editor.
How to Open the Macro Editor
The exact steps depend on your ONLYOFFICE version (Desktop, Cloud, or self-hosted), but the general path is the same:
- Open a document in ONLYOFFICE Docs.
- Go to the Plugins tab on the top toolbar.
- Click Macros (or Macro in some versions). A sidebar or popup appears.
- Click New to create a blank macro, or select an existing one from the list.
- Write or paste your code in the editor area.
- Click Run (the play button) to execute the macro on the current document.
If you don’t see the Plugins tab, your administrator may have disabled plugins. In that case, ask them to enable the Macros plugin in the editor configuration.
Writing Your First Macro: A Simple Example
Let’s create a macro that inserts the current date at the cursor position. In the macro editor, type:
var oDocument = Api.GetDocument(); var oParagraph = Api.CreateParagraph(); oParagraph.AddText(new Date().toLocaleDateString()); oDocument.InsertContent([oParagraph]);
Here’s what each line does:
Api.GetDocument()gets the active document object.Api.CreateParagraph()creates a new empty paragraph.AddText()adds the date string to that paragraph.InsertContent()inserts the paragraph into the document at the current selection.
Run it, and the current date appears where your cursor is. This is the basic pattern: get the document, create content, then insert it.
Understanding the ONLYOFFICE API
The macro system exposes a rich API under the Api global object. You can manipulate documents, spreadsheets, and presentations. For documents, common objects include:
Api.GetDocument()— the whole documentoDocument.GetSelection()— the selected text or rangeApi.CreateParagraph(),Api.CreateTable(),Api.CreateImage()— create elementsoParagraph.AddText(),oParagraph.SetBold(),oParagraph.SetJc("center")— formatting
For spreadsheets, you use Api.GetActiveSheet(), oSheet.GetRange("A1"), oRange.SetValue(), and similar. The API is extensive; the official ONLYOFFICE documentation lists every method with examples. Since the API is versioned, check the docs for your specific build (Desktop vs. server).
How to Save and Reuse Macros
When you create a macro and click Save, it is stored in the editor’s local storage (for Desktop) or in your user profile (for web editors). You can give it a name and later run it from the Macros panel. There is no built-in way to export macros to a file, but you can copy the code to a text file and paste it back later.
For frequently used macros, you can assign them to buttons on the toolbar using the Customize Toolbar option (available in some versions). This lets you run a macro with one click instead of opening the plugin each time.
Common Problems and How to Fix Them
Here are the issues most users run into:
- Macro does nothing — Check the console for errors (press F12 in most browsers). Often the API method name is wrong or the selection is empty.
- “Api is not defined” — This means the macro is running outside the ONLYOFFICE environment (e.g., in a plain browser console). Always run from the Macros plugin.
- Formatting not applied — Some methods require you to get the
ApiDocumentand then calloDocument.GetElement(0)to get the first paragraph. Make sure you’re targeting the correct object. - Macro works in Desktop but not in web — The web version may have a slightly different API version or plugin permissions. Test in both environments.
- Cannot insert image — Image insertion requires a valid path or base64 data. Use
Api.CreateImage("https://example.com/pic.png")or an array of bytes.
If you get a specific error message, search the ONLYOFFICE forum or documentation—most issues are already answered there.
Alternatives to Macros
If macros feel too technical, ONLYOFFICE offers other automation options:
- Plugins — Ready-made plugins (like Translator, Word Count, etc.) that you can install from the Plugin Marketplace.
- Document Builder — A separate tool that lets you generate documents from scripts (Node.js or command line) without opening the editor.
- Forms — For creating fillable forms, you can use the built-in Forms feature, which doesn’t require coding.
- Mail Merge — In spreadsheets, you can use the Mail Merge plugin to generate personalized documents.
These are simpler for basic tasks, but macros give you full control.
Frequently Asked Questions
Can I use VBA code in ONLYOFFICE?
No. ONLYOFFICE macros use JavaScript, not VBA. You can translate simple VBA logic to JavaScript, but the object model is different (e.g., Selection vs. GetSelection()). You’ll need to rewrite the code.
Do macros work in the free desktop version?
Yes, the Desktop Editors (free for personal use) include the Macros plugin. Some advanced features may require a paid license, but basic macro creation and execution are free.
Can I share a macro with a colleague?
There is no built-in sharing, but you can copy the code and send it. The recipient can create a new macro and paste the code. If you’re on a server, you can also store macros in a shared folder that the editor reads.
Is there a way to run a macro automatically when a document opens?
Not directly. The macro system does not support auto-run on document open. You can run it manually or use the Document Builder to process files in batch.
What should I do if the macro editor is missing?
Check if the Plugins tab is hidden. In the web editor, the administrator can enable or disable plugins. In Desktop, go to Settings → Plugins and make sure Macros is toggled on.
Are macros safe to run?
Macros can access your document data and, if written maliciously, could delete content or send data to external servers. Only run macros from trusted sources. The editor does not sandbox macros, so treat them like any executable code.
Related
Formula used
in t | Macro run time (s) | s | 0.5 | 0 | 60 out pace | Macros per hour | macros/h | 3600 / t | 2
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Channel TLD Websites: What They Are & How to Get One5.00 min/km
- Django Class Based Views CRUD REST API
- Ryobi One+ Battery System: Is the Switch Worth It?
- Tmux Terminal Upgrade: Is It Worth It?
- Most Secure OS for Privacy: 3 Picks by Device
- Refurbished Tech Buying Checklist