|
|
||||||
How to Use a Macro to Format Calc CellsFormatting the Text in an OpenOffice.org Cell by Using Macros
This article shows how to use a macro to alter the format of text in OpenOffice.org Calc cells - so that a fully formatted report can be created at the touch of a button.
There is an urban legend that the Eskimos have dozens of words for snow, however it is an actual fact that OpenOffice.org has 19 words for underline alone, a fact that is of particular use for anyone wishing to create macros to automate the formatting of the text in an OpenOffice.org Calc Spreadsheet. In fact, by using a macro, it is possible to format the:
However, a cell must be selected before that cell can be formatted. Selecting Sheets, Rows, Columns and CellsText formatting can applied on four levels in an OpenOffice.org Calc spreadsheet:
The macro must, therefore, select the sheet to be used, for example 'Sheet1' in the current document: sheet = thisComponent.Sheets(0)
The macro can the choose a row, for example row 1: row = sheet.rows(0) ' Row 1 has index number 0
or a column, for example column B: column = sheet.columns(1)
and, finally, a single cell can be selected: cell = sheet.getCellRangeByName("B2")
or: cell = sheet.getCellByReference(1,1)
Changing the Cell FontOnce the sheet, row, column or cell has been selected then the formatting properties can be altered, for example changing the font of the whole sheet to Courier: sheet.CharFontName = "Courier"
or the text of a single cell to Arial: cell.CharFontName = "Arial"
Changing the Cell Font HeightThe font height can also be changed for the whole sheet: sheet.CharHeight = 10
or even a whole column: column.CharHeight = 12
Changing the Font ColorThe font type and size properties are useful, but it may also be useful to highlight a cell by changing the text color: cell.CharColor = RGB (255, 0, 0)
Here the RGB function has been used to change the color of the text to red. Changing the Background color The cell background color can also be change, in this case to yellow: cell.CellBackColor = RGB (255, 255, 0)
Underlining Text in a CellAs mentioned at the start of this article, there are quite a number of underline styles that can be used, each of which has it's own OpenOffice.org constant id number; these are:
The underline can be set in either of two ways:
Interestingly (especially for anyone that noticed that there's a gap in the underline ID numbers) there's even an underline type called Don't know (with its own id number of 4), although this just has the same effect as setting the underline to 'none'. Adding Line WrappingLine wrapping is not turned on by default and so all of the text in a cell will appear on a single line; however, the text may be displayed over multiple lines by using the macro to turn line wrapping on: column.IsTextWrapped = True
ConclusionText formatting can be applied to:
and the formatting consists of setting a number of properties:
With those simple properties it is possible to create a very professional looking report just by using a macro.
The copyright of the article How to Use a Macro to Format Calc Cells in Computer Programming is owned by Mark Alexander Bain. Permission to republish How to Use a Macro to Format Calc Cells in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||