Reputation: 7525
https://wiki.documentfoundation.org/Development/Extension_Development
writes that it is possible to develop Libre Office extensions in JavaScript.
However, the suggested
https://wiki.openoffice.org/wiki/Extensions_development
does not list any JavaScript tutorial for that.
https://www.openoffice.org/framework/scripting/release-0.2/javascript-devguide.html
says that this is experimental.
Is there any serious example of writing a JS extension for Libre Office?
And, a good tutorial on that?
Upvotes: 7
Views: 5330
Reputation: 401
This simple JavaScript code reads the values two cells and write the result to one of them.
// Import standard OpenOffice.org API classes. For more information on
// these classes and the OpenOffice.org API, see the OpenOffice.org
// Developers Guide at:
// http://api.libreoffice.org/
importClass(Packages.com.sun.star.uno.UnoRuntime);
importClass(Packages.com.sun.star.sheet.XSpreadsheetDocument);
importClass(Packages.com.sun.star.container.XIndexAccess);
importClass(Packages.com.sun.star.table.XCellRange);
importClass(Packages.com.sun.star.table.XCell);
// Get the document object from the scripting context
oDoc = XSCRIPTCONTEXT.getDocument();
// Get the XSpreadsheetDocument interface from the document
xSDoc = UnoRuntime.queryInterface(XSpreadsheetDocument, oDoc);
// Get the XIndexAccess interface used to access each sheet
xSheetsIndexAccess = UnoRuntime.queryInterface(XIndexAccess, xSDoc.getSheets());
// Get sheet 1
xSheet = xSheetsIndexAccess.getByIndex(0); // base 0 for sheet 1
// Get the XCellRange interface used to access a cell
xCll = UnoRuntime.queryInterface(XCellRange, xSheet);
// Get interfaces to cells A9 and K9
xCell_A9 = xCll.getCellByPosition(0,8); // base 0 for A9
xVar_A9 = UnoRuntime.queryInterface(XCell, xCell_A9);
xCell_K9 = xCll.getCellByPosition(10,8); // base 0 for K9
xVar_K9 = UnoRuntime.queryInterface(XCell, xCell_K9);
// Get the value of the A9 and K9 cells
varA9 = xVar_A9.getValue();
varK9 = xVar_K9.getValue();
// Write the sum of A9 and K9 to A9
xVar_A9.setValue(varA9 + varK9);
Upvotes: 6
Reputation: 401
Using LibreOffice 6.0, I'm able to create and run a JavaScript macro in a Calc (spreadsheet) document.
Its a goal-seeking macro and there's lots of calculations involved, so its pretty damn slow.
Because I don't trust the built-in editor, especially with lack of "Undo" and distrust over the "Save" functionality, I'm editing the macro in Atom, and copy-paste into LibreOffice's script editor.
Note: I'm most comfortable in JavaScript, of the scripting languages available, so I used that.
Oh, and yes I know I'm not yet providing much useful information, but I hope to do that later.
Upvotes: 1