Difference between revisions of "Working with styles"
m |
|||
(3 intermediate revisions by 3 users not shown) | |||
Line 2: | Line 2: | ||
'''Display style names''' and '''style names''' are sometimes different. Thus to get a list of 'display' style names: | '''Display style names''' and '''style names''' are sometimes different. Thus to get a list of 'display' style names: | ||
− | < | + | <syntaxhighlight lang="oobas"> |
function fnGetDisplayStyleNames(oDoc, sStyleFamily as string) | function fnGetDisplayStyleNames(oDoc, sStyleFamily as string) | ||
dim mApiNames, nStyles as integer, i as integer, oStyles as object | dim mApiNames, nStyles as integer, i as integer, oStyles as object | ||
Line 16: | Line 16: | ||
fnGetDisplayStyleNames = mStyleNames() | fnGetDisplayStyleNames = mStyleNames() | ||
end function | end function | ||
− | </ | + | </syntaxhighlight> |
− | <tt>subShellSort</tt> is defined in [[ sorting and searching]]. | + | <tt>subShellSort</tt> is defined in [[sorting and searching]]. |
An example of calling this function: | An example of calling this function: | ||
− | < | + | <syntaxhighlight lang="oobas"> |
mStyleFamilyNames = thisComponent.getStyleFamilies.getElementNames | mStyleFamilyNames = thisComponent.getStyleFamilies.getElementNames | ||
for i = 0 to uBound(mStyleFamilyNames) | for i = 0 to uBound(mStyleFamilyNames) | ||
msgbox join(fnGetDisplayStyleNames(thisComponent, mStyleFamilyNames(i)), chr(13)), 0, mStyleFamilyNames(i) | msgbox join(fnGetDisplayStyleNames(thisComponent, mStyleFamilyNames(i)), chr(13)), 0, mStyleFamilyNames(i) | ||
next | next | ||
− | </ | + | </syntaxhighlight> |
− | + | ||
Line 33: | Line 32: | ||
Number formats have an Id number. Here is a function for getting that number: | Number formats have an Id number. Here is a function for getting that number: | ||
− | < | + | <syntaxhighlight lang="oobas"> |
function fnGetNumberFormatId(oDoc, sNumberFormat) | function fnGetNumberFormatId(oDoc, sNumberFormat) | ||
sCharLocale = oDoc.getPropertyValue("CharLocale") | sCharLocale = oDoc.getPropertyValue("CharLocale") | ||
Line 42: | Line 41: | ||
fnGetNumberFormatId = nFormatId | fnGetNumberFormatId = nFormatId | ||
end function | end function | ||
− | </ | + | </syntaxhighlight> |
An example for directly applying a number format to a range of cells in a spreadsheet: | An example for directly applying a number format to a range of cells in a spreadsheet: | ||
− | < | + | <syntaxhighlight lang="oobas"> |
oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10") | oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10") | ||
nFourDP = fnGetNumberFormatId(thisComponent, "0.0000") | nFourDP = fnGetNumberFormatId(thisComponent, "0.0000") | ||
oCells.setPropertyValue("NumberFormat", nFourDp) | oCells.setPropertyValue("NumberFormat", nFourDp) | ||
− | </ | + | </syntaxhighlight> |
− | + | ||
Line 56: | Line 54: | ||
The following function is for returning a style of a particular name, if the name doesn't already exist a style is created with that name. | The following function is for returning a style of a particular name, if the name doesn't already exist a style is created with that name. | ||
− | < | + | <syntaxhighlight lang="oobas"> |
function fnGetStyle(oDoc, sStyleFamily, sInstance, sStyleName) | function fnGetStyle(oDoc, sStyleFamily, sInstance, sStyleName) | ||
oStyles = thisComponent.getStyleFamilies.getByName(sStyleFamily) | oStyles = thisComponent.getStyleFamilies.getByName(sStyleFamily) | ||
Line 67: | Line 65: | ||
fnGetStyle = oStyle | fnGetStyle = oStyle | ||
end function | end function | ||
− | </ | + | </syntaxhighlight> |
− | (It would be good if the sInstance parameter could be removed but I haven't worked out how to do that. If you work it out please edit this page!) | + | (It would be good if the sInstance parameter could be removed, but I haven't worked out how to do that. If you work it out please edit this page!) |
An example of calling the above function, it creates a cell style called FourDP if it doesn't already exist, sets its number format, and applys the cell format to a range of cells: | An example of calling the above function, it creates a cell style called FourDP if it doesn't already exist, sets its number format, and applys the cell format to a range of cells: | ||
− | < | + | <syntaxhighlight lang="oobas"> |
sStyle = "FourDP" | sStyle = "FourDP" | ||
oStyle = fnGetStyle(thisComponent, "CellStyles", "com.sun.star.style.CellStyle", sStyle) | oStyle = fnGetStyle(thisComponent, "CellStyles", "com.sun.star.style.CellStyle", sStyle) | ||
Line 78: | Line 76: | ||
oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10") | oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10") | ||
oCells.setPropertyValue("CellStyle", sStyle) | oCells.setPropertyValue("CellStyle", sStyle) | ||
− | </ | + | </syntaxhighlight> |
− | + | ||
Line 85: | Line 82: | ||
To find an existing style that has the properties that you want: | To find an existing style that has the properties that you want: | ||
− | < | + | <syntaxhighlight lang="oobas"> |
function fnFindStyle(oDoc, sStyleFamily, mProperties) | function fnFindStyle(oDoc, sStyleFamily, mProperties) | ||
oStyleFamilies = oDoc.StyleFamilies | oStyleFamilies = oDoc.StyleFamilies | ||
Line 105: | Line 102: | ||
next | next | ||
end function | end function | ||
− | </ | + | </syntaxhighlight> |
An example of calling this function: | An example of calling this function: | ||
− | < | + | <syntaxhighlight lang="oobas"> |
dim mProperties() | dim mProperties() | ||
subAddProperty(mProperties(), "NumberFormat", fnGetNumberFormatId(thisComponent, "0.0000")) | subAddProperty(mProperties(), "NumberFormat", fnGetNumberFormatId(thisComponent, "0.0000")) | ||
Line 121: | Line 118: | ||
oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10") | oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10") | ||
oCells.setPropertyValue("CellStyle", sStyle) | oCells.setPropertyValue("CellStyle", sStyle) | ||
− | </ | + | </syntaxhighlight> |
This example calls <tt>subAddProperty</tt> which is defined in [[Working_with_properties#subAddProperty]]. | This example calls <tt>subAddProperty</tt> which is defined in [[Working_with_properties#subAddProperty]]. | ||
[[Category:Basic:Tutorials]] | [[Category:Basic:Tutorials]] | ||
− |
Latest revision as of 14:54, 24 August 2022
Display Style Names
Display style names and style names are sometimes different. Thus to get a list of 'display' style names:
function fnGetDisplayStyleNames(oDoc, sStyleFamily as string) dim mApiNames, nStyles as integer, i as integer, oStyles as object oStyles = oDoc.getStyleFamilies.getByName(sStyleFamily) mApiNames = oStyles.getElementNames nStyles = uBound(mApiNames) dim mStyleNames(nStyles) for i = 0 to nStyles mStyleNames(i) = oStyles.getByIndex(i).getPropertyValue("DisplayName") next subShellSort(mStyleNames()) fnGetDisplayStyleNames = mStyleNames() end function
subShellSort is defined in sorting and searching.
An example of calling this function:
mStyleFamilyNames = thisComponent.getStyleFamilies.getElementNames for i = 0 to uBound(mStyleFamilyNames) msgbox join(fnGetDisplayStyleNames(thisComponent, mStyleFamilyNames(i)), chr(13)), 0, mStyleFamilyNames(i) next
Number formats
Number formats have an Id number. Here is a function for getting that number:
function fnGetNumberFormatId(oDoc, sNumberFormat) sCharLocale = oDoc.getPropertyValue("CharLocale") nFormatId = oDoc.getNumberFormats.queryKey(sNumberFormat, sCharLocale, false) if nFormatId = -1 then 'Not yet defined nFormatId = oDoc.getNumberFormats.addNew(sNumberFormat, sCharLocale) end if fnGetNumberFormatId = nFormatId end function
An example for directly applying a number format to a range of cells in a spreadsheet:
oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10") nFourDP = fnGetNumberFormatId(thisComponent, "0.0000") oCells.setPropertyValue("NumberFormat", nFourDp)
Get Style
The following function is for returning a style of a particular name, if the name doesn't already exist a style is created with that name.
function fnGetStyle(oDoc, sStyleFamily, sInstance, sStyleName) oStyles = thisComponent.getStyleFamilies.getByName(sStyleFamily) if not oStyles.hasByName(sStyleName) then oStyle = thisComponent.createInstance(sInstance) oStyles.insertByName(sStyleName, oStyle) else oStyle = oStyles.getByName(sStyleName) end if fnGetStyle = oStyle end function
(It would be good if the sInstance parameter could be removed, but I haven't worked out how to do that. If you work it out please edit this page!)
An example of calling the above function, it creates a cell style called FourDP if it doesn't already exist, sets its number format, and applys the cell format to a range of cells:
sStyle = "FourDP" oStyle = fnGetStyle(thisComponent, "CellStyles", "com.sun.star.style.CellStyle", sStyle) oStyle.setPropertyValue("NumberFormat", fnGetNumberFormatId(thisComponent, "0.0000") oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10") oCells.setPropertyValue("CellStyle", sStyle)
Find Style
To find an existing style that has the properties that you want:
function fnFindStyle(oDoc, sStyleFamily, mProperties) oStyleFamilies = oDoc.StyleFamilies oStyles = oStyleFamilies.getByName(sStyleFamily) nProperties = ubound(mProperties) For i = 0 To oStyles.Count - 1 oStyle = oStyles(i) bFound = true for j = 0 to nProperties if oStyle.getPropertyValue(mProperties(j).name) <> mProperties(j).value then bFound = false exit for end if next if bFound then fnFindStyle = oStyle exit function end if next end function
An example of calling this function:
dim mProperties() subAddProperty(mProperties(), "NumberFormat", fnGetNumberFormatId(thisComponent, "0.0000")) oStyle = fnFindStyle(thisComponent, "CellStyles", mProperties()) if isNull(oStyle) then sStyle = "FourDP" oStyle = fnGetStyle(thisComponent, "CellStyles", "com.sun.star.style.CellStyle", sStyle) oStyle.setPropertyValues(mProperties()) else sStyle = oStyle.getName end if oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10") oCells.setPropertyValue("CellStyle", sStyle)
This example calls subAddProperty which is defined in Working_with_properties#subAddProperty.