1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
4 * This file is part of the LibreOffice project.
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/.
10 * This file incorporates work covered by the following license notice:
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 .
20 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"Internet" script:
language=
"StarBasic">REM ***** BASIC *****
22 Public sNewSheetName as String
24 Function CheckHistoryControls()
25 Dim bLocGoOn as Boolean
28 LastDate = CDateFromUNODate(StockRatesModel.txtEndDate.Date)
29 FirstDate = CDateFromUNODate(StockRatesModel.txtStartDate.Date)
30 bLocGoOn = FirstDate
<> 0 And LastDate
<> 0
32 If FirstDate
>= LastDate Then
33 Msgbox(sMsgStartDatebeforeEndDate,
16, sProductname)
37 CheckHistoryControls = bLocGoon
41 Sub InsertCompanyHistory()
42 Dim StockName as String
44 Dim sMsgInternetError as String
47 Dim sStockID as String
48 Dim ChartSource as String
49 If CheckHistoryControls() Then
50 StartDate = CDateFromUNODate(StockRatesModel.txtStartDate.Date)
51 EndDate = CDateFromUNODate(StockRatesModel.txtEndDate.Date)
52 DlgStockRates.EndExecute()
53 If StockRatesModel.optDaily.State =
1 Then
54 sInterval =
"d
"
56 ElseIf StockRatesModel.optWeekly.State =
1 Then
57 sInterval =
"w
"
59 StartDate = StartDate - WeekDay(StartDate) +
2
60 EndDate = EndDate - WeekDay(EndDate) +
2
62 iEndDay = Day(EndDate)
63 iEndMonth = Month(EndDate)
64 iEndYear = Year(EndDate)
65 iStartDay = Day(StartDate)
66 iStartMonth = Month(StartDate)
67 iStartYear = Year(StartDate)
68 ' oDocument.AddActionLock()
69 UnprotectSheets(oSheets)
70 InitializeStatusline(
"",
10,
1)
71 oBackGroundSheet = oSheets.GetbyName(
"Background
")
72 StockName = DlgStockRates.GetControl(
"lstStockNames
").GetSelectedItem()
73 CurRow = GetStockRowIndex(Stockname)
74 sStockID = oFirstSheet.GetCellByPosition(SBCOLUMNID1, CurRow).String
75 ChartSource = ReplaceString(HistoryChartSource, sStockID,
"<StockID
>")
76 ChartSource = ReplaceString(ChartSource, iStartDay,
"<StartDay
>")
77 ChartSource = ReplaceString(ChartSource, cStr(iStartMonth-
1),
"<StartMonth
>")
78 ChartSource = ReplaceString(ChartSource, iStartYear,
"<StartYear
>")
79 ChartSource = ReplaceString(ChartSource, iEndDay,
"<EndDay
>")
80 ChartSource = ReplaceString(ChartSource, cStr(iEndMonth-
1),
"<EndMonth
>")
81 ChartSource = ReplaceString(ChartSource, iEndYear,
"<EndYear
>")
82 ChartSource = ReplaceString(ChartSource, sInterval,
"<interval
>")
83 oStatusLine.SetValue(
2)
84 If GetCurrentRate(ChartSource, CurRate,
1) Then
85 oStatusLine.SetValue(
8)
86 UpdateValue(StockName, Today, CurRate)
87 oStatusLine.SetValue(
9)
88 UpdateChart(StockName)
89 oStatusLine.SetValue(
10)
91 sMsgInternetError = Stockname
& ":
" & sNoInternetDataAvailable
& chr(
13)
& sCheckInternetSettings
92 Msgbox(sMsgInternetError,
16, sProductname)
94 ProtectSheets(oSheets)
96 If oSheets.HasbyName(sNewSheetName) Then
97 oController.ActiveSheet = oSheets.GetByName(sNewSheetName)
99 ' oDocument.RemoveActionLock()
107 Dim StocksCount as Integer
108 Dim iStartRow as Integer
110 Dim StockName as String
111 Dim CurRate as Double
113 Dim sMsgInternetError as String
114 Dim sStockID as String
115 Dim ChartSource as String
116 ' oDocument.AddActionLock()
118 UnprotectSheets(oSheets)
119 StocksCount = GetStocksCount(iStartRow)
120 InitializeStatusline(
"", StocksCount +
1,
1)
122 For i = iStartRow +
1 To iStartRow + StocksCount
123 StockName = oFirstSheet.GetCellbyPosition(SBCOLUMNNAME1, i).String
124 sStockID = oFirstSheet.GetCellByPosition(SBCOLUMNID1, i).String
125 ChartSource = ReplaceString(sCurChartSource, sStockID,
"<StockID
>")
126 If GetCurrentRate(ChartSource, CurRate,
0) Then
127 InsertCurrentValue(CurRate, i, Now)
129 sMsgInternetError = Stockname
& ":
" & sNoInternetDataAvailable
& chr(
13)
& sCheckInternetSettings
130 Msgbox(sMsgInternetError,
16, sProductname)
132 oStatusline.SetValue(i - iStartRow +
1)
134 ProtectSheets(oSheets)
136 ' oDocument.RemoveActionLock
141 Function GetCurrentRate(sUrl as String, fValue As Double, iValueRow as Integer) as Boolean
142 Dim sFilter As String
143 Dim sOptions As String
144 Dim oLinkSheet As Object
146 If oSheets.hasByName(
"Link
") Then
147 oLinkSheet = oSheets.getByName(
"Link
")
149 oLinkSheet = oDocument.createInstance(
"com.sun.star.sheet.Spreadsheet
")
150 oSheets.insertByName(
"Link
", oLinkSheet)
151 oLinkSheet.IsVisible = False
154 sFilter =
"Text - txt - csv (StarCalc)
"
155 sOptions = sCurSeparator
& ",
34,SYSTEM,
1,
1/
10/
2/
10/
3/
10/
4/
10/
5/
10/
6/
10/
7/
10/
8/
10/
9/
10"
157 oLinkSheet.LinkMode = com.sun.star.sheet.SheetLinkMode.NONE
158 oLinkSheet.link(sUrl,
"", sFilter, sOptions,
1 )
159 fValue = oLinkSheet.getCellByPosition(iValueCol, iValueRow).Value
162 sValue = oLinkSheet.getCellByPosition(
1, iValueRow).String
163 sValue = ReplaceString(sValue,
".
",
",
")
166 GetCurrentRate = fValue
<> 0
171 Sub UpdateValue(ByVal sName As String, fDate As Double, fValue As Double )
178 Dim bLeaveLoop as Boolean
179 Dim RemoveCount as Long
181 Dim iLastLinkRow as Long
184 Dim oLinkSheet as Object
185 Dim StartIndex as Long
186 Dim iCellValue as Long
187 ' Insert Sheet with Company - Chart
188 sName = CheckNewSheetname(oSheets, sName)
189 If NOT oSheets.hasByName(sName) Then
190 oSheets.CopybyName(
"Background
", sName, oSheets.Count)
191 oSheet = oSheets.getByName(sName)
194 oCell = oSheet.getCellByPosition(SBDATECOLUMN, iCurRow)
197 sNewSheetName = sName
198 oLinkSheet = oSheets.GetByName(
"Link
")
199 oSheet = oSheets.getByName(sName)
200 iLastRow = GetLastUsedRow(oSheet)-
2
201 iLastLinkRow = GetLastUsedRow(oLinkSheet)
205 ' Delete all Cells in Date Area
207 oCell = oSheet.GetCellbyPosition(SBDATECOLUMN,iCurRow)
208 If oCell.CellStyle = sColumnHeader Then
211 iCurRow = iCurRow +
1
213 RemoveCount = RemoveCount +
1
214 iCurRow = iCurRow -
1
216 Loop Until bLeaveLoop
217 If RemoveCount
> 1 Then
218 oSheet.Rows.RemoveByIndex(iCurRow, RemoveCount-
1)
220 For i =
1 To iLastLinkRow
221 oCell = oSheet.GetCellbyPosition(SBDATECOLUMN,iCurRow)
222 iCellValue = oLinkSheet.GetCellByPosition(
0,i).Value
223 If iCellValue
> 0 Then
224 oCell.SetValue(oLinkSheet.GetCellByPosition(
0,i).Value)
226 oCell.SetValue(StringToDate(oLinkSheet.GetCellByPosition(
0,i).String))
228 oCell = oSheet.GetCellbyPosition(SBVALUECOLUMN,iCurRow)
229 oCell.SetValue(oLinkSheet.GetCellByPosition(
4,i).Value)
230 If i
< iLastLinkRow Then
231 iCurRow = iCurRow +
1
232 oSheet.Rows.InsertByIndex(iCurRow,
1)
239 Function StringToDate(DateString as String) as Date
241 Dim DateList() as String
242 Dim MaxIndex as Integer
244 ShortMonths(
0) =
"Jan
"
245 ShortMonths(
1) =
"Feb
"
246 ShortMonths(
2) =
"Mar
"
247 ShortMonths(
3) =
"Apr
"
248 ShortMonths(
4) =
"May
"
249 ShortMonths(
5) =
"Jun
"
250 ShortMonths(
6) =
"Jul
"
251 ShortMonths(
7) =
"Aug
"
252 ShortMonths(
8) =
"Sep
"
253 ShortMonths(
9) =
"Oct
"
254 ShortMonths(
10) =
"Nov
"
255 ShortMonths(
11) =
"Dec
"
257 DateString = ReplaceString(DateString,CStr(i+
1),ShortMonths(i))
259 DateString = ReplaceString(DateString,
".
",
"-
")
260 StringToDate = CDate(DateString)
264 Sub UpdateChart(sName As String)
266 Dim oCell As Object, oCursor As Object
267 Dim oChartRange As Object
268 Dim oEmbeddedChart As Object, oCharts As Object
269 Dim oChart As Object, oDiagram As Object
270 Dim oYAxis As Object, oXAxis As Object
271 Dim fMin As Double, fMax As Double
272 Dim nDateFormat As Long
275 Dim oContainerChart as Object
276 Dim mRangeAddresses(
0) as New com.sun.star.table.CellRangeAddress
277 mRangeAddresses(
0).Sheet = GetSheetIndex(oSheets, sNewSheetName)
278 mRangeAddresses(
0).StartColumn = SBDATECOLUMN
279 mRangeAddresses(
0).StartRow = SBSTARTROW-
1
280 mRangeAddresses(
0).EndColumn = SBVALUECOLUMN
281 mRangeAddresses(
0).EndRow = iMaxRow
283 oSheet = oDocument.Sheets.getByName(sNewSheetName)
284 oCharts = oSheet.Charts
286 If Not oCharts.hasElements Then
287 oSheet.GetCellbyPosition(
2,
2).SetString(sName)
288 oChartRange = oSheet.getCellRangeByPosition(SBDATECOLUMN,
6,
5,SBSTARTROW-
3)
289 aPos = oChartRange.Position
290 aSize = oChartRange.Size
292 Dim oRectangleShape As New com.sun.star.awt.Rectangle
293 oRectangleShape.X = aPos.X
294 oRectangleShape.Y = aPos.Y
295 oRectangleShape.Width = aSize.Width
296 oRectangleShape.Height = aSize.Height
297 oCharts.addNewByName(sName, oRectangleShape, mRangeAddresses(), True, False)
298 oContainerChart = oCharts.getByName(sName)
299 oChart = oContainerChart.EmbeddedObject
300 oChart.Title.String =
""
301 oChart.HasLegend = False
302 oChart.diagram = oChart.createInstance(
"com.sun.star.chart.XYDiagram
")
303 oDiagram = oChart.Diagram
304 oDiagram.DataRowSource = com.sun.star.chart.ChartDataRowSource.COLUMNS
305 oChart.Area.LineStyle = com.sun.star.drawing.LineStyle.SOLID
306 oXAxis = oDiagram.XAxis
307 oXAxis.TextBreak = False
308 nDateFormat = oXAxis.NumberFormats.getStandardFormat(com.sun.star.util.NumberFormat.DATE, oDocLocale)
310 oYAxis = oDiagram.getYAxis()
311 oYAxis.AutoOrigin = True
314 oChart.Ranges = mRangeAddresses()
315 oChart.HasRowHeaders = False
316 oEmbeddedChart = oChart.EmbeddedObject
317 oDiagram = oEmbeddedChart.Diagram
318 oXAxis = oDiagram.XAxis
320 oXAxis.AutoStepMain = False
321 oXAxis.AutoStepHelp = False
322 oXAxis.StepMain = iStep
323 oXAxis.StepHelp = iStep
324 fMin = oSheet.getCellByPosition(SBDATECOLUMN,SBSTARTROW).Value
325 fMax = oSheet.getCellByPosition(SBDATECOLUMN,iMaxRow).Value
328 oXAxis.AutoMin = False
329 oXAxis.AutoMax = False
333 Sub CalculateChartafterSplit(SheetName, NewNumber, OldNumber, NoteText, SplitDate)
336 Dim oValueCell as Object
337 Dim oDateCell as Object
338 Dim bLeaveLoop as Boolean
339 If oSheets.HasbyName(SheetName) Then
340 oSheet = oSheets.GetbyName(SheetName)
344 oValueCell = oSheet.GetCellbyPosition(SBVALUECOLUMN, SBSTARTROW + i)
345 If oValueCell.CellStyle = CurrCellStyle Then
346 SplitCellValue(oSheet, OldNumber, NewNumber, SBVALUECOLUMN, SBSTARTROW + i,
"")
351 Loop Until bLeaveLoop
352 oDateCell = oSheet.GetCellbyPosition(SBDATECOLUMN, SBSTARTROW + i-
1)
353 oDateCell.Annotation.SetString(NoteText)