BeanShell scripting in LibreOffice
Here is a BeanShell script to xploit functionalities from LibreOffice spreadsheeat:
import com.sun.star.uno.UnoRuntime;If you run this code you will se the following:
import com.sun.star.sheet.XSpreadsheetView;
import com.sun.star.text.XText;
import com.sun.star.sheet.XCellRangeData;
//
// Get active spreadsheet
//
model = XSCRIPTCONTEXT.getDocument();
controller = model.getCurrentController();
view = UnoRuntime.queryInterface(XSpreadsheetView.class, controller);
sheet = view.getActiveSheet();
//
// Create a TEXT CELL
//
// Get cell by position
cell = sheet.getCellByPosition(0, 0);
// Access the cell text
cellText = UnoRuntime.queryInterface(XText.class, cell);
// Get the text cursor of the cell text
textCursor = cellText.createTextCursor();
// Insert a string in the cell through the text cursor and overwrite the cell content
// Using 'false' as third parameter adds the inserted string
cellText.insertString(textCursor, "BeanShell macro example", true);
//
// Access and modify VALUE CELLS
//
// Get cell by position
cell = sheet.getCellByPosition(0, 2);
// Set cell value
cell.setValue(1);
// Get cell value and multiply the value by 2
double nDblValue = cell.getValue() * 2;
// Set cell value with result
sheet.getCellByPosition(0, 3).setValue(nDblValue);
//
// Create FORMULA CELLS
//
// Get cell by position
cell = sheet.getCellByPosition(0, 4);
// Set formula string
cell.setFormula("=SUM(A3:A4)");
//
// Fill CELL RANGE
//
// Get cell range by name
cellRange = sheet.getCellRangeByName("B3:C4");
data = UnoRuntime.queryInterface(XCellRangeData.class, cellRange);
Object[][] values =
{
{new Double(1.1), new Integer(666)},
{new Double(2.2), new String("The end")}
};
data.setDataArray(values);
// BeanShell OpenOffice.org scripts should always return 0
return 0;
Comments
Post a Comment