ConvertRun.xba 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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="ConvertRun" script:language="StarBasic">Option Explicit
  21. Public oPreSelRange as Object
  22. Sub Main()
  23. BasicLibraries.LoadLibrary(&quot;Tools&quot;)
  24. If InitResources(&quot;Euro Converter&quot;) Then
  25. bDoUnProtect = False
  26. bPreSelected = True
  27. oDocument = ThisComponent
  28. RetrieveDocumentObjects() &apos; Statusline, SheetsCollection etc.
  29. InitializeConverter(oDocument.CharLocale, 1)
  30. GetPreSelectedRange()
  31. If GoOn Then
  32. DialogModel.lstCurrencies.TabIndex = 2
  33. DialogConvert.GetControl(&quot;chkComplete&quot;).SetFocus()
  34. DialogConvert.Execute
  35. End If
  36. DialogConvert.Dispose
  37. End If
  38. End Sub
  39. Sub SelectListItem()
  40. Dim Listbox as Object
  41. Dim oListSheet as Object
  42. Dim CurStyleName as String
  43. Dim oCursheet as Object
  44. Dim oTempRanges as Object
  45. Dim sCurSheetName as String
  46. Dim RangeName as String
  47. Dim oSheetRanges as Object
  48. Dim ListIndex as Integer
  49. Dim a as Integer
  50. Dim i as Integer
  51. Dim n as Integer
  52. Dim m as Integer
  53. Dim MaxIndex as Integer
  54. Listbox = DialogModel.lstSelection
  55. If Ubound(Listbox.SelectedItems()) &gt; -1 Then
  56. EnableStep1DialogControls(False, False, False)
  57. oSelRanges = oDocument.createInstance(&quot;com.sun.star.sheet.SheetCellRanges&quot;)
  58. &apos; Is the sheet the basis, then the sheetobject has to be created
  59. If DialogModel.optDocRanges.State = 1 Then
  60. &apos; Document is the basis for the conversion
  61. ListIndex = Listbox.SelectedItems(0)
  62. oCurSheet = RetrieveSheetoutofRangeName(Listbox.StringItemList(ListIndex))
  63. oDocument.CurrentController.SetActiveSheet(oCurSheet)
  64. Else
  65. oCurSheet = oDocument.CurrentController.ActiveSheet
  66. End If
  67. sCurSheetName = oCurSheet.Name
  68. If DialogModel.optCellTemplates.State = 1 Then
  69. Dim CurIndex as Integer
  70. For i = 0 To Ubound(Listbox.SelectedItems())
  71. CurIndex = Listbox.SelectedItems(i)
  72. CurStylename = Listbox.StringItemList(CurIndex)
  73. oSheetRanges = oCursheet.CellFormatRanges.createEnumeration
  74. While oSheetRanges.hasMoreElements
  75. oRange = oSheetRanges.NextElement
  76. If oRange.getPropertyState(&quot;NumberFormat&quot;) = 1 Then
  77. If oRange.CellStyle = CurStyleName Then
  78. oSelRanges.InsertbyName(&quot;&quot;,oRange)
  79. End If
  80. End If
  81. Wend
  82. Next i
  83. Else
  84. &apos; Hard Formatation is selected
  85. a = -1
  86. For n = 0 To Ubound(Listbox.SelectedItems())
  87. m = Listbox.SelectedItems(n)
  88. RangeName = Listbox.StringItemList(m)
  89. oListSheet = RetrieveSheetoutofRangeName(RangeName)
  90. a = a + 1
  91. MaxIndex = Ubound(SelRangeList())
  92. If a &gt; MaxIndex Then
  93. Redim Preserve SelRangeList(MaxIndex + SBRANGEUBOUND)
  94. End If
  95. SelRangeList(a) = RangeName
  96. If oListSheet.Name = sCurSheetName Then
  97. oRange = RetrieveRangeoutofRangeName(RangeName)
  98. oSelRanges.InsertbyName(&quot;&quot;,oRange)
  99. End If
  100. Next n
  101. End If
  102. If a &gt; -1 Then
  103. ReDim Preserve SelRangeList(a)
  104. Else
  105. ReDim SelRangeList()
  106. End If
  107. oDocument.CurrentController.Select(oSelRanges)
  108. EnableStep1DialogControls(True, True, True)
  109. End If
  110. End Sub
  111. &apos; Procedure that is called by an event
  112. Sub RetrieveEnableValue()
  113. Dim EnableValue as Boolean
  114. EnableValue = Not DialogModel.lstSelection.Enabled
  115. EnableStep1DialogControls(True, EnableValue, True)
  116. End Sub
  117. Sub EnableStep1DialogControls(bCurrEnabled as Boolean, bFrameEnabled as Boolean, bButtonsEnabled as Boolean)
  118. Dim bCurrIsSelected as Boolean
  119. Dim bObjectIsSelected as Boolean
  120. Dim bConvertWholeDoc as Boolean
  121. Dim bDoEnableFrame as Boolean
  122. bConvertWholeDoc = DialogModel.chkComplete.State = 1
  123. bDoEnableFrame = bFrameEnabled And (NOT bConvertWholeDoc)
  124. &apos; Controls around the Selection Listbox
  125. With DialogModel
  126. .lblCurrencies.Enabled = bCurrEnabled
  127. .lstCurrencies.Enabled = bCurrEnabled
  128. .lstSelection.Enabled = bDoEnableFrame
  129. .lblSelection.Enabled = bDoEnableFrame
  130. .hlnSelection.Enabled = bDoEnableFrame
  131. .optCellTemplates.Enabled = bDoEnableFrame
  132. .optSheetRanges.Enabled = bDoEnableFrame
  133. .optDocRanges.Enabled = bDoEnableFrame
  134. .optSelRange.Enabled = bDoEnableFrame
  135. End With
  136. &apos; The CheckBox has the Value &apos;1&apos; when the Controls in the Frame are disabled
  137. If bButtonsEnabled Then
  138. bCurrIsSelected = Ubound(DialogModel.lstCurrencies.SelectedItems()) &lt;&gt; -1
  139. &apos; Enable GoOnButton only when Currency is selected
  140. DialogModel.cmdGoOn.Enabled = bCurrIsSelected
  141. DialogModel.chkComplete.Enabled = bCurrIsSelected
  142. If bDoEnableFrame AND DialogModel.cmdGoOn.Enabled Then
  143. &apos; If FrameControls are enabled, check if Listbox is Empty
  144. bObjectIsSelected = Ubound(DialogModel.lstSelection.SelectedItems()) &lt;&gt; -1
  145. DialogModel.cmdGoOn.Enabled = bObjectIsSelected
  146. End If
  147. Else
  148. DialogModel.cmdGoOn.Enabled = False
  149. DialogModel.chkComplete.Enabled = False
  150. End If
  151. End Sub
  152. Sub ConvertRangesOrStylesOfDocument()
  153. Dim i as Integer
  154. Dim ItemName as String
  155. Dim SelList() as String
  156. Dim oSheetRanges as Object
  157. bDocHasProtectedSheets = CheckSheetProtection(oSheets)
  158. If bDocHasProtectedSheets Then
  159. bDocHasProtectedSheets = UnprotectSheetsWithPassWord(oSheets, bDoUnProtect)
  160. DialogModel.cmdGoOn.Enabled = False
  161. End If
  162. If Not bDocHasProtectedSheets Then
  163. EnableStep1DialogControls(False, False, False)
  164. InitializeProgressBar()
  165. If DialogModel.optSelRange.State = 1 Then
  166. SelectListItem()
  167. End If
  168. SelList() = DialogConvert.GetControl(&quot;lstSelection&quot;).SelectedItems()
  169. If DialogModel.optCellTemplates.State = 1 Then
  170. &apos; Option &apos;Soft&apos; Formatation is selected
  171. AssignRangestoStyle(DialogModel.lstSelection.StringItemList(), SelList())
  172. ConverttheSoftWay(SelList(), True)
  173. ElseIf DialogModel.optSelRange.State = 1 Then
  174. oSheetRanges = oPreSelRange.CellFormatRanges.createEnumeration
  175. While oSheetRanges.hasMoreElements
  176. oRange = oSheetRanges.NextElement
  177. If CheckFormatType(oRange) Then
  178. ConvertCellCurrencies(oRange)
  179. SwitchNumberFormat(oRange, oFormats, sEuroSign)
  180. End If
  181. Wend
  182. Else
  183. ConverttheHardWay(SelList(), False, True)
  184. End If
  185. oStatusline.End
  186. EnableStep1DialogControls(True, False, True)
  187. DialogModel.cmdGoOn.Enabled = True
  188. oDocument.CurrentController.Select(oSelRanges)
  189. End If
  190. End Sub
  191. Sub ConvertWholeDocument()
  192. Dim s as Integer
  193. DialogModel.cmdGoOn.Enabled = False
  194. DialogModel.chkComplete.Enabled = False
  195. GoOn = ConvertDocument()
  196. EmptyListbox(DialogModel.lstSelection())
  197. EnableStep1DialogControls(True, True, True)
  198. End Sub
  199. &apos; Everything previously selected will be deselected
  200. Sub EmptySelection()
  201. Dim RangeName as String
  202. Dim i as Integer
  203. Dim MaxIndex as Integer
  204. Dim EmptySelRangeList() as String
  205. If Not IsNull(oSelRanges) Then
  206. If oSelRanges.HasElements Then
  207. EmptySelRangeList() = ArrayOutofString(oSelRanges.RangeAddressesasString, &quot;;&quot;, MaxIndex)
  208. For i = 0 To MaxIndex
  209. oSelRanges.RemovebyName(EmptySelRangeList(i))
  210. Next i
  211. End If
  212. oDocument.CurrentController.Select(oSelRanges)
  213. Else
  214. oSelRanges = oDocument.createInstance(&quot;com.sun.star.sheet.SheetCellRanges&quot;)
  215. End If
  216. End Sub
  217. Function AddSelectedRangeToSelRangesEnum() as Object
  218. Dim oLocRange as Object
  219. osheet = oDocument.CurrentController.GetActiveSheet
  220. oSelRanges = oDocument.createInstance(&quot;com.sun.star.sheet.SheetCellRanges&quot;)
  221. &apos; Check if a Currency-Range has been selected
  222. oLocRange = oDocument.CurrentController.Selection
  223. bPreSelected = oLocRange.SupportsService(&quot;com.sun.star.sheet.SheetCellRange&quot;)
  224. If bPreSelected Then
  225. oSelRanges.InsertbyName(&quot;&quot;,oLocRange)
  226. AddSelectedRangeToSelRangesEnum() = oLocRange
  227. End If
  228. End Function
  229. Sub GetPreSelectedRange()
  230. Dim i as Integer
  231. Dim OldCurrSymbolList(2) as String
  232. Dim OldCurrIndex as Integer
  233. Dim OldCurExtension(2) as String
  234. oPreSelRange = AddSelectedRangeToSelRangesEnum()
  235. DialogModel.chkComplete.State = Abs(Not(bPreSelected))
  236. If bPreSelected Then
  237. DialogModel.optSelRange.State = 1
  238. AddRangeToListbox(oPreSelRange)
  239. Else
  240. DialogModel.optCellTemplates.State = 1
  241. CreateStyleEnumeration()
  242. End If
  243. EnableStep1DialogControls(True, bPreSelected, True)
  244. DialogModel.optSelRange.Enabled = bPreSelected
  245. End Sub
  246. Sub AddRangeToListbox(oLocRange as Object)
  247. EmptyListBox(DialogModel.lstSelection)
  248. PreName = RetrieveRangeNamefromAddress(oLocRange)
  249. AddSingleItemToListbox(DialogModel.lstSelection, Prename)&apos;, 0)
  250. SelectListboxItem(DialogModel.lstCurrencies, CurrIndex)
  251. TotCellCount = CountRangeCells(oLocRange)
  252. End Sub
  253. Sub CheckRangeSelection(Optional oEvent)
  254. EmptySelection()
  255. AddRangeToListbox(oPreSelRange)
  256. oPreSelRange = AddSelectedRangeToSelRangesEnum()
  257. End Sub
  258. &apos; Checks if a Field (LocField) is already defined in an Array
  259. &apos; Returns &apos;True&apos; or &apos;False&apos;
  260. Function FieldInList(LocList(), MaxIndex as integer, ByVal LocField ) As Boolean
  261. Dim i as integer
  262. LocField = UCase(LocField)
  263. For i = Lbound(LocList()) to MaxIndex
  264. If UCase(LocList(i)) = LocField then
  265. FieldInList = True
  266. Exit Function
  267. End if
  268. Next
  269. FieldInList = False
  270. End Function
  271. Function CheckLocale(oLocale) as Boolean
  272. Dim i as Integer
  273. Dim LocCountry as String
  274. Dim LocLanguage as String
  275. LocCountry = oLocale.Country
  276. LocLanguage = oLocale.Language
  277. For i = 0 To 1
  278. If LocLanguage = LangIDValue(CurrIndex,i,0) AND LocCountry = LangIDValue(CurrIndex,i,1) Then
  279. CheckLocale = True
  280. Exit Function
  281. End If
  282. Next i
  283. CheckLocale = False
  284. End Function
  285. Sub SetOptionValuestoNull()
  286. With DialogModel
  287. .optCellTemplates.State = 0
  288. .optSheetRanges.State = 0
  289. .optDocRanges.State = 0
  290. .optSelRange.State = 0
  291. End With
  292. End Sub
  293. Sub SetStatusLineText(sStsREPROTECT as String)
  294. If Not IsNull(oStatusLine) Then
  295. oStatusline.SetText(sStsREPROTECT)
  296. End If
  297. End Sub
  298. </script:module>