Access cellrange in stringitemlist

Creating a macro - Writing a Script - Using the API

Access cellrange in stringitemlist

Postby tmanikanda » Tue May 15, 2018 1:00 pm

Hi All,
I am new to open office and even macros. I am trying to build a dialog box with list box or combo box in it. I need to load the list box with contents from a calc spreadsheet column.

oMyCombo= oDialog1.getControl("ComboBox1").getModel()
oCellRange = oSheet_tst.getCellRangeByName("I2:I7")
arg = Array("Item1", "Item2", "Item3", "Item4", "Item5")
REM Array(oCellRange) - Not working
REM Array( "Item1", "Item2", "Item3", "Item4", "Item5" ) - Working
oMyCombo.StringItemList=(arg)


In the above code if i use array of strings hardcoded it is working, but if I use oCellRange inside Array, I am getting error - Incorrect property value. Please help me in this
Apache OpenOffice 4.1.3 , Windows 10
tmanikanda
 
Posts: 6
Joined: Tue May 15, 2018 9:36 am

Re: Access cellrange in stringitemlist

Postby Zizi64 » Tue May 15, 2018 3:40 pm

Please upload your ODF type samle file with the embedded macro code here
Tibor Kovacs, Hungary; LO4.4.7, LO5.4.7 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.0.5 and AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 6899
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Access cellrange in stringitemlist

Postby FJCC » Tue May 15, 2018 5:10 pm

You cannot assign the oCellRange to StringItemList because oCellRange is not an array of strings, it is an object describing a range of cells. One of its properties is a DataArray, which is an array of arrays of strings representing the cell contents. DataArray(i)(j) refers to the jth column of the ith row and you can refer to the entire ith row with DataArray(i). There is not compact way, I think, to refer to the entire jth column because those elements are in separate arrays. I haven't thought about array handling in Basic for a long time, so I may be wrong about that. If you store your values in a row, say I2:M2, this should work. (I put the combo box directly on my spreadsheet, not in a dialog)
Code: Select all   Expand viewCollapse view
  oDrawPages = ThisComponent.getDrawPages()
  oObj1 = oDrawPages.getByIndex(0)
  oObj2 = oObj1.getByIndex(0)
  oControl = oObj2.getControl()
 
  oSheet = ThisComponent.Sheets.getByIndex(0)
  oCellrange = oSheet.getCellRangeByName("I2:M2")
  DatArr = oCellrange.DataArray
  oControl.StringItemList = DatArr(0)
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 6582
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Access cellrange in stringitemlist

Postby Lupp » Tue May 15, 2018 7:20 pm

The indices of DataArray (as most indices on the technical level) are 0-based.

Another question to the Original Questioner: What is your understanding of a LIST?
That is not just the same as an array (1D or higher). 
You seem to want an array eventually, but another row in your post looks a if you try to treat a CellRange as a list.
The problem with this is not just that cell contents cannot be referenced as the cells themselves (like a default property), but also that neither the CellRange nor its DataArray are lists.

What TYPE of value do you actually want to assign to a property of your ComboBox?
(If this sounds silly: I never use ComboBoxes in Calc.)
On Windows 10: LibreOffice 6.1 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 1879
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Access cellrange in stringitemlist

Postby tmanikanda » Thu May 17, 2018 9:17 am

Hi All,

thanks all for your reply. Please find the sample sheet i am working on. The Item list will be a getting added. Also based on the indicator value i want them on the list.
Attachments
Item_list.ods
(13.15 KiB) Downloaded 40 times
Apache OpenOffice 4.1.3 , Windows 10
tmanikanda
 
Posts: 6
Joined: Tue May 15, 2018 9:36 am

Re: Access cellrange in stringitemlist

Postby tmanikanda » Sat May 26, 2018 7:59 am

Hi All
I am using for loop and additem to add items into the list box, but it is becoming slow when the row count increase, also i am still finding a way to filter the entries

For vi=0 To 100
vitem = oCellRange.DataArray(vi)(0)
If vitem <> "" Then
olistbox.AddItem(oCellRange.DataArray(vi)(0),vi)
End If
Next vi
I am not able to use match and index functions. Getting error

oItemRng = oSheet_Prod.getCellRangeByName("B2:B1000")
arg1 = Array(oProdRng)
oItemIdRng = oSheet_Prod.getCellRangeByName("A2:A1000")
arg2 = Array(oProdIdRng)

vtmp1 = svc.callFunction("MATCH",vproduct_name,arg1,0)
vproduct_id = svc.callFunction("INDEX",arg2,vtmp1)


Can someone help me in this
Apache OpenOffice 4.1.3 , Windows 10
tmanikanda
 
Posts: 6
Joined: Tue May 15, 2018 9:36 am

Re: Access cellrange in stringitemlist

Postby Zizi64 » Sat May 26, 2018 8:17 am

How related the colorized variables to this task? Where you assign a value to the variable oProdRng? Please upload the full code of the Sub (or Function) with all of the dependencies. Better to upload a full sample file (again)

Code: Select all   Expand viewCollapse view
[color=#FF0000]oItemRng[/color] = oSheet_Prod.getCellRangeByName("B2:B1000")
arg1 = Array([color=#FF0000]oProdRng[/color])
oItemIdRng = oSheet_Prod.getCellRangeByName("A2:A1000")
arg2 = Array(oProdIdRng)

vtmp1 = svc.callFunction("MATCH",vproduct_name,arg1,0)
vproduct_id = svc.callFunction("INDEX",arg2,vtmp1)
Tibor Kovacs, Hungary; LO4.4.7, LO5.4.7 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.0.5 and AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 6899
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Access cellrange in stringitemlist

Postby tmanikanda » Mon Jun 04, 2018 7:26 am

Hi Zizi64
oItemRng and oProdRng are same its my mistake while copying the code. I have attached a sample file in my previous post on May 17th. Also the for loop i have used is becoming slower when the count is more(ex. 1000)
Apache OpenOffice 4.1.3 , Windows 10
tmanikanda
 
Posts: 6
Joined: Tue May 15, 2018 9:36 am

Re: Access cellrange in stringitemlist

Postby hubert lambert » Mon Jun 04, 2018 4:38 pm

Hi,

You can call the TRANSPOSE() function to get the item array. Something like this:
Code: Select all   Expand viewCollapse view
  oMyCombo= oDialog1.getControl("ComboBox1")
  oCellRange = oSheet_tst.getCellRangeByName("A2:C8")
  s = createUnoService("com.sun.star.sheet.FunctionAccess")
  data = s.callFunction("TRANSPOSE", array(oCellRange))
  oDialog1.getControl("ComboBox1").addItems(data(0), 0)
  oMyCombo.DropDownLineCount = ubound(data(0)) +1

Regards
Attachments
tmanikanda.ods
(14.92 KiB) Downloaded 21 times
AOOo 4.1.2 on Win7 | LibreOffice on various Linux systems
hubert lambert
 
Posts: 83
Joined: Mon Jun 13, 2016 10:50 am

Re: Access cellrange in stringitemlist

Postby tmanikanda » Thu Jul 12, 2018 11:41 am

Hi All
Thanks for your replies. I tried lambert suggestion. Its working for getting the whole column into the listbox, But i need to get the items only which is not yet active, can i do this? Also i am in need of new requirement. Want to select more than one item and update them, For ex. select both item 3 and 4 from list box and make them active. Need help on this
Attachments
tmanikanda.ods
(14.47 KiB) Downloaded 4 times
Apache OpenOffice 4.1.3 , Windows 10
tmanikanda
 
Posts: 6
Joined: Tue May 15, 2018 9:36 am

Re: Access cellrange in stringitemlist

Postby Lupp » Thu Jul 12, 2018 4:45 pm

@hubert lambert:
1.
Code: Select all   Expand viewCollapse view
data = s.callFunction("TRANSPOSE", array(oCellRange))
works taking .DataArray as the "default property" of a com.sun.star.sheet.CellRange object. I hadn't expected that - and I would still prefer to write
Code: Select all   Expand viewCollapse view
dataArray = s.callFunction("TRANSPOSE", array(oCellRange.DataArray))
The name dataArray replacing data there because CellRange objects also have an array property .Data which (for historical reasons?) only contains numeric data NAN for strings and blank cells.
2.
Since you seem to be familiar with the array-properties of CellRanges and with their specific way of using indices, a question OT in this thread: Do you know a way to assign such a special array to a properly dimensioned ordinary array with one statement (without any loops)?
The reverse case is Cellrange.SetDataArray() with works with 2D-arrays dimensioned in Basic.

tmanikanda wrote:But i need to get the items only which is not yet active, can i do this?

Having the one-column-arrays for item, isActive, and price you can scan isActive for active entries and create a compacted version of 'items'.
To correctly get the current price for a selected item, and to push it to the respective cell, you can generate an array giving an indirection to the original indices depending on the listbox indices for the selected item.

Sorry. Just found you wanted to allow selection of INactive items only. Simply adapt the few respective lines and add
Code: Select all   Expand viewCollapse view
    oCellRange.getCellByPosition(1, indirection(itempos)).String = "Y"
where already the new price is pushed.
=== Edit ===
Demo replaced.
See crude demo attached.
Attachments
tmanikandaReworkedInactive.ods
(15.34 KiB) Downloaded 6 times
On Windows 10: LibreOffice 6.1 and older versions, PortableOpenOffice 4.1.5 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
 
Posts: 1879
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Access cellrange in stringitemlist

Postby tmanikanda » Sat Jul 14, 2018 3:46 pm

Hi Lupp
The code you gave worked. Thanks. Just want to understand that your adding the items which are inactive to a array and using that array in additems function. Am i right.
One more thing i am looking for is to select multiple items in the listbox. getselecteditem is working where i am selecting only one item from the listbox, but getselecteditems is not working where i am selecting multiple items. Any idea on this
Apache OpenOffice 4.1.3 , Windows 10
tmanikanda
 
Posts: 6
Joined: Tue May 15, 2018 9:36 am

Re: Access cellrange in stringitemlist

Postby Zizi64 » Sat Jul 14, 2018 4:24 pm

getselecteditem is working where i am selecting only one item from the listbox, but getselecteditems is not working where i am selecting multiple items. Any idea on this


https://forum.openoffice.org/en/forum/v ... 20&t=94115
Tibor Kovacs, Hungary; LO4.4.7, LO5.4.7 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.0.5 and AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 6899
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 2 guests