Hi. I’d like to change a cell’s background colour using a JavaScript macro in Calc. How would I do this? Looking through the API, I surmised that I needed to use the setPropertyValue
method to change the CellBackColor
property of the cell, but I’m not sure what the proper usage syntax for it should be.
This is what I’ve tried:
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);
importClass(Packages.com.sun.star.beans.XPropertySet);
importClass(java.lang.Long);
importClass(Packages.com.sun.star.util.Color);
var cell = UnoRuntime.queryInterface(
XCellRange,
UnoRuntime.queryInterface(
XIndexAccess,
UnoRuntime.queryInterface(
XSpreadsheetDocument,
XSCRIPTCONTEXT.getDocument()
).getSheets()
).getByIndex(0)
).getCellByPosition(0, 0);
UnoRuntime.queryInterface(
XPropertySet,
UnoRuntime.queryInterface(XCell, cell)
).setPropertyValue("CellBackColor", new Color(new Long(16776960)));
I was hoping that running that would change the colour of the cell in sheet 0, column 0, row 0 to the colour that 16776960 corresponds to (which afaik should be yellow), but it doesn’t work, presumably because I’m using the setPropertyValue
method incorrectly. As such, I’d like to know, what’s the proper way to do this and what inputs does the setPropertyValue
method actually take?
Fwiw, I know that 16776960 corresponds to yellow from running the following code after having manually set the colour of the cell to yellow:
// Imports are the same as above, just removed here for conciseness..
cell.setFormula(
"".concat(
UnoRuntime.queryInterface(
XPropertySet,
UnoRuntime.queryInterface(XCell, cell)
).getPropertyValue("CellBackColor")
)
);