Getting started with macros

In ONLYOFFICE editors macros are small scripts used to facilitate your day-to-day routine work with different types of documents. Macros use JavaScript language syntax and ONLYOFFICE Document Builder API scripting notation, thus the methods available in JavaScript and all the methods supported by ONLYOFFICE Document Builder are also supported by macros.

You can add your own macros to documents (all document types are supported: text, spreadsheet, presentation), edit, save them with the document - anything to make your work with document files easier and more convenient.

Let’s see how to write the simplest Hello world! macro, which inserts the Hello World! phrase into the document.

  1. Open or create a document of the necessary type, click the Macros button on the Plugins tab. The macros window will be opened. Click New to create a new macro.

  2. Explore the ONLYOFFICE Document Builder API documentation first to see which methods you need to complete the task.

    The following methods will be used for the Hello world! macro:

  3. Write code for your macro. It will look like this:

    (function(){
        oDocument = Api.GetDocument();
        oParagraph = Api.CreateParagraph();
        oParagraph.AddText("Hello world!");
        oDocument.InsertContent([oParagraph]);
    })();
    
  4. That’s it! When ready, click Run to run your code onto the document and make sure it works as you expected. If not, try to find and fix a bug and run it again. The result will look the following way:

    Macros window

You can also rename your macro to distinguish it from the other ones in case they are used in the same document, or delete the unnecessary ones.

Unlike plugins, macros are attached to a specific document and there is no possibility to make them global.