ONLYOFFICE / DocumentServer

ONLYOFFICE Docs is a free collaborative online office suite comprising viewers and editors for texts, spreadsheets and presentations, forms and PDF, fully compatible with Office Open XML formats: .docx, .xlsx, .pptx and enabling collaborative editing in real time.
https://www.onlyoffice.com
GNU Affero General Public License v3.0
4.87k stars 1.09k forks source link

How to use GlobalVariable for macros code? #2202

Closed wwjje closed 6 months ago

wwjje commented 1 year ago

js代码怎么执行宏代码,或者调用宏呢?还有GlobalVariable怎么获取呢?提示GlobalVariable is not defined

askonev commented 1 year ago

@wwjje The macros plugin is a wrapper over the docbuilder product and uses almost the entire docbuilder api. But there are methods which are only available for desktop docbuilder

The GlobalVariable mechanism is designed to exchange data in the context of ONLYOFFICE Document Builder. It is not available in the plugin macros

The macros plugin is a wrapper over the docbuilder product and uses almost the entire docbuilder api. But there are methods which are only available for desktop docbuilder

The GlobalVariable mechanism is designed to exchange data in the context of ONLYOFFICE Document Builder. It is not available in the macros plugin

You can use this mechanism to write .builder scripts for exchanging data between assembled documents.

The builder object is also only available in the context of the .builder script

builder.OpenFile("https://example.com/DocumentWithComments.docx");
var oDocument = Api.GetDocument();
GlobalVariable["CommentsReport"] = oDocument.GetCommentsReport();
builder.CloseFile();

builder.CreateFile("docx");
var oCommentsReport = GlobalVariable["CommentsReport"];
... // now you can use the 'CommentsReport' variable from the previous document in the current document;
builder.SaveFile("docx", "GetCommentsReport.docx");
builder.CloseFile();
wwjje commented 1 year ago

我的意思是,我的js代码应该怎样去执行宏代码,或者调用宏呢?我直接编写以下代码,会报错Api is not defined,这些代码只能在Plugins---Macros去执行吗?我可以在index.js编写执行吗?function getContent() { var oDocument = Api.GetDocument(); var aDocElements = oDocument.GetContent(true); return aDocElements; } image

wwjje commented 1 year ago

@askonev

askonev commented 1 year ago

@wwjje

The Automation API mechanism is provided for js execution. (This is a paid feature. Contact sales@onlyoffice.com about purchasing)

At the time of initializing the docserver integration object, we create a connector object that allows you to use plugin api methods.

Example of integration implementation

Documentation for plugin api methods

Warn But also there is a technical limitation: getting data from macros context into plugin context is not realized at the moment.

askonev commented 1 year ago

There is also a request to continue communicating in English in order to use your answers as a basis for future answers.

wwjje commented 1 year ago

Well, thank you

wwjje commented 1 year ago

How should I implement data exchange between two files, copy and paste? GlobalVariables cannot be used, nor can they directly use variables defined by JS. var connector = docEditor.createConnector(); var obj = null; connector.callCommand( () => { obj = oDocument.GetCommentsReport(); GlobalVariable["CommentsReport"] = oDocument.GetCommentsReport(); }, function () { console.log("callback command"); } ); obj is not defined GlobalVariable is not defined

wwjje commented 1 year ago

@askonev

askonev commented 1 year ago

The GlobalVariable mechanism is designed to be used in the document builder library, either installed on the desktop or integrated into the server. Example of access via http to document builder service

The connector using the callCommand() method executes js instructions not in the context of the document builder integrated on the server, but in some shell with which the macros plugin communicates.

In other words, some of the methods for the *.builder script are not available.

In particular these are:

  1. Methods from the builder.* example: builder.CreateFile("docx");
  2. the Global Variable mechanism
  3. mechanism for comparing documents

There is a GetSelectedText() method to retrieve formatted data via api plugin

Also, after initializing new DocsAPI.DocEditor(), you open a single document context. In other words, there is no full-fledged exchange between the two documents.

Maybe the solution to your problem is to write a docbuilder script which opens the file at the url, gets the data, and then passes it via GV in the context of the next file

example.builder

builder.OpenFile("http://nginx/file/current_file.xlsx");
    var oWorksheet = Api.GetActiveSheet();
    var oRange = oWorksheet.GetRange("A1:C4");
    var text = oRange.GetText();
GlobalVariable["DATA"] = text
builder.CloseFile();

builder.CreateFile("docx");
    var text = GlobalVariable["DATA"];
    var oDocument = Api.GetDocument();
    var oParagraph = oDocument.GetElement(0);
        oParagraph.AddText("Document" + text);
builder.SaveFile("docx", "document.docx");
builder.CloseFile();

And then form an http request to document builder service with url to the file with the script

{
    "async": true,
    "url": "https://example.com/example.builder"
}
wwjje commented 1 year ago

image

Click the button to copy all the contents of the file on the right and paste it to the cursor of the file on the left,How should it be implemented?

This is a partial code block that I have implemented

insertElement() { var projectEditorConnector = this.projectEditor.createConnector(); var elementEditorConnector = this.elementEditor.createConnector(); elementEditorConnector.callCommand( function () { sessionStorage.removeItem("elementTemp"); var oDocument = Api.GetDocument(); sessionStorage.setItem( "elementTemp", oDocument.ToJSON(true, true, true, true, true, true) ); }, function () { console.log("callback command"); } ); projectEditorConnector.callCommand( function () { var oDocument = Api.GetDocument(); var json = Api.FromJSON(sessionStorage.getItem("elementTemp")); oDocument.InsertContent(json); }, function () { console.log("callback command"); } ); }

I have reviewed the document introducing the global variable GlobalVariable, but I may report an error stating that GlobalVariable is not defined, so I used sessionStorage. Is there a better solution to implement it?

Even so, if I configure config. type=“embedded”, the above method cannot be implemented, and callCommand will not execute

wwjje commented 1 year ago

@askonev

askonev commented 1 year ago

@wwjje Greetings, I think this scenario is for a separate issue. If I have helped you with it then close this one. And open a new one. Thank you.

wwjje commented 1 year ago

@askonev Do you mean I need to open a new issue?

askonev commented 12 months ago

@askonev Do you mean I need to open a new issue?

Yes, please

Rita-Bubnova commented 6 months ago

I close this issue. Feel free to comment or reopen it if you got further questions.