Re: [api-dev] Re: Variant vs other types...
Thanks for your interesting reply! Parts of original messages and comments and maybe more questions below: 2007/1/29, Andrew Douglas Pitonyak [EMAIL PROTECTED]: Johnny Andersson wrote: Maybe I should put it another way: Let's say that I have these lines, among others: Dim Ctl As Object Dim Doc As Object, Sheet As Object Dim A As String Dim OBDef As Double ' Or whatever, it doesn't seem to matter in this case, see further below... Doc=ThisComponent Sheet=Doc.Sheets.getByName(Blah) OBDef=DataSheet.getCellRangeByPosition(4,1,9,3).getDataArray() ' Now OBDef seems to change to Variant. You have a Calc sheet and you return a cell range based on its position. Next, you call getDataArray() on the cell range. The official return type is sequence sequence any , which means an array of arrays of variants. My opinion is that this should cause an error because an array of arrays of any should not be assigned to a double unless the type can be coerced or cast into the double type. so, you found a but (that has been around a long time) in StarBasic. I rarely think about it because it has never caused me any problems. There might be some reason for the behavior but I am not aware of the reasons. So it is an array of arrays? That explains what happens when I copy it to a variable: The variable is shown as MyVariable(x)(y) rather than MyVariable(x,y) in the Basic debugger (I called it Övervakaren before, I just found out that it's actually Bevakaren, but in this case it's just about the same thing). If you wanted the specific values, i expect you to write something like this: Dim oData() Dim oRow() Dim i%, j% Dim s$ s = oData() = DataSheet.getCellRangeByPosition(4,1,9,3).getDataArray() For i=LBound(oData()) To UBound(oData()) oRow() = oData(i) For j = LBound(oRow()) To UBound(oRow()) s = s CStr(oRow(j)) Next s = s CHR$(10) Next MsgBox s What do you really want to accomplish? Do you want to copy a range of data from one location to another? If so, you can use methods to directly copy the data. I have used cheat methods such as oCellRange1.setData(oCellRange2.getData()) No, a function will use the data (and other data) for calculation. The function will return a value that will be written to another cell. The function will be in a loop and use three of the values (one column), different every time. Another solution would perharps be to create a struct (as it's called in C), but is that possible in OpenOffice.org Basic? Yes, you can create a user defined structure/type. To quote from my AndrewMacro.odt document. 1.1. User Defined Data Types As of OOo 1.1.1, you can define your own data types. Listing 5.51: You can define your own data types. Type PersonType FirstName As String LastName As String End Type Sub ExampleCreateNewType Dim Person As PersonType Person.FirstName = Andrew Person.LastName = Pitonyak PrintPerson(Person) End Sub Sub PrintPerson(x) Print Person = x.FirstName x.LastName End Sub Wow, I didn't know that! Now I have to rewrite everything macro and every function I've made so far..! They are not that many, on the other hand... In your example above, is the following possible? Sub ExampleCreateNewType Dim Person(100) As PersonType Person(0).FirstName = Andrew Person(0).LastName = Pitonyak PrintPerson(Person(0)) End Sub And is the following possible? Type PersonType FirstName As String LastName As String ThingsToDo(9) As String End Type And finally, is the following possible? Type PersonType FirstName As String LastName As String End Type Type Human Properties As PersonType Friends(9) As PersonType End Type And even more finally, is there something similar to pointers in OpenOffice.org Basic? I gave a presentation at the 2004 OOo Conference in Berlin concerning creating advanced data types using structures. The examples are in the presentation available on my web site. Interesting! I'd better take a look there then! Thanks for your reply! Johnny
Re: [api-dev] Re: Variant vs other types...
Johnny Andersson wrote: 2007/1/29, Andrew Douglas Pitonyak [EMAIL PROTECTED]: What do you really want to accomplish? Do you want to copy a range of data from one location to another? If so, you can use methods to directly copy the data. I have used cheat methods such as oCellRange1.setData(oCellRange2.getData()) No, a function will use the data (and other data) for calculation. The function will return a value that will be written to another cell. The function will be in a loop and use three of the values (one column), different every time. Very good. I assume that you now know how to do this. Another solution would perharps be to create a struct (as it's called in C), but is that possible in OpenOffice.org Basic? Yes, you can create a user defined structure/type. To quote from my AndrewMacro.odt document. 1.1. User Defined Data Types As of OOo 1.1.1, you can define your own data types. Listing 5.51: You can define your own data types. Type PersonType FirstName As String LastName As String End Type Sub ExampleCreateNewType Dim Person As PersonType Person.FirstName = Andrew Person.LastName = Pitonyak PrintPerson(Person) End Sub Sub PrintPerson(x) Print Person = x.FirstName x.LastName End Sub Wow, I didn't know that! Now I have to rewrite everything macro and every function I've made so far..! They are not that many, on the other hand... In your example above, is the following possible? Sub ExampleCreateNewType Dim Person(100) As PersonType Person(0).FirstName = Andrew Person(0).LastName = Pitonyak PrintPerson(Person(0)) End Sub Try it, yes, it works And is the following possible? Type PersonType FirstName As String LastName As String ThingsToDo(9) As String End Type No, you can NOT have an array inside of a struct. You need to fake it: Type PersonType FirstName As String LastName As String REM You can NOT have an array inside a structure REM ThingsToDo(9) As String REM But you can fake it... ThingsToDo As Variant End Type Sub ExampleCreateNewType Dim Person As PersonType Dim aa(9) As String Person.FirstName = Andrew Person.LastName = Pitonyak Person.ThingsToDo = aa() Print UBound(Person.ThingsToDo) REM Prints 9 Person.ThingsToDo = DimArray(9) Print UBound(Person.ThingsToDo) REM Prints 9 'PrintPerson(Person) End Sub And finally, is the following possible? Type PersonType FirstName As String LastName As String End Type Type Human Properties As PersonType Friends(9) As PersonType End Type You can not have an array inside of a struct. A struct in a struct, however, is fine. And even more finally, is there something similar to pointers in OpenOffice.org Basic? I demonstrate this in the presentation I referenced. I gave a presentation at the 2004 OOo Conference in Berlin concerning creating advanced data types using structures. The examples are in the presentation available on my web site. Interesting! I'd better take a look there then! Thanks for your reply! Johnny -- Andrew Pitonyak My Macro Document: http://www.pitonyak.org/AndrewMacro.odt My Book: http://www.hentzenwerke.com/catalog/oome.htm Info: http://www.pitonyak.org/oo.php See Also: http://documentation.openoffice.org/HOW_TO/index.html - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: [api-dev] Re: Variant vs other types...
Johnny Andersson wrote: Maybe I should put it another way: Let's say that I have these lines, among others: Dim Ctl As Object Dim Doc As Object, Sheet As Object Dim A As String Dim OBDef As Double ' Or whatever, it doesn't seem to matter in this case, see further below... Doc=ThisComponent Sheet=Doc.Sheets.getByName(Blah) OBDef=DataSheet.getCellRangeByPosition(4,1,9,3).getDataArray() ' Now OBDef seems to change to Variant. You have a Calc sheet and you return a cell range based on its position. Next, you call getDataArray() on the cell range. The official return type is sequence sequence any , which means an array of arrays of variants. My opinion is that this should cause an error because an array of arrays of any should not be assigned to a double unless the type can be coerced or cast into the double type. so, you found a but (that has been around a long time) in StarBasic. I rarely think about it because it has never caused me any problems. There might be some reason for the behavior but I am not aware of the reasons. If you wanted the specific values, i expect you to write something like this: Dim oData() Dim oRow() Dim i%, j% Dim s$ s = oData() = DataSheet.getCellRangeByPosition(4,1,9,3).getDataArray() For i=LBound(oData()) To UBound(oData()) oRow() = oData(i) For j = LBound(oRow()) To UBound(oRow()) s = s CStr(oRow(j)) Next s = s CHR$(10) Next MsgBox s ' By the way, (4,1,9,3) is E2:J4. Row 2 and 3 in that spreadsheet contains numbers, row 4 contains text. A=OBDef(2,0) ' This doesn't work. See above, it is an array of arrays, this is not a two dimensional array. Okay, so far, no good. When I look at the variables with Övervakaren (in Swedish, I don't have a clue what it might be called in English, but I am talking about the tool that helps me to see the content of variables while I step through the macro, the button that allows me to select variables to watch in the Basic API thing, has a pair of glasses on it and F7 does the same thing), it seems like OBDef(2)(0) is a Variant/String. What's the difference between Variant/String and just String? A variant can hold any type. So, you have a variant that references a string. So my question is really something like this: How do I, in this meaningless example, make the content of OBDef(2,0), which is a String variable, originally some text in cell E4 in spreadsheet Blah, to be copied to A, which is an ordinary String? Does th example shown above help? If not... oRow() = oBDef(2) A = oRow(0) What I would like to achieve is to copy a CellRange to a 2-dimensional variable. Since some rows contains numbers and other contains text I though that the type Variant could be useable, but obviously not... No I really have to copy the CellRange cell by cell? What do you really want to accomplish? Do you want to copy a range of data from one location to another? If so, you can use methods to directly copy the data. I have used cheat methods such as oCellRange1.setData(oCellRange2.getData()) Another solution would perharps be to create a struct (as it's called in C), but is that possible in OpenOffice.org Basic? Yes, you can create a user defined structure/type. To quote from my AndrewMacro.odt document. 1.1. User Defined Data Types As of OOo 1.1.1, you can define your own data types. Listing 5.51: You can define your own data types. Type PersonType FirstName As String LastName As String End Type Sub ExampleCreateNewType Dim Person As PersonType Person.FirstName = Andrew Person.LastName = Pitonyak PrintPerson(Person) End Sub Sub PrintPerson(x) Print Person = x.FirstName x.LastName End Sub I gave a presentation at the 2004 OOo Conference in Berlin concerning creating advanced data types using structures. The examples are in the presentation available on my web site. Johnny -- Andrew Pitonyak My Macro Document: http://www.pitonyak.org/AndrewMacro.odt My Book: http://www.hentzenwerke.com/catalog/oome.htm Info: http://www.pitonyak.org/oo.php See Also: http://documentation.openoffice.org/HOW_TO/index.html - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]