Hard.xba 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <!--
  4. * This file is part of the LibreOffice project.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * This file incorporates work covered by the following license notice:
  11. *
  12. * Licensed to the Apache Software Foundation (ASF) under one or more
  13. * contributor license agreements. See the NOTICE file distributed
  14. * with this work for additional information regarding copyright
  15. * ownership. The ASF licenses this file to you under the Apache
  16. * License, Version 2.0 (the "License"); you may not use this file
  17. * except in compliance with the License. You may obtain a copy of
  18. * the License at http://www.apache.org/licenses/LICENSE-2.0 .
  19. -->
  20. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Hard" script:language="StarBasic">REM ***** BASIC *****
  21. Option Explicit
  22. Sub CreateRangeList()
  23. Dim MaxIndex as Integer
  24. MaxIndex = -1
  25. EnableStep1DialogControls(False, False, False)
  26. EmptySelection()
  27. DialogModel.lblSelection.Label = sCURRRANGES
  28. EmptyListbox(DialogModel.lstSelection)
  29. oDocument.CurrentController.Select(oSelRanges)
  30. If (DialogModel.optSheetRanges.State = 1) AND (DialogModel.chkComplete.State &lt;&gt; 1) Then
  31. &apos; Conversion on a sheet?
  32. SetStatusLineText(sStsRELRANGES)
  33. osheet = oDocument.CurrentController.GetActiveSheet
  34. oRanges = osheet.CellFormatRanges.createEnumeration()
  35. MaxIndex = AddSheetRanges(oRanges, MaxIndex, oSheet, False)
  36. If MaxIndex &gt; -1 Then
  37. ReDim Preserve RangeList(MaxIndex)
  38. End If
  39. Else
  40. CreateRangeEnumeration(False)
  41. bRangeListDefined = True
  42. End If
  43. EnableStep1DialogControls(True, True, True)
  44. SetStatusLineText(&quot;&quot;)
  45. End Sub
  46. Sub CreateRangeEnumeration(bAutopilot as Boolean)
  47. Dim i as Integer
  48. Dim MaxIndex as integer
  49. Dim sStatustext as String
  50. MaxIndex = -1
  51. If Not bRangeListDefined Then
  52. &apos; Cellranges are not yet defined
  53. oSheets = oDocument.Sheets
  54. For i = 0 To oSheets.Count-1
  55. oSheet = oSheets.GetbyIndex(i)
  56. If bAutopilot Then
  57. IncreaseStatusValue(SBRELGET/osheets.Count)
  58. Else
  59. sStatustext = ReplaceString(sStsRELSHEETRANGES,Str(i+1),&quot;%1Number%1&quot;)
  60. sStatustext = ReplaceString(sStatusText,oSheets.Count,&quot;%2TotPageCount%2&quot;)
  61. SetStatusLineText(sStatusText)
  62. End If
  63. oRanges = osheet.CellFormatRanges.createEnumeration
  64. MaxIndex = AddSheetRanges(oRanges, MaxIndex, oSheet, bAutopilot)
  65. Next i
  66. Else
  67. If Not bAutoPilot Then
  68. SetStatusLineText(sStsRELRANGES)
  69. &apos; cellranges already defined
  70. For i = 0 To Ubound(RangeList())
  71. If RangeList(i) &lt;&gt; &quot;&quot; Then
  72. AddSingleItemToListBox(DialogModel.lstSelection, RangeList(i))
  73. End If
  74. Next
  75. End If
  76. End If
  77. If MaxIndex &gt; -1 Then
  78. ReDim Preserve RangeList(MaxIndex)
  79. Else
  80. ReDim RangeList()
  81. End If
  82. Rangeindex = MaxIndex
  83. End Sub
  84. Function AddSheetRanges(oRanges as Object, r as Integer, oSheet as Object, bAutopilot)
  85. Dim RangeName as String
  86. Dim AddtoList as Boolean
  87. Dim iCurStep as Integer
  88. Dim MaxIndex as Integer
  89. iCurStep = DialogModel.Step
  90. While oRanges.hasMoreElements
  91. oRange = oRanges.NextElement
  92. AddToList = CheckFormatType(oRange)
  93. If AddToList Then
  94. RangeName = RetrieveRangeNamefromAddress(oRange)
  95. TotCellCount = TotCellCount + CountRangeCells(oRange)
  96. If Not bAutoPilot Then
  97. AddSingleItemToListbox(DialogModel.lstSelection, RangeName)
  98. End If
  99. &apos; The Ranges are only passed to an Array when the whole Document is the basis
  100. &apos; Redimension the RangeList Array if necessary
  101. MaxIndex = Ubound(RangeList())
  102. r = r + 1
  103. If r &gt; MaxIndex Then
  104. MaxIndex = MaxIndex + SBRANGEUBOUND
  105. ReDim Preserve RangeList(MaxIndex)
  106. End If
  107. RangeList(r) = RangeName
  108. End If
  109. Wend
  110. AddSheetRanges = r
  111. End Function
  112. &apos; adds a section to the collection
  113. Sub SelectRange()
  114. Dim i as Integer
  115. Dim RangeName as String
  116. Dim SelItem as String
  117. Dim CurRange as String
  118. Dim SheetRangeName as String
  119. Dim DescriptionList() as String
  120. Dim MaxRangeIndex as Integer
  121. Dim StatusValue as Integer
  122. StatusValue = 0
  123. MaxRangeIndex = Ubound(SelRangeList())
  124. CurSheetName = oSheet.Name
  125. For i = 0 To MaxRangeIndex
  126. SelItem = SelRangeList(i)
  127. &apos; Is the Range already included in the collection?
  128. oRange = RetrieveRangeoutOfRangename(SelItem)
  129. TotCellCount = TotCellCount + CountRangeCells(oRange)
  130. DescriptionList() = ArrayOutofString(SelItem,&quot;.&quot;,1)
  131. SheetRangeName = DeleteStr(DescriptionList(0),&quot;&apos;&quot;)
  132. If SheetRangeName = CurSheetName Then
  133. oSelRanges.InsertbyName(&quot;&quot;,oRange)
  134. End If
  135. IncreaseStatusValue(SBRELGET/MaxRangeIndex)
  136. Next i
  137. End Sub
  138. Sub ConvertThehardWay(ListboxList(), SwitchFormat as Boolean, bRemove as Boolean)
  139. Dim i as Integer
  140. Dim AddCells as Long
  141. Dim OldStatusValue as Single
  142. Dim RangeName as String
  143. Dim LastIndex as Integer
  144. Dim oSelListbox as Object
  145. oSelListbox = DialogConvert.GetControl(&quot;lstSelection&quot;)
  146. Lastindex = Ubound(ListboxList())
  147. If TotCellCount &gt; 0 Then
  148. OldStatusValue = StatusValue
  149. &apos; hard format
  150. For i = 0 To LastIndex
  151. RangeName = ListboxList(i)
  152. oRange = RetrieveRangeoutofRangeName(RangeName)
  153. ConvertCellCurrencies(oRange)
  154. If bRemove Then
  155. If oSelRanges.HasbyName(RangeName) Then
  156. oSelRanges.RemovebyName(RangeName)
  157. oDocument.CurrentController.Select(oSelRanges)
  158. End If
  159. End If
  160. If SwitchFormat Then
  161. If oRange.getPropertyState(&quot;NumberFormat&quot;) &lt;&gt; 1 Then
  162. &apos; Range is hard formatted
  163. SwitchNumberFormat(oRange, oFormats, sEuroSign)
  164. End If
  165. Else
  166. SwitchNumberFormat(oRange, oFormats, sEuroSign)
  167. End If
  168. AddCells = CountRangeCells(oRange)
  169. CurCellCount = AddCells
  170. IncreaseStatusValue((CurCellCount/TotCellCount)*(100-OldStatusValue))
  171. If bRemove Then
  172. RemoveListBoxItemByName(oSelListbox.Model,Rangename)
  173. End If
  174. Next
  175. End If
  176. End Sub
  177. Sub ConvertCellCurrencies(oRange as Object)
  178. Dim oValues as Object
  179. Dim oCells as Object
  180. Dim oCell as Object
  181. oValues = oRange.queryContentCells(com.sun.star.sheet.CellFlags.VALUE)
  182. If (oValues.Count &gt; 0) Then
  183. oCells = oValues.Cells.createEnumeration
  184. While oCells.hasMoreElements
  185. oCell = oCells.nextElement
  186. ModifyObjectValuewithCurrFactor(oCell)
  187. Wend
  188. End If
  189. End Sub
  190. Sub ModifyObjectValuewithCurrFactor(oDocObject as Object)
  191. Dim oDocObjectValue as double
  192. oDocObjectValue = oDocObject.Value
  193. oDocObject.Value = Round(oDocObjectValue/CurrFactor, 2)
  194. End Sub
  195. Function CheckIfRangeisCurrency(FormatObject as Object)
  196. Dim oFormatofObject() as Object
  197. &apos; Retrieve the Format of the Object
  198. On Local Error GoTo NOKEY
  199. oFormatofObject() = oFormats.getByKey(FormatObject.NumberFormat)
  200. On Local Error GoTo 0
  201. CheckIfRangeIsCurrency = INT(oFormatofObject.Type) AND com.sun.star.util.NumberFormat.CURRENCY
  202. Exit Function
  203. NOKEY:
  204. CheckIfRangeisCurrency = False
  205. Resume CLERROR
  206. CLERROR:
  207. End Function
  208. Function CountColumnsForRow(IndexArray() as String, Row as Integer)
  209. Dim i as Integer
  210. Dim NoNulls as Boolean
  211. For i = 1 To Ubound(IndexArray,2)
  212. If IndexArray(Row,i)= &quot;&quot; Then
  213. NoNulls = False
  214. Exit For
  215. End If
  216. Next
  217. CountColumnsForRow = i
  218. End Function
  219. Function CountRangeCells(oRange as Object) As Long
  220. Dim oRangeAddress as Object
  221. Dim LocCellCount as Long
  222. oRangeAddress = oRange.RangeAddress
  223. LocCellCount = (oRangeAddress.EndColumn - oRangeAddress.StartColumn + 1) * (oRangeAddress.EndRow - oRangeAddress.StartRow + 1)
  224. CountRangeCells = LocCellCount
  225. End Function</script:module>