You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openoffice.apache.org by "Rony G. Flatscher" <Ro...@wu.ac.at> on 2022/08/04 12:12:37 UTC
Portable version (Re: OLE: scalc (creating data, display it in a chart)
This is the second of a total of four postings with the intention to demonstrate how to realize the
same functionality of the posted OLE samples without OLE and in a portable way (running unchanged on
Windows, Linux and Apple).
These are samples in the ooRexx scripting language, which usually can be easily adapted to other
languages by replacing the tilde (~), the ooRexx message operator, with a dot (.).
Also, these solutions will use queryInterface() such that one can see for other programming
languages that need to employ queryInterface() what the interface names are. The ooRexx solution
(actually the ooRexx-Java bridge BSF4ooRexx) takes advantage of the available message paradigm and
allows one to merely send the (unqualified) interface name to an UNO object (instead of coding the
entire queryInterface() statement). The fully qualified interface name can always be looked up
quickly from the AOO index for the letter "X":
<https://www.openoffice.org/api/docs/common/ref/index-files/index-24.html>.
Here the portable, OLE-less solution as a follow-up to the matching posting (see underneath):
/**********************************************************************
scalc_chart.rxo: using UNO.CLS (i.e. Java UNO under the hood) with ooRexx
Links:<https://OpenOffice.org>
<https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Bridge/Automation_Bridge>
<https://www.pitonyak.org/oo.php>
<https://www.openoffice.org/udk/common/man/spec/ole_bridge.html>
Using UNO.CLS create a new scalc worksheet, create random data and a chart based on it.
Demonstrates how to incorporate UNO_CONSTANTS and UNO_ENUM values into a
Rexx directory-like collection for easier use.
***********************************************************************/
/* create spreadsheet, get first sheet */
xDesktop=uno.createDesktop() -- bootstrap & get access to XDesktop
xcl=xDesktop~XComponentLoader -- get XComponentLoader interface
uri="private:factory/scalc" -- new scalc document
doc=xcl~loadComponentFromURL(uri,"_blank",0,.uno~noProps)
xSheets=doc~XSpreadSheetDocument~getSheets~XIndexAccess
sheet =xSheets~getByIndex(0)~XSpreadSheet -- get first spreadsheet
/* note values can be assigned to cells with "string", "formula" or "value" (for numbers) */
/* create the titles, pretend the last two years */
year = date()~right(4)-2
titles = "Quarter", year, year+1 /* title array */
do col = 1 to titles~items
call uno.setCell sheet, col-1, 0, titles[col]
end
/* get all UNO_ENUM values in a Rexx directory */
justify = .uno_enum~new("com.sun.star.table.CellHoriJustify")
say "justify:" justify
props=sheet~XCellRange~getCellRangeByName("B1:C1")~XPropertySet
props~setPropertyValue("HoriJustify", justify~right)
/* get all UNO_CONSTANTS values in a Rexx directory */
weights = .uno_constants~new("com.sun.star.awt.FontWeight")
say "weights:" weights
props=sheet~XCellRange~getCellRangeByName("A1:C1")~XPropertySet
props~setPropertyValue("CharWeight", weights~bold)
/* create random values for the quarter numbers */
do line = 1 to 4
call uno.setCell sheet, 0,line, "Q"line /* title in first column */
call uno.setCell sheet, 1,line, random(0,500000)/100
call uno.setCell sheet, 2,line, random(0,750000)/100
end
props=sheet~XCellRange~getCellRangeByName("A2:A5")~XPropertySet
props~setPropertyValue("CharWeight", weights~bold)
/* format numbers, predefined style, format: "#,##0.00" */
props=sheet~XCellRange~getCellRangeByName("B2:C5")~XPropertySet
props~setPropertyValue("NumberFormat",4)
/* create a chart from the data */
structRect = .bsf~new("com.sun.star.awt.Rectangle")
structRect~X = 300 -- x-offset: 0.300 cm
structRect~Y = 2250 -- y-offset: 2.250 cm
structRect~Width = 16000 -- width: 16.000 cm
structRect~Height = 8000 -- height: 8.000 cm
range = sheet~XCellRange~getCellRangeByName("A1:C5") -- data to be used for the chart
rangeAddr = range~XCellRangeAddressable~getRangeAddress
arrOfAddr = bsf.createArrayOf(rangeAddr~getClass, rangeAddr) -- create array
tableCharts = sheet~XTableChartsSupplier~getCharts -- get chart collection & insert
tableCharts~addNewByName("FirstChart", structRect, arrOfAddr, .true, .true)
::requires UNO.CLS -- get UNO support, will require BSF.CLS (ooRexx-Java bridge)
If there are any questions, please ask them.
---rony
On 24.06.2022 13:02, Rony G. Flatscher wrote:
> This ooRexx program creates a scalc spreadsheet, its data and a chart. There is a routine that
> allows to fetch constant and enum values in an ooRexx directory, such that sending the name of a
> constant or enum value will return the value one has to use as an argument.
>
> /**********************************************************************
> AOO_scalc_chart.rex using OLE (object linking and embedding) with ooRexx
>
> Links: <https://OpenOffice.org>
> <https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Bridge/Automation_Bridge>
> <https://www.pitonyak.org/oo.php>
> <https://www.openoffice.org/udk/common/man/spec/ole_bridge.html>
>
> Using OLE create a new scalc worksheet, create random data and a chart based on it.
> Demonstrates how to use UNO reflection and incorporate UNO_CONSTANTS and UNO_ENUM
> values into a Rexx directory for easier use.
> ***********************************************************************/
>
> /* create a spreadsheet, add data for comparing two years by quarter, add a chart */
> serviceManager = .OLEObject~new('com.sun.star.ServiceManager')
> /* create spreadsheet, get first sheet */
> desktop = serviceManager~createInstance('com.sun.star.frame.Desktop')
> noProps = .array~new /* empty array (no properties) */
> document = desktop~loadComponentFromURL('private:factory/scalc', '_blank', 0, noProps)
> sheet = document~sheets~getByIndex(0) -- get first spreadsheet
>
> /* note values can be assigned to cells with "string", "formula" or "value" (for numbers) */
> /* create the titles, pretend the last two yers */
> year = date()~right(4)-2
> titles = "Quarter", year, year+1 /* title array */
> do col = 1 to titles~items
> sheet~getCellByPosition(col-1,0)~string = titles[col]
> end
> /* get all UNO_ENUM values in a Rexx directory */
> justify = getAsDirectory(serviceManager, "com.sun.star.table.CellHoriJustify")
> say "justify:" justify
> /* right adjust the last two years
> - possibility 1: sheet~getCellRangeByName("B1:C1")~setPropertyValue("HoriJustify",
> justify~right)
> - or: */
> sheet~getCellRangeByName("B1:C1")~HoriJustify = justify~right
>
> /* get all UNO_CONSTANTS values in a Rexx directory */
> weights = getAsDirectory(serviceManager,"com.sun.star.awt.FontWeight")
> say "weights:" weights
> sheet~getCellRangeByName("A1:C1")~CharWeight = weights~bold /* column headings */
>
> /* create random values for the quarter numbers */
> do line = 1 to 4
> sheet~getCellByPosition(0,line)~string = "Q"line /* title in first column */
> sheet~getCellByPosition(1,line)~value = random(0,500000)/100
> sheet~getCellByPosition(2,line)~value = random(0,750000)/100
> end
> sheet~getCellRangeByName("A2:A5")~CharWeight = weights~bold /* column headings */
> /* format numbers, predefined style, format: "#,##0.00" */
> sheet~getCellRangeByName("B2:C5")~setPropertyValue("NumberFormat",4)
>
> /* create a chart from the data */
> structRect = serviceManager~bridge_getStruct("com.sun.star.awt.Rectangle")
> structRect~X = 300 -- x-offset: 0.300 cm
> structRect~Y = 2250 -- y-offset: 2.250 cm
> structRect~Width = 16000 -- width: 16.000 cm
> structRect~Height = 8000 -- height: 8.000 cm
>
> range = sheet~getCellRangeByName("A1:C5") /* data to be used for the chart */
> rangeAddr = range~getRangeAddress
> arrOfAddr = .array~of(rangeAddr) /* create array with the range address */
> tableCharts = sheet~getCharts /* get chart collection & insert */
> tableCharts~addNewByName("FirstChart", structRect, arrOfAddr, .true, .true)
>
> /* Routine returns a Rexx directory containing all names and values of the supplied
> UNO_CONSTANTS or UNO_ENUM class name (needs to be fully qualified). */
> ::routine getAsDirectory
> use strict arg serviceManager, unoClzName
>
> dir = .Directory~new -- directory will get
> dir~objectName = unoClzName -- allows to show the uno class it represents
>
> ctxt = serviceManager~defaultContext
> tdm = ctxt~getValueByName("/singletons/com.sun.star.reflection.theTypeDescriptionManager")
> reflClz= tdm~getByHierarchicalName(unoClzName)
> if reflClz~isNil then return dir -- return empty directory
>
> typeClass = reflClz~getTypeClass
> if typeClass = 30 then -- UNO_CONSTANTS
> do
> dir~objectName = unoClzName "(UNO_CONSTANTS)" -- supply type info to name
> do c over reflClz~getConstants -- iterate over constant fields
> name = c~getName -- fully qualified
> name = name~substr(name~lastPos('.')+1) -- extract last word
> dir[name] = c~getConstantValue -- store constant values with their names
> -- say "name:" name "->" c~getConstantValue
> end
> end
> else if typeClass = 15 then -- UNO_ENUMERATION
> do
> dir~objectName = unoClzName "(UNO_ENUM)" -- supply type info to name
> enumNames = reflClz~getEnumNames -- get all enumeration names
> enumValues = reflClz~getEnumValues -- get all enumeration values
> do i=1 to enumNames~items
> name = enumNames[i]
> name = name~substr(name~lastPos('.')+1) -- extract last word
> dir[name] = enumValues[i] -- store enum values with their names
> -- say "name:" name "->" enumValues[i]
> end
> end
> return dir
>
> HTH
>
> ---rony
>
> P.S.: The short paper at <https://epub.wu.ac.at/8118/> introduces ooRexx briefly in ten pages,
> home of Rexx based technologies is the non-profit SIG "Rexx Language Association" at
> <https://www.RexxLA.org>.