[Solved] Array of spreadsheets from a directory

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] Array of spreadsheets from a directory

Post by sokolowitzky »

Hello, I have this code below. It works fine, I've found it in this forum.
Anyway, I need this code to work for all "Fname"s in a selected folder.
I know that I should use something like that =>"for i = 1 to ubound". But I don't know how to modify this. I think I should use some kind of array of spreadsheets.

Code: Select all

Global oDocument as Object
Global Fname as string
Sub OpenData

FName="C:\Users\Sokolowitzky\Desktop\Data Archive\B1596.csv" 
fnURL=ConvertToURL(FName) 


'Create new document and import data
oDocument = StarDesktop.LoadComponentFromURL( fnURL, "_blank", 0, _
Array(MakePropertyValue( "FilterName", "Text - txt - csv (StarCalc)" ), _
         MakePropertyValue( "FilterOptions", "44,34,0,1,1" )

End Sub

'property function 

Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue 
   oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" ) 
   If Not IsMissing( cName ) Then 
      oPropertyValue.Name = cName 
   EndIf 
   If Not IsMissing( uValue ) Then 
      oPropertyValue.Value = uValue 
   EndIf 
   MakePropertyValue() = oPropertyValue 
End Function
Last edited by sokolowitzky on Mon Aug 20, 2018 7:10 am, edited 3 times in total.
Win10-OpenOffice 4.1/LibreOffice 7.4
UnklDonald418
Volunteer
Posts: 1548
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Array of spreadsheets from a directory

Post by UnklDonald418 »

Try this

Code: Select all

DirName = "C:\Users\Sokolowitzky\Desktop\Data Archive\"
FileName = Dir( DirName & "*.*")   
Do While FileName <> ""
   FName=DirName & FileName
   fnURL=ConvertToURL(FName)
' process the selected file here
   
FileName=dir()  'get next file name
Loop
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: Array of spreadsheets from a directory

Post by sokolowitzky »

Works like a charm. Thanks a lot!
Win10-OpenOffice 4.1/LibreOffice 7.4
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: [Solved]Array of spreadsheets from a directory

Post by sokolowitzky »

What if I used a spreadsheet instead of a directory? I made this code out of the former one, but unfortunately it does not work.
The website addresses are on column A. When I click run, this code does nothing. What do you think?

Code: Select all

REM  *****  BASIC  *****

Global oDocument as Object
Global Fname as string
Global fnURL as string

Sub OpenData
dim i as integer
Dim oSheet as object
Dim oCell1 as object



oDocument = ThisComponent
oSheet = oDocument.getCurrentController.getActiveSheet



i = 0
oCell1 = oSheet.getCellByPosition(i,0)

while oCell1.Type <> com.sun.star.table.CellContentType.EMPTY
i = i+1
oCell1 =  oSheet.getCellByPosition(i,0)




'Create new document and import data
oDocument = StarDesktop.LoadComponentFromURL( fnURL, "_blank", 0, _
Array(MakePropertyValue( "FilterName", "Text - txt - csv (StarCalc)" ), _
MakePropertyValue( "FilterOptions", "44,34,0,1,1" )
wend
fnURL=oCell1.string

End Sub

'property function 

Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue                 
oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" ) 
If Not IsMissing( cName ) Then 
  oPropertyValue.Name = cName 
EndIf 
If Not IsMissing( uValue ) Then 
  oPropertyValue.Value = uValue 
EndIf 
MakePropertyValue() = oPropertyValue 
End Function
Win10-OpenOffice 4.1/LibreOffice 7.4
UnklDonald418
Volunteer
Posts: 1548
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: [Solved]Array of spreadsheets from a directory

Post by UnklDonald418 »

If Column A contains a list of complete URL's the assignment

Code: Select all

fnURL=oCell1.string

should work
If Column A contains a list of file names includeing the path such as
C:\Users\Sokolowitzky\Desktop\Data Archive\File1.csv
then use

Code: Select all

fnURL=ConvertToURL(oCell1.string)
In my example Column A just contains a list of file names with extensions

Code: Select all

REM  *****  BASIC  *****

Sub OpenData
Dim i as integer
Dim oDocument as Object
Dim oSheet as object
Dim oCell1 as object
Dim fnURL as string

oDocument = ThisComponent
oSheet = oDocument.getCurrentController.getActiveSheet


DirName = "C:\Users\Sokolowitzky\Desktop\Data Archive\"
i = 0
oCell1 = oSheet.getCellByPosition(0,i)
'oMRI.inspect oCell1
while oCell1.Type <> com.sun.star.table.CellContentType.EMPTY
	fnURL=ConvertToURL(DirName & oCell1.getString())
	'Create new document and import data
	oDocument = StarDesktop.LoadComponentFromURL( fnURL, "_blank", 0, _
	Array(MakePropertyValue( "FilterName", "Text - txt - csv (StarCalc)" ), _
	MakePropertyValue( "FilterOptions", "44,34,0,1,1" )
	i = i+1
	oCell1 =  oSheet.getCellByPosition(0,i)
	wend

End Sub

'property function

Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue                 
oPropertyValue = createUnoStruct( "com.sun.star.beans.PropertyValue" )
If Not IsMissing( cName ) Then
  oPropertyValue.Name = cName
EndIf
If Not IsMissing( uValue ) Then
  oPropertyValue.Value = uValue
EndIf
MakePropertyValue() = oPropertyValue
End Function
In your code the arguments were reversed in the method getCellByPosition, you were incrementing columns instead of rows.
The variable fnURL needs to have a value assigned to it before it can be used.
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
Post Reply