OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Wriying arrays into a spreadsheet

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
liam
Newbie
Newbie


Joined: 10 Apr 2010
Posts: 3

PostPosted: Sat Apr 10, 2010 3:08 am    Post subject: Wriying arrays into a spreadsheet Reply with quote

If I have an array X(1000) and want to write its contents into a spreadsheet, say Sheet1, I would write the following

Dim i

For i=0 To 999
thisComponent.Sheets.GetByName("Sheet1").getCellRangeByName("A1:A10000").getCellByPosition(0,i).String=X(i)

Next i

I have noticed though, this is a sluggish way to do this. It can take seconds, when in reality it should be able to do this almost instantly.

Is there any quicker way?
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1453
Location: France

PostPosted: Sat Apr 10, 2010 5:52 am    Post subject: Reply with quote

Hi,
  1. Don't suppose that Basic will optimize your code. Do it yourself.
  2. Learn API and read similar posts

Code:
Dim mySheet As Object
Dim x As Long,  t As Long, v(10000) As String

for x = 0 to 10000
  v(x) = "Hello " & x
next

t = GetSystemTicks

ThisComponent.lockControllers
mySheet = ThisComponent.Sheets.getByName("Sheet1")
for x = 0 to 1000
  mySheet.getCellByPosition(1, x).String = v(x)
next
ThisComponent.unlockControllers

MsgBox("Done in " & GetSystemTicks-t & " milliseconds")

The writing loop takes around 180 millisec on my PC.
_________________
Bernard

OpenOffice.org 1.1.5 fr / Apache OpenOffice 4.0.1 / LibreOffice 4.1.0
MS-Windows 7 Home SP1
This forum is spammed, use instead Apache OpenOffice forums
Back to top
View user's profile Send private message Visit poster's website
turtle47
Power User
Power User


Joined: 24 Aug 2008
Posts: 66
Location: Germany

PostPosted: Sat Apr 10, 2010 6:54 am    Post subject: Reply with quote

The following code is slightly faster:

Code:
Sub Main
Dim x As Long,  t As Long, v(10000,0) As String

   for x = 0 to 10000
   v(x,0) = "Hello " & x
   next
   t = GetSystemTicks
   oSheet1 = ThisComponent.sheets().getbyname("Tabelle1")
   oSheet1.getCellrangeByName("A1:A10001").SetDataArray(v())   
   MsgBox("Done in " & GetSystemTicks-t & " milliseconds")
End Sub


It takes around 110 millisec on my PC
_________________
My Extensions
Color2Rows and LastSession
Back to top
View user's profile Send private message
B Marcelly
Super User
Super User


Joined: 12 May 2004
Posts: 1453
Location: France

PostPosted: Sat Apr 10, 2010 9:14 am    Post subject: Reply with quote

turtle47 wrote:
The following code is slightly faster:

Well, not slightly Laughing
it is more than 10 times faster.

I also tried it, but made a mistake with the loop numbers ( in my above code, 1000 instead of 10 000) and erroneously found a similar value for both algorithms.
Well done Wink

Edit
I also found that your code runs in 109 millisec.
But if you change one line as :
Code:
oSheet1.getCellrangeByName("A1:A10001").DataArray = v()

then it runs in 234 millisec. Very strange.
_________________
Bernard

OpenOffice.org 1.1.5 fr / Apache OpenOffice 4.0.1 / LibreOffice 4.1.0
MS-Windows 7 Home SP1
This forum is spammed, use instead Apache OpenOffice forums
Back to top
View user's profile Send private message Visit poster's website
turtle47
Power User
Power User


Joined: 24 Aug 2008
Posts: 66
Location: Germany

PostPosted: Sat Apr 10, 2010 10:14 am    Post subject: Reply with quote

Quote:
I also tried it, but made a mistake with the loop numbers ( in my above code, 1000 instead of 10 000)

Thanks for feedback.
I was somewhat blind on my eyes Cool
_________________
My Extensions
Color2Rows and LastSession
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group