Hard.xba 7.7 KB

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