Internet.xba 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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="Internet" script:language="StarBasic">REM ***** BASIC *****
  21. Option Explicit
  22. Public sNewSheetName as String
  23. Function CheckHistoryControls()
  24. Dim bLocGoOn as Boolean
  25. Dim Firstdate as Date
  26. Dim LastDate as Date
  27. LastDate = CDateFromUNODate(StockRatesModel.txtEndDate.Date)
  28. FirstDate = CDateFromUNODate(StockRatesModel.txtStartDate.Date)
  29. bLocGoOn = FirstDate &lt;&gt; 0 And LastDate &lt;&gt; 0
  30. If bLocGoOn Then
  31. If FirstDate &gt;= LastDate Then
  32. Msgbox(sMsgStartDatebeforeEndDate,16, sProductname)
  33. bLocGoOn = False
  34. End If
  35. End If
  36. CheckHistoryControls = bLocGoon
  37. End Function
  38. Sub InsertCompanyHistory()
  39. Dim StockName as String
  40. Dim CurRow as Integer
  41. Dim sMsgInternetError as String
  42. Dim CurRate as Double
  43. Dim oCell as Object
  44. Dim sStockID as String
  45. Dim ChartSource as String
  46. If CheckHistoryControls() Then
  47. StartDate = CDateFromUNODate(StockRatesModel.txtStartDate.Date)
  48. EndDate = CDateFromUNODate(StockRatesModel.txtEndDate.Date)
  49. DlgStockRates.EndExecute()
  50. If StockRatesModel.optDaily.State = 1 Then
  51. sInterval = &quot;d&quot;
  52. iStep = 1
  53. ElseIf StockRatesModel.optWeekly.State = 1 Then
  54. sInterval = &quot;w&quot;
  55. iStep = 7
  56. StartDate = StartDate - WeekDay(StartDate) + 2
  57. EndDate = EndDate - WeekDay(EndDate) + 2
  58. End If
  59. iEndDay = Day(EndDate)
  60. iEndMonth = Month(EndDate)
  61. iEndYear = Year(EndDate)
  62. iStartDay = Day(StartDate)
  63. iStartMonth = Month(StartDate)
  64. iStartYear = Year(StartDate)
  65. &apos; oDocument.AddActionLock()
  66. UnprotectSheets(oSheets)
  67. InitializeStatusline(&quot;&quot;, 10, 1)
  68. oBackGroundSheet = oSheets.GetbyName(&quot;Background&quot;)
  69. StockName = DlgStockRates.GetControl(&quot;lstStockNames&quot;).GetSelectedItem()
  70. CurRow = GetStockRowIndex(Stockname)
  71. sStockID = oFirstSheet.GetCellByPosition(SBCOLUMNID1, CurRow).String
  72. ChartSource = ReplaceString(HistoryChartSource, sStockID, &quot;&lt;StockID&gt;&quot;)
  73. ChartSource = ReplaceString(ChartSource, iStartDay, &quot;&lt;StartDay&gt;&quot;)
  74. ChartSource = ReplaceString(ChartSource, cStr(iStartMonth-1), &quot;&lt;StartMonth&gt;&quot;)
  75. ChartSource = ReplaceString(ChartSource, iStartYear, &quot;&lt;StartYear&gt;&quot;)
  76. ChartSource = ReplaceString(ChartSource, iEndDay, &quot;&lt;EndDay&gt;&quot;)
  77. ChartSource = ReplaceString(ChartSource, cStr(iEndMonth-1), &quot;&lt;EndMonth&gt;&quot;)
  78. ChartSource = ReplaceString(ChartSource, iEndYear, &quot;&lt;EndYear&gt;&quot;)
  79. ChartSource = ReplaceString(ChartSource, sInterval, &quot;&lt;interval&gt;&quot;)
  80. oStatusLine.SetValue(2)
  81. If GetCurrentRate(ChartSource, CurRate, 1) Then
  82. oStatusLine.SetValue(8)
  83. UpdateValue(StockName, Today, CurRate)
  84. oStatusLine.SetValue(9)
  85. UpdateChart(StockName)
  86. oStatusLine.SetValue(10)
  87. Else
  88. sMsgInternetError = Stockname &amp; &quot;: &quot; &amp; sNoInternetDataAvailable &amp; chr(13) &amp; sCheckInternetSettings
  89. Msgbox(sMsgInternetError, 16, sProductname)
  90. End If
  91. ProtectSheets(oSheets)
  92. oStatusLine.End
  93. If oSheets.HasbyName(sNewSheetName) Then
  94. oController.ActiveSheet = oSheets.GetByName(sNewSheetName)
  95. End If
  96. &apos; oDocument.RemoveActionLock()
  97. End If
  98. End Sub
  99. Sub InternetUpdate()
  100. Dim i as Integer
  101. Dim StocksCount as Integer
  102. Dim iStartRow as Integer
  103. Dim sUrl as String
  104. Dim StockName as String
  105. Dim CurRate as Double
  106. Dim oCell as Object
  107. Dim sMsgInternetError as String
  108. Dim sStockID as String
  109. Dim ChartSource as String
  110. &apos; oDocument.AddActionLock()
  111. Initialize(True)
  112. UnprotectSheets(oSheets)
  113. StocksCount = GetStocksCount(iStartRow)
  114. InitializeStatusline(&quot;&quot;, StocksCount + 1, 1)
  115. Today = CDate(Date)
  116. For i = iStartRow + 1 To iStartRow + StocksCount
  117. StockName = oFirstSheet.GetCellbyPosition(SBCOLUMNNAME1, i).String
  118. sStockID = oFirstSheet.GetCellByPosition(SBCOLUMNID1, i).String
  119. ChartSource = ReplaceString(sCurChartSource, sStockID, &quot;&lt;StockID&gt;&quot;)
  120. If GetCurrentRate(ChartSource, CurRate, 0) Then
  121. InsertCurrentValue(CurRate, i, Now)
  122. Else
  123. sMsgInternetError = Stockname &amp; &quot;: &quot; &amp; sNoInternetDataAvailable &amp; chr(13) &amp; sCheckInternetSettings
  124. Msgbox(sMsgInternetError, 16, sProductname)
  125. End If
  126. oStatusline.SetValue(i - iStartRow + 1)
  127. Next
  128. ProtectSheets(oSheets)
  129. oStatusLine.End
  130. &apos; oDocument.RemoveActionLock
  131. End Sub
  132. Function GetCurrentRate(sUrl as String, fValue As Double, iValueRow as Integer) as Boolean
  133. Dim sFilter As String
  134. Dim sOptions As String
  135. Dim oLinkSheet As Object
  136. Dim sDate as String
  137. If oSheets.hasByName(&quot;Link&quot;) Then
  138. oLinkSheet = oSheets.getByName(&quot;Link&quot;)
  139. Else
  140. oLinkSheet = oDocument.createInstance(&quot;com.sun.star.sheet.Spreadsheet&quot;)
  141. oSheets.insertByName(&quot;Link&quot;, oLinkSheet)
  142. oLinkSheet.IsVisible = False
  143. End If
  144. sFilter = &quot;Text - txt - csv (StarCalc)&quot;
  145. sOptions = sCurSeparator &amp; &quot;,34,SYSTEM,1,1/10/2/10/3/10/4/10/5/10/6/10/7/10/8/10/9/10&quot;
  146. oLinkSheet.LinkMode = com.sun.star.sheet.SheetLinkMode.NONE
  147. oLinkSheet.link(sUrl, &quot;&quot;, sFilter, sOptions, 1 )
  148. fValue = oLinkSheet.getCellByPosition(iValueCol, iValueRow).Value
  149. If fValue = 0 Then
  150. Dim sValue as String
  151. sValue = oLinkSheet.getCellByPosition(1, iValueRow).String
  152. sValue = ReplaceString(sValue, &quot;.&quot;,&quot;,&quot;)
  153. fValue = Val(sValue)
  154. End If
  155. GetCurrentRate = fValue &lt;&gt; 0
  156. End Function
  157. Sub UpdateValue(ByVal sName As String, fDate As Double, fValue As Double )
  158. Dim oSheet As Object
  159. Dim iColumn As Long
  160. Dim iRow As Long
  161. Dim i as Long
  162. Dim oCell As Object
  163. Dim LastDate as Date
  164. Dim bLeaveLoop as Boolean
  165. Dim RemoveCount as Long
  166. Dim iLastRow as Long
  167. Dim iLastLinkRow as Long
  168. Dim dDate as Date
  169. Dim CurDate as Date
  170. Dim oLinkSheet as Object
  171. Dim StartIndex as Long
  172. Dim iCellValue as Long
  173. &apos; Insert Sheet with Company - Chart
  174. sName = CheckNewSheetname(oSheets, sName)
  175. If NOT oSheets.hasByName(sName) Then
  176. oSheets.CopybyName(&quot;Background&quot;, sName, oSheets.Count)
  177. oSheet = oSheets.getByName(sName)
  178. iCurRow = SBSTARTROW
  179. iMaxRow = iCurRow
  180. oCell = oSheet.getCellByPosition(SBDATECOLUMN, iCurRow)
  181. oCell.Value = fDate
  182. End If
  183. sNewSheetName = sName
  184. oLinkSheet = oSheets.GetByName(&quot;Link&quot;)
  185. oSheet = oSheets.getByName(sName)
  186. iLastRow = GetLastUsedRow(oSheet)- 2
  187. iLastLinkRow = GetLastUsedRow(oLinkSheet)
  188. iCurRow = iLastRow
  189. bLeaveLoop = False
  190. RemoveCount = 0
  191. &apos; Delete all Cells in Date Area
  192. Do
  193. oCell = oSheet.GetCellbyPosition(SBDATECOLUMN,iCurRow)
  194. If oCell.CellStyle = sColumnHeader Then
  195. bLeaveLoop = True
  196. StartIndex = iCurRow
  197. iCurRow = iCurRow + 1
  198. Else
  199. RemoveCount = RemoveCount + 1
  200. iCurRow = iCurRow - 1
  201. End If
  202. Loop Until bLeaveLoop
  203. If RemoveCount &gt; 1 Then
  204. oSheet.Rows.RemoveByIndex(iCurRow, RemoveCount-1)
  205. End If
  206. For i = 1 To iLastLinkRow
  207. oCell = oSheet.GetCellbyPosition(SBDATECOLUMN,iCurRow)
  208. iCellValue = oLinkSheet.GetCellByPosition(0,i).Value
  209. If iCellValue &gt; 0 Then
  210. oCell.SetValue(oLinkSheet.GetCellByPosition(0,i).Value)
  211. Else
  212. oCell.SetValue(StringToDate(oLinkSheet.GetCellByPosition(0,i).String))
  213. End If
  214. oCell = oSheet.GetCellbyPosition(SBVALUECOLUMN,iCurRow)
  215. oCell.SetValue(oLinkSheet.GetCellByPosition(4,i).Value)
  216. If i &lt; iLastLinkRow Then
  217. iCurRow = iCurRow + 1
  218. oSheet.Rows.InsertByIndex(iCurRow,1)
  219. End If
  220. Next i
  221. iMaxRow = iCurRow
  222. End Sub
  223. Function StringToDate(DateString as String) as Date
  224. Dim ShortMonths(11)
  225. Dim DateList() as String
  226. Dim MaxIndex as Integer
  227. Dim i as Integer
  228. ShortMonths(0) = &quot;Jan&quot;
  229. ShortMonths(1) = &quot;Feb&quot;
  230. ShortMonths(2) = &quot;Mar&quot;
  231. ShortMonths(3) = &quot;Apr&quot;
  232. ShortMonths(4) = &quot;May&quot;
  233. ShortMonths(5) = &quot;Jun&quot;
  234. ShortMonths(6) = &quot;Jul&quot;
  235. ShortMonths(7) = &quot;Aug&quot;
  236. ShortMonths(8) = &quot;Sep&quot;
  237. ShortMonths(9) = &quot;Oct&quot;
  238. ShortMonths(10) = &quot;Nov&quot;
  239. ShortMonths(11) = &quot;Dec&quot;
  240. For i = 0 To 11
  241. DateString = ReplaceString(DateString,CStr(i+1),ShortMonths(i))
  242. Next i
  243. DateString = ReplaceString(DateString, &quot;.&quot;, &quot;-&quot;)
  244. StringToDate = CDate(DateString)
  245. End Function
  246. Sub UpdateChart(sName As String)
  247. Dim oSheet As Object
  248. Dim oCell As Object, oCursor As Object
  249. Dim oChartRange As Object
  250. Dim oEmbeddedChart As Object, oCharts As Object
  251. Dim oChart As Object, oDiagram As Object
  252. Dim oYAxis As Object, oXAxis As Object
  253. Dim fMin As Double, fMax As Double
  254. Dim nDateFormat As Long
  255. Dim aPos As Variant
  256. Dim aSize As Variant
  257. Dim oContainerChart as Object
  258. Dim mRangeAddresses(0) as New com.sun.star.table.CellRangeAddress
  259. mRangeAddresses(0).Sheet = GetSheetIndex(oSheets, sNewSheetName)
  260. mRangeAddresses(0).StartColumn = SBDATECOLUMN
  261. mRangeAddresses(0).StartRow = SBSTARTROW-1
  262. mRangeAddresses(0).EndColumn = SBVALUECOLUMN
  263. mRangeAddresses(0).EndRow = iMaxRow
  264. oSheet = oDocument.Sheets.getByName(sNewSheetName)
  265. oCharts = oSheet.Charts
  266. If Not oCharts.hasElements Then
  267. oSheet.GetCellbyPosition(2,2).SetString(sName)
  268. oChartRange = oSheet.getCellRangeByPosition(SBDATECOLUMN,6,5,SBSTARTROW-3)
  269. aPos = oChartRange.Position
  270. aSize = oChartRange.Size
  271. Dim oRectangleShape As New com.sun.star.awt.Rectangle
  272. oRectangleShape.X = aPos.X
  273. oRectangleShape.Y = aPos.Y
  274. oRectangleShape.Width = aSize.Width
  275. oRectangleShape.Height = aSize.Height
  276. oCharts.addNewByName(sName, oRectangleShape, mRangeAddresses(), True, False)
  277. oContainerChart = oCharts.getByName(sName)
  278. oChart = oContainerChart.EmbeddedObject
  279. oChart.Title.String = &quot;&quot;
  280. oChart.HasLegend = False
  281. oChart.diagram = oChart.createInstance(&quot;com.sun.star.chart.XYDiagram&quot;)
  282. oDiagram = oChart.Diagram
  283. oDiagram.DataRowSource = com.sun.star.chart.ChartDataRowSource.COLUMNS
  284. oChart.Area.LineStyle = com.sun.star.drawing.LineStyle.SOLID
  285. oXAxis = oDiagram.XAxis
  286. oXAxis.TextBreak = False
  287. nDateFormat = oXAxis.NumberFormats.getStandardFormat(com.sun.star.util.NumberFormat.DATE, oDocLocale)
  288. oYAxis = oDiagram.getYAxis()
  289. oYAxis.AutoOrigin = True
  290. Else
  291. oChart = oCharts(0)
  292. oChart.Ranges = mRangeAddresses()
  293. oChart.HasRowHeaders = False
  294. oEmbeddedChart = oChart.EmbeddedObject
  295. oDiagram = oEmbeddedChart.Diagram
  296. oXAxis = oDiagram.XAxis
  297. End If
  298. oXAxis.AutoStepMain = False
  299. oXAxis.AutoStepHelp = False
  300. oXAxis.StepMain = iStep
  301. oXAxis.StepHelp = iStep
  302. fMin = oSheet.getCellByPosition(SBDATECOLUMN,SBSTARTROW).Value
  303. fMax = oSheet.getCellByPosition(SBDATECOLUMN,iMaxRow).Value
  304. oXAxis.Min = fMin
  305. oXAxis.Max = fMax
  306. oXAxis.AutoMin = False
  307. oXAxis.AutoMax = False
  308. End Sub
  309. Sub CalculateChartafterSplit(SheetName, NewNumber, OldNumber, NoteText, SplitDate)
  310. Dim oSheet as Object
  311. Dim i as Integer
  312. Dim oValueCell as Object
  313. Dim oDateCell as Object
  314. Dim bLeaveLoop as Boolean
  315. If oSheets.HasbyName(SheetName) Then
  316. oSheet = oSheets.GetbyName(SheetName)
  317. i = 0
  318. bLeaveLoop = False
  319. Do
  320. oValueCell = oSheet.GetCellbyPosition(SBVALUECOLUMN, SBSTARTROW + i)
  321. If oValueCell.CellStyle = CurrCellStyle Then
  322. SplitCellValue(oSheet, OldNumber, NewNumber, SBVALUECOLUMN, SBSTARTROW + i, &quot;&quot;)
  323. i = i + 1
  324. Else
  325. bLeaveLoop = True
  326. End If
  327. Loop Until bLeaveLoop
  328. oDateCell = oSheet.GetCellbyPosition(SBDATECOLUMN, SBSTARTROW + i-1)
  329. oDateCell.Annotation.SetString(NoteText)
  330. End If
  331. End Sub
  332. </script:module>