Writing the plugin code

The main plugin code is placed to a .js file which describes what and how must be done by the plugin. This file is placed to the plugin root folder together with the config.json and index.html files.

How it works?

Any plugin has window.Asc.plugin object which in its turn has several methods for it to interact with ONLYOFFICE document, spreadsheet and presentation editors.

For the plugin to work the developer must specify two obligatory methods for the window.Asc.plugin object: window.Asc.plugin.init and window.Asc.plugin.button. After that the window.Asc.plugin.executeCommand method is used to send data to the editors using the in-built ONLYOFFICE Document Builder API features.

Let's see how this is done in the helloworld.js plugin:

(function (window, undefined) {
    window.Asc.plugin.init = function () {
        var sScript = 'var oDocument = Api.GetDocument();';
        sScript += 'oDocument.CreateNewHistoryPoint();';
        sScript += 'oParagraph = Api.CreateParagraph();';
        sScript += 'oParagraph.AddText(\'Hello world!\');';
        sScript += 'oDocument.InsertContent([oParagraph]);';
        window.Asc.plugin.info.recalculate = true;
        this.executeCommand("close", sScript);
    };
    window.Asc.plugin.button = function (id) {
    };
})(window, undefined);

When the plugin object is being initialized (window.Asc.plugin.init = function (text) {...}), the editor forms a paragraph with the 'Hello World' phrase and then uses ONLYOFFICE Document Builder API to create the document with this text in it (with the help of the window.Asc.plugin.executeCommand method - this.executeCommand("close", sScript);).

Please note, that all ONLYOFFICE Document Builder features are already included into all the versions (including the open source one) of ONLYOFFICE document editors, this is how the plugins interact with the editors. The standalone ONLYOFFICE Document Builder version is required, however, in case you want to create documents without any document editors.

The only 'OK' button (window.Asc.plugin.button = function (id) {...}) is used to create the text and finish the work with the plugin.

This way any other plugin can be created. Currently the plugins support not only sending text or formatted text to the editors, but also OLE objects embedding (such examples are available here: chess - the chess game plugin, and youtube - embedding the YouTube video into an editor page).

More existing open source plugin examples can be found here: https://github.com/ONLYOFFICE/sdkjs-plugins.