Is it a 1-dimensional or 2-dimensional array?

Hello,

I’m trying to have a Pie-Chart switch from the display of actual values, in the Data Labels, to percentages and back.

In VBA I create a 1-dimensional array (with the percentages) and assign that array to the Values-Series of the Chart. To switch back I assign the original Range back to de Values-Series. That always worked for me.

Now I’m trying to do the same thing in LO Calc. First thing in my research was to find out where the values are stored and I managed to come up with an array which holds the values:

oSh = thiscomponent.sheets.getByIndex(0)
oCh = oSh.charts.getbyindex(0)
myArray() = oCh.embeddedObject.Data.Data

I expected myArray() to be 1-dimensional, but it seems it is a 2-dimensional array:

Print myArray(2) 'Fails, which makes sense for 2-dimensional.
Print myArray(2)(0) 'That works.

Now I tought I understanded it, but to my surprise, getting the Ubound for de second dimension seemed to be inpossible:

This statement gives an error massage saying:
Inadmissible value or data type. Index out of defined range”.

Print UBound(myArray(),2)

So I tried to get more information by applying Listing 101 from Andrew Pitonyak’s book (http://www.pitonyak.org/OOME_3_0.pdf):

The arrayInfo() function from the book gives:
Array dimensioned as myArray(0 to 7) As Object

Print arrayInfo(myArray, "myArray")

I hope someone will be so kind to clarify this for me, because I’m getting quite confused now. :face_with_thermometer:

Just to be clear: I’m not looking for a solution, I already found one. I’m just looking for an explanation for the strange behavior of the array, returned from embeddedObject.Data.Data.

Thank you in advance.
Grtz.

It’s an array of row arrays. In case of one row, it’s an array with one row array.

Row = Array(Array("A1","B1","C1"))
Range = Array(Array("A1","B1","C1"), Array("A2","B2","C2"))
Column = Array(Array("A1"),Array("A2"), Array("A3"))

Wow, that is really cool. It explains everything I needed to know.

And to think that I was struggling with what I thought was a syntax problem: the syntax myArray(1,1) wouldn’t work. Of course in wouldn’t, it’s a 1-dimensional array! In my ignorance I changed the syntax to myArray(1)(1), which naturally worked, but I didn’t have a good understanding of what was going on.
So thanks for releasing me from this cargo cult experience. :smiley:
:+1:

These nested arrays are compatible with many (all?) programming languages.
However, if you work with sheet functions programmed in StarBasic, you will may notice that you get 2-D-Arrays in Basic style from the sheet whereas all the API functions assume nested arrays.
Many years ago, I wrote the following helper functions:

'convert a 2-dimensional, 1-based plain array (as passed by an array function of a cell)
'to a 1 dimensional, zero-based array of rows with nested arrays of columns (as returned by oRange.getDataArray())
Sub PlainArray2DataArray(a())
dim aCol(),aRow(), i%,j%,iUB1%,iUB2%
iUB1 = uBound(a(),1)
redim aRow(iUB1 -1)
iUB2 = uBound(a(),2)
for i = 1 to iUB1
   redim aCol(iUB2 -1)
   for j = 1 to iUB2
      aCol(j -1) = a(i,j)   
   next
   aRow(i -1) = aCol()
next
a() = aRow()
End Sub
'the opposite of the above
Sub DataArray2PlainArray(aRows())
Dim i%,j%,aCols(),aTmp()
redim aTmp(1 to uBound(aRows()) +1,1 To uBound(aRows(0)) +1)
for i = 0 to uBound(aRows())
   aCols = aRows(i)
   for j = 0 to uBound(aCols())
      aTmp(i +1,j +1) = aCols(j)
   next
next
aRows() = aTmp()
End Sub