Guest User

LibreOffice Basic Export and Concatenate

a guest
Nov 4th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. REM  *****  BASIC  *****
  2. REM Workhorse function for the concatenate operation
  3. Sub InsertSheetFromFile(sFName, optional sShName, optional tShName, optional pos, optional prot)
  4.    'Brings in a sheet from another document
  5.   '   sFName = file name that can be converted to a url
  6.   '    sShName = name of sheet in source file; default = first sheet (also falls back to this if sheet can't be found)
  7.   '   tShName = name you want for sheet in current document, modified to avoid duplicates; default = sShName
  8.   '   pos = position in current document; if negative count backward from end (-1 = place at end), default = -1
  9.   '   prot: set True if you want the new sheet protected; default = False
  10.  
  11.    REM target document
  12.    Dim tDoc, tSheets, tSh, sName$, p%
  13.    tDoc = ThisComponent
  14.    tSheets = tDoc.Sheets
  15.    if not isMissing(tShName) then tShName = getUniqueName(tSheets, tShName)
  16.    On Local Error GoTo badPos
  17.       p = cInt(pos)
  18.       if p<0 then
  19.          p = max(0, tSheets.count + 1 + p)
  20.       elseif p>0 then
  21.          p = min(p, tSheets.count)
  22.       end if
  23.    badPos: if err <> 0 then p = tSheets.count  
  24.  
  25.    REM source document
  26.    Dim sURL$, sDoc, shName$
  27.    Dim args(2) As New com.sun.star.beans.PropertyValue
  28.    args(0).Name = "Hidden"
  29.    args(0).Value = True
  30.    args(1).Name = "ReadOnly"
  31.    args(1).Value = True
  32.    args(2).Name = "FilterOptions"
  33.    REM 9 is ASCII value for tab
  34.    REM 0 is for no text delimiter
  35.    REM 76 is unicode (UTF-8), 60 is cp932 (SHIFT-JIS)
  36.    REM 1 is first line to import
  37.    args(2).Value = "9,0,76,1"
  38.  
  39.    sURL = converttoUrl(sFName)     
  40.    sURL = convertToUrl(sFName)
  41.    sDoc = StarDesktop.loadComponentFromURL(sURL, "_default", 0, args())
  42.  
  43.    On Local Error Goto NoSheet : sShName = sDoc.sheets.getByName(sShName).name
  44.    NoSheet: if err <> 0 then sShName = sDoc.sheets(0).name
  45.  
  46.    tsheets.importSheet(sDoc, sShName, p)
  47.    sDoc.close(True)
  48.  
  49.    REM Tidy up the target document
  50.    tSh = tSheets.getByIndex(p)
  51.    if not isMissing(tShName) then tSh.name = tShName
  52.    on local error goto NoProt:
  53.       if not prot then tsh.unprotect("")
  54.    NoProt: tsh.unprotect("")
  55. End Sub
  56.  
  57. Function getUniqueName(oContainer,sName$)
  58. Dim i%,sNew$
  59.    sNew = sName
  60.    Do while oContainer.hasByName(sNew)
  61.       i = i +1
  62.       sNew = sName &"_"& i      
  63.    loop
  64.    getUniqueName = sNew
  65. End Function
  66.  
  67. function max(a,b) : max = iif(a < b, b, a) : end function
  68. function min(a,b) : min = iif(a > b, b, a) : end function
  69.  
  70. sub Compile()
  71.     Dim i, strFile, ext_pos
  72.     REM You need the \ at the end of your path (or / for Linux systems)
  73.     path ="YOUR PATH HERE\"
  74.     strFile = Dir(path,0)
  75.     i = 1
  76.     while strFile <> ""
  77.         ext_pos = InStr(strFile, ".")
  78.         if Right(strFile, 4) = ".tsv" then
  79.             InsertSheetFromFile(path + strFile,,left(strFile, ext_pos-1))
  80.         end if
  81.         strFile = Dir ' returns next entry
  82.        i = i + 1
  83.     wend
  84.     thisComponent.getSheets().removeByName("Sheet1")
  85. End Sub
  86.  
  87. REM  *****  BASIC  *****
  88.  
  89. REM It exports all sheets in the workbook in .tsv format
  90. REM UTF-8 encoded. File names are taken from sheet names.
  91. REM Default output folder is the folder the workbook is in.
  92.  
  93. Sub TSV_export(optional outputfolder)
  94. if isMissing(outputfolder) then
  95.     outputfolder = GetPath()
  96. end if
  97. Doc = ThisComponent
  98. Dim Propval(1) as New com.sun.star.beans.PropertyValue
  99. REM 9 is ASCII value for tab
  100. REM 0 is for no text delimiter
  101. REM 76 is unicode (UTF-8), 60 is cp932 (SHIFT-JIS)
  102. REM 1 is first line to import
  103. Propval(0).Name = "FilterName"
  104. Propval(0).Value = "Text - txt - csv (StarCalc)"
  105. Propval(1).Name = "FilterOptions"
  106. Propval(1).Value ="9,0,76,1"   'ASCII  44 = ,  34 = "
  107.  
  108. for SheetNum = 0 to Doc.Sheets.Count - 1
  109.     Sheet = Doc.Sheets(SheetNum)
  110.     Doc.CurrentController.setActiveSheet(Sheet)
  111.     FileName = outputfolder + Sheet.Name + ".tsv"
  112.     FileURL = convertToURL(FileName)
  113.     Doc.StoreToURL(FileURL, Propval())
  114. next SheetNum
  115. End Sub
  116.  
  117. Function GetPath as string
  118. path = ThisComponent.getURL()
  119. for i = len(path) - 1 to 1 step -1
  120.     if mid(path, i, 1) = "/" then
  121.         exit for
  122.     end if
  123. next i
  124. GetPath = left(path,i)
  125. End Function
Add Comment
Please, Sign In to add comment