Document Server version:7.1.1
Hello, I’m using vue2 + onlyoffice to make plugins.
I would like to know how to return data in callCommand
I tried callCommand(func, isClose, isCalc, callback) in the callback returns undefined,
Also try to add the parameter to the callback function is undefined.
I also tried to define an Asc.scope.cellKeyNameData = 1 in callCommand, change the value to 2 in the callCommand function and return Asc.scope.cellKeyNameData to callback, the result is read Asc.scope.cellKeyNameData = 1
I also tried to use localStorage.setItem() in the callCommand and read it on the web page, but the storage address of localStorage.setItem() is the address of onlyofficeDocument, not the address of the page, so the data cannot be interconnected.
I have also used Vuex but it doesn’t, for the same reason as 4
window.Asc.plugin.callCommand(
function () {
let cellKeyNameData = [];
let oDocument = Api.GetDocument();
let oTable = oDocument.GetAllTablesOnPage(0)[0];
console.log("oTable", oTable);
let oTableRow = oTable.GetRow(0);
console.log("oTableRows", oTableRow);
let oTableCell = oTable.GetCell(1, 4).GetContent().GetRange().GetText();
console.log("oTableCell", oTableCell);
let oTableCellIamgeTag = oTable.GetCell(0, 6).GetContent().GetElement(0).GetTag();
for (let i = 0; i < oTable.GetRowsCount(); i++) {
for (let j = 0; j < oTable.GetRow(i).GetCellsCount(); j++) {
let curRow = oTable.GetRow(i)
let curCell = curRow.GetCell(j)
let curCellRange = curCell.GetContent().GetRange();
let curCellRangeText = curCellRange.GetText();
let curCellContent = curCell.GetContent();
if (curCellRangeText != "" && curCellRangeText.trim() !== "") {
cellKeyNameData.push(curCellRangeText);
}
}
}
})
First of all, please note that you are using outdated version of Document Server. I’d recommend updating to recently released version 8.2.
In provided example you didn’t specify the callback as per description of the callCommand method. The last parameter callback is used to define function that is return after execution. For example:
Thanks, I forgot to mention my needs.
I want callCommand to return cellKeyNameData
I have tried your method and the result is cellKeyNameData is not defined
I’m not sure if I understand your goal. In your example cellKeyNameData is an empty array. What are you trying to achieve? Do you want to get text values from each non-empty cell in the table?
I defined a function export function getTableData() in a JS file and used window in this function Asc.plugin.callCommand
Then in the window The Asc.plugin.callCommand defines let cellKeyNameData = ;
Then obtained the text content of all non empty cells and pushed it to cellKeyNameData
cellKeyNameData.push(curCellRangeText);
The results are as follows:
let cellKeyNameData = [Name ‘,’ Gender ‘,’ Date of Birth ‘,’ Current Position ‘,’ Rank (Position) Time ‘,’ Resume Information ‘,’ Family Members’, ‘Title’, ‘Name’, ‘Age’, ‘Work Unit’, ‘Barcode’]
I hope to return the data of cellKeyNameData to an external function
If you still don’t understand, please take a look at the example. I hope to return the change of Asc.scope.data in Call Command to the outside. That is to say, I hope to change Asc.scope.data to 2
export function getData(){
let data = 1
Asc.scope.data = data
let testEvent = window.Asc.plugin.callCommand(function () {
console.log("Asc.scope.data",Asc.scope.data);
// Asc.scope.data = 1
Asc.scope.data = 2;
console.log("Asc.scope.data",Asc.scope.data);
// Asc.scope.data = 2
return Asc.scope.data
},false,false,function(){
console.log("Callback Asc.scope.data",Asc.scope.data);
// Asc.scope.data = 1
})
console.log("Asc.scope.data",Asc.scope.data);
// Asc.scope.data = 1
return Asc.scope.data
// return 1
}
I see. Callback for callCommand supports all JS types, so, for instance, you can transfer result like:
window.Asc.plugin.callCommand(
function() {
let cellKeyNameData = [];
let oDocument = Api.GetDocument();
let oTable = oDocument.GetAllTablesOnPage(0)[0];
let oTableRow = oTable.GetRow(0);
let oTableCell = oTable.GetCell(1, 4).GetContent().GetRange().GetText();
// let oTableCellIamgeTag = oTable.GetCell(0, 6).GetContent().GetElement(0).GetTag();
// This one returns an error, you should revise it.
for (let i = 0; i < oTable.GetRowsCount(); i++) {
for (let j = 0; j < oTable.GetRow(i).GetCellsCount(); j++) {
let curRow = oTable.GetRow(i)
let curCell = curRow.GetCell(j)
let curCellRange = curCell.GetContent().GetRange();
let curCellRangeText = curCellRange.GetText();
let curCellContent = curCell.GetContent();
if (curCellRangeText != "" && curCellRangeText.trim() !== "") {
cellKeyNameData.push(curCellRangeText);
}
}
}
return { data: cellKeyNameData };
},
function(result) {
console.log('Received result:', result.data);
})
That way your filled array is transferred into second function. It is up to you how to manage the data further.
After trying to copy your code, I found that console.log ('Received result: ', result. data) was not executed. The console also did not report any errors
As for me, it takes a long time to understand the way to get data from document to plugin.
So, there are 4 arguments of callCommand method: func, isClosed, isCalc and Callback.
func - the function, executed at the document context. You should send data to this function by using the Asc.scope object. And if you want to get data from this function, you have to RETURN data! This is important! And you may return only the Number, String or Array. If You want to return the Object, you should use JSON.stringify(…)!
Callback - the function, executing after returning process control from document back to plugin. And this callback can get argument, tht is your data, returned from document!
Example (for worksheet):
function getData () {
let data = 1;
Asc.scope.data = data;
window.Asc.plugin.callCommand( function () {
const sh = Api.GetActivesheet();
const rng = sh.GetRange("A2");
rng.SetValue(Asc.scope.data);
const resRng = sh.GetRange("B4");
const result = resRng.GetValue();
//return your data!!
return JSON.stringify({data: result});
},
//isClose:
false,
//isCalc
true,
//Callback function:
function (res) {
//argument res is returned data!
console.log(`result from worksheet: ${JSON.parse(res)}`);
});
}
I am not quite following the design. Are you trying to get a page in the spreadsheet editor? In general, you code returns Api.GetCurrentVisiblePage is not a function - can you share a link to this method for the reference? Also, used GetIndex method returns index of the sheet in the file.
Using the above code in the variable “currentPage” I am able to get the current sheet number for Excel and current viewing page for other documents and presentations.
The problem I am facing is, I am unable to use the “currentPage” value outside callCommand function.
First of all, you can use only one option to return a callback. I recommend using onCommandCallback method. For example, this returns the value:
window.Asc.plugin.callCommand(
function () {
var currentPage = Asc.scope.editorType === 'cell'
? Api.GetActiveSheet().GetIndex()
: Api.GetCurrentVisiblePage();
console.log('current page Audit page', currentPage); // getting currentPage value
return currentPage;
});
window.Asc.plugin.onCommandCallback = function (currentPage) {
console.log('Received onCommandCallback audit result:', currentPage); // returns the same value
};
You can further execute your code in onCommandCallback.
Second, as I mentioned, GetIndex returns index of the sheet, instead of something like a page number. Not sure if there is a workaround to this as canvas in the spreadsheet editor is basically one.