[Solved] SetDataArray setting array with 1 column

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

[Solved] SetDataArray setting array with 1 column

Post by sokolowitzky »

Hi,

I'm trying to split some string into a range. SetDataArray would help me do this better.
But apparently this function does not work with a one line array. It gives me "Object variable not set." error. I feel like this is because the array is not 2d. But I can't figure out how to turn this into an array with 2 columns

Here below you can find a code I wrote.

Code: Select all

SUB Test_2_Array_to_String
otext = "splitted"

dim da(len(otext))

for ii = 0 to LEN(OTEXT) - 1
da(ii) = mid(otext, ii + 1, 1)
next
msgbox """" & join(da(), chr(10)) & """"
for yy = 0 to ubound(da()) -1
next
oRange = ThisComponent.Sheets(3).getCellRangeByPosition(0,0,0,len(otext)-1)
oRange.setdataarray(da())


END SUB
Last edited by robleyd on Sun Jun 27, 2021 1:42 am, edited 5 times in total.
Reason: Add green tick
Win10-OpenOffice 4.1/LibreOffice 7.4
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: SetDataArray issue for setting an array with one column

Post by Villeroy »

menu:Data>Text to columns... does the trick quite well and easily.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: SetDataArray issue for setting an array with one column

Post by sokolowitzky »

Actually I use this splitting macro as a part of another macro. So it'd be helpful if I could do this without "text to columns".
Win10-OpenOffice 4.1/LibreOffice 7.4
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: SetDataArray issue for setting an array with one column

Post by RoryOF »

There is discussion on setDataArray in this topic
viewtopic.php?f=20&t=90995
which may be helpful.

Do I recollect correctly that the term setDataArray is case sensitive?
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: SetDataArray issue for setting an array with one column

Post by Villeroy »

RoryOF wrote:Do I recollect correctly that the term setDataArray is case sensitive?
With a little help from my friends:

Code: Select all

REM  *****  BASIC  *****
Sub test_Splitter()
REM calling helper functions: getOffsetRange, getRangeFromAddress, bas_PushArray
Const cSplit = "/"
src = ThisComponent.getCurrentSelection()
tgt = getOffsetRange(src,0,0,0,src.Columns.getCount()+1)
da() = src.getDataArray()
rows = uBound(da())
cols = uBound(da(0))
for i = 0 to rows
	row = da(i)
	s() = split(row(0), cSplit)
	row(0) = s(0)
	bas_PushArray(row, s(1))
	da(i) = row
next i
tgt.setDataArray(da())
End Sub

Function getOffsetRange(oRg, nRowOffset&, nColOffset&, nRowResize&, nColResize&)
Dim addr
'calls: getRangeByAddress
	addr = oRg.getRangeAddress()
	addr.StartRow = addr.StartRow + nRowOffset
	addr.EndRow = addr.EndRow + nRowOffset
	addr.StartColumn = addr.StartColumn + nColOffset
	addr.EndColumn = addr.EndColumn + nColOffset
	if nRowResize > 0 then addr.EndRow = addr.StartRow + nRowResize -1
	if nColResize > 0 then addr.EndColumn = addr.StartColumn + nColResize -1
	getOffsetRange = getRangeByAddress(oRg.getSpreadsheet(), addr)
End Function

'pass a spreadsheet-document, sheet or range  together with a c.s.s.table.CellRangeAddress
'return empty if oAddr out of bounds or wrong obj
Function getRangeByAddress(obj, oAddr as com.sun.star.table.CellRangeAddress)
on error goto nullErr:
Dim oSheet
	If obj.supportsService("com.sun.star.sheet.SpreadsheetDocument") then
		REM use the sheet specified by given address
		oSheet = obj.getSheets.getByIndex(oAddr.Sheet)
	else
		REM use given object (range/sheet) as parent range
		oSheet = obj
	endif
	getRangeByAddress = oSheet.getCellRangeByPosition(oAddr.StartColumn,oAddr.StartRow,oAddr.EndColumn,oAddr.EndRow)
exit function
nullErr:
	getRangeByAddress = Null
End Function

'very simple routine appending some element to an array which can be undimensioned (LBound > UBound)
Sub bas_PushArray(xArray(),vNextElement)
Dim iUB%,iLB%
	iLB = lBound(xArray())
	iUB = uBound(xArray())
	If iLB > iUB then
		iUB = iLB
		redim xArray(iLB To iUB)
	else
		iUB = iUB +1
		redim preserve xArray(iLB To iUB)
	endif
	xArray(iUB) = vNextElement
End Sub
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: SetDataArray issue for setting an array with one column

Post by JeJe »

Code: Select all

SUB Test_2_Array_to_String
otext = "splitted"
dim da(len(otext)-1)
for ii = 0 to LEN(OTEXT) - 1
da(ii) =array( mid(otext, ii + 1, 1))
next
oRange = ThisComponent.Sheets(3).getCellRangeByPosition(0,0,0,len(otext)-1)
oRange.setdataarray(da)
END SUB
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: [Solved]SetDataArray issue while setting array with 1 co

Post by sokolowitzky »

JeJe & Villeroy, RoryOF thank you all for your help and suggestions
Win10-OpenOffice 4.1/LibreOffice 7.4
User avatar
karolus
Volunteer
Posts: 1159
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved]SetDataArray issue while setting array with 1 co

Post by karolus »

Hallo

With python its litterally "easy as shit":

Code: Select all

doc = XSCRIPTCONTEXT.getDocument()
sel = doc.CurrentSelection
sheet = sel.Spreadsheet
cursor =sheet.createCursorByRange(sel)

data = [list("some Text")]

# for AOO:
#data= tuple((tuple("some Text"),))

cursor.collapseToSize( len(data[0]) , len(data) )
cursor.setDataArray(data)
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Post Reply