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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With