Difference between revisions of "Working with styles"

From Apache OpenOffice Wiki
Jump to: navigation, search
(One intermediate revision by one other user 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:  
  
<code>[oobas]
+
<source 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
</code>
+
</source>
 
<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:  
  
<code>[oobas]
+
<source 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
</code>
+
</source>
  
  
Line 33: Line 33:
 
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:  
  
<code>[oobas]
+
<source lang="oobas">
 
function fnGetNumberFormatId(oDoc, sNumberFormat)
 
function fnGetNumberFormatId(oDoc, sNumberFormat)
 
sCharLocale = oDoc.getPropertyValue("CharLocale")
 
sCharLocale = oDoc.getPropertyValue("CharLocale")
Line 42: Line 42:
 
fnGetNumberFormatId = nFormatId
 
fnGetNumberFormatId = nFormatId
 
end function
 
end function
</code>
+
</source>
 
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:  
  
<code>[oobas]
+
<source 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)
</code>
+
</source>
  
  
Line 56: Line 56:
 
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.  
  
<code>[oobas]
+
<source 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 67:
 
fnGetStyle = oStyle
 
fnGetStyle = oStyle
 
end function
 
end function
</code>
+
</source>
 
(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:  
  
<code>[oobas]
+
<source 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 78:
 
oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10")
 
oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10")
 
oCells.setPropertyValue("CellStyle", sStyle)
 
oCells.setPropertyValue("CellStyle", sStyle)
</code>
+
</source>
  
  
Line 85: Line 85:
 
To find an existing style that has the properties that you want:  
 
To find an existing style that has the properties that you want:  
  
<code>[oobas]
+
<source lang="oobas">
 
function fnFindStyle(oDoc, sStyleFamily, mProperties)
 
function fnFindStyle(oDoc, sStyleFamily, mProperties)
 
oStyleFamilies = oDoc.StyleFamilies
 
oStyleFamilies = oDoc.StyleFamilies
Line 105: Line 105:
 
next
 
next
 
end function
 
end function
</code>
+
</source>
 
An example of calling this function:  
 
An example of calling this function:  
  
<code>[oobas]
+
<source lang="oobas">
 
dim mProperties()
 
dim mProperties()
 
subAddProperty(mProperties(), "NumberFormat", fnGetNumberFormatId(thisComponent, "0.0000"))
 
subAddProperty(mProperties(), "NumberFormat", fnGetNumberFormatId(thisComponent, "0.0000"))
Line 121: Line 121:
 
oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10")
 
oCells = thisComponent.getSheets.getByIndex(0).getCellRangeByName("B2:B10")
 
oCells.setPropertyValue("CellStyle", sStyle)
 
oCells.setPropertyValue("CellStyle", sStyle)
</code>
+
</source>
 
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]]
[[Category:Tutorials]]
 

Revision as of 17:52, 10 July 2008

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.

Personal tools