https://bugs.documentfoundation.org/show_bug.cgi?id=167585
Bug ID: 167585
Summary: Macros create cell style names, but formatting is not
applied
Product: LibreOffice
Version: 7.3.7.2 release
Hardware: All
OS: All
Status: UNCONFIRMED
Severity: normal
Priority: medium
Component: Calc
Assignee: [email protected]
Reporter: [email protected]
When assigning a built-in cell style like "Accent 3" to a cell using
`oCell.CellStyle = "Accent 3"`, the formatting is applied immediately and is
visible as expected.
In contrast, when a macro is used to create a custom cell style (e.g. by
setting `.CellBackColor`), the style is created and appears in the style list,
but it is not properly formatted. As a result, applying that style to a cell
has no visual effect — the formatting is missing even though the macro runs
without error and the style is clearly assigned to the cell.
If the same formatting is applied directly to a cell (e.g. via
`oCell.CellBackColor = RGB(...)`), the result is immediate and correct. This
suggests that style creation via macro is not properly initializing the
formatting properties, unlike direct formatting or use of built-in styles,
which both work reliably.
This report includes three macros demonstrating the issue:
### ✅ Macro That Applies a Built-in Style (Working)
Applies the built-in "Accent 3" style and sets cell content. Formatting appears
as expected.
8<------------8<---------------8<
Sub ApplyAccent3Style
Dim oDoc As Object
Dim oSheet As Object
Dim oCell As Object
Dim sStyleName As String
oDoc = ThisComponent
sStyleName = "Accent 3"
' Apply style "Accent 3" to cell A1 in the first sheet and write "Test"
oSheet = oDoc.getSheets().getByIndex(0)
oCell = oSheet.getCellRangeByName("A1")
oCell.CellStyle = sStyleName
oCell.setString("Test")
End Sub
8<------------8<---------------8<
### ❌ Macro That Creates and Applies a Custom Style (Fails)
The style is created and assigned by the macro, but it is not formatted and has
no visible effect when applied.
8<------------8<---------------8<
Sub CreateAndApplyCustomStyle
Dim oDoc As Object
Dim oStyles As Object
Dim oStyle As Object
Dim oSheet As Object
Dim oCell As Object
Dim sStyleName As String
oDoc = ThisComponent
oStyles = oDoc.StyleFamilies.getByName("CellStyles")
sStyleName = "CustomStyle_b3cac7"
' Only create the style if it does not exist
If Not oStyles.hasByName(sStyleName) Then
oStyle = oDoc.createInstance("com.sun.star.style.CellStyle")
With oStyle
' Set background color to #b3cac7 (R=179, G=202, B=199)
.CellBackColor = RGB(179, 202, 199)
End With
oStyles.insertByName(sStyleName, oStyle)
End If
' Apply style to A1 in the first sheet and write "Test"
oSheet = oDoc.getSheets().getByIndex(0)
oCell = oSheet.getCellRangeByName("A1")
oCell.CellStyle = sStyleName
oCell.setString("Test")
End Sub
8<------------8<---------------8<
### ✅ Macro That Directly Sets Background Color (Working)
Direct cell formatting without using styles works correctly.
8<------------8<---------------8<
Sub SetA1BackColorAndText
Dim oDoc As Object
Dim oSheet As Object
Dim oCell As Object
oDoc = ThisComponent
oSheet = oDoc.getSheets().getByIndex(0)
oCell = oSheet.getCellRangeByName("A1")
oCell.CellBackColor = RGB(179, 202, 199)
oCell.setString("Test")
End Sub
8<------------8<---------------8<
--
You are receiving this mail because:
You are the assignee for the bug.