Closed wwjje closed 6 months 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();
我的意思是,我的js代码应该怎样去执行宏代码,或者调用宏呢?我直接编写以下代码,会报错Api is not defined,这些代码只能在Plugins---Macros去执行吗?我可以在index.js编写执行吗?function getContent() { var oDocument = Api.GetDocument(); var aDocElements = oDocument.GetContent(true); return aDocElements; }
@askonev
@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.
There is also a request to continue communicating in English in order to use your answers as a basis for future answers.
Well, thank you
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
@askonev
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:
builder.CreateFile("docx");
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"
}
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
@askonev
@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.
@askonev Do you mean I need to open a new issue?
@askonev Do you mean I need to open a new issue?
Yes, please
I close this issue. Feel free to comment or reopen it if you got further questions.
js代码怎么执行宏代码,或者调用宏呢?还有GlobalVariable怎么获取呢?提示GlobalVariable is not defined