Ich bin schon seit längerer Zeit auf der Suche nach einer Funktion, die es mir zuverlässig erlauben würde, den momentanen Kurswert einer Aktie (idealerweise sogar inklusive der historischen Werte) aus dem Internet abzurufen un in OpenOffice Calc zu plotten. Nach sehr langer Suche, die mir sehr viele verschiedene Makros diversester Komplexität eingebracht hat, bin ich dem Ziel leider nicht wirklich näher gekommen. Es gibt entsprechende Funktionen für Excel (das ich aber nicht besonders mag) und es gab solche Funktionen sogar mal für OpenOffice (http://stackoverflow.com/questions/9185 ... c-getquote, leider jedoch nicht mehr zugänglich), nur scheint es momentan nichts entsprechendes (zugängliches) für OpenOffice zu geben. Den besten (da einfachsten - ich bei kompletter Anfänger, wenn es um das Programmieren in OOo Basic geht) Makro poste ich mal hier (der Code ist nicht von mir, leider habe ich die Quelle verloren):
Code: Alles auswählen
REM ***** BASIC *****
Sub Main()
' Hole den Aktienkurs für die Software AG über die URL
' http://quote.yahoo.com/d/quotes.csv?s=SOW.DE&f=sl1d1t1c1ohgv&e=.csv
Print "Software AG (WKN 330400): ", GetStockPrice( "SOW.DE" )
' Hole den Aktienkurs für die United Internet AG über die URL
' http://quote.yahoo.com/d/quotes.csv?s=UTDI.DE&f=sl1d1t1c1ohgv&e=.csv
Print "United Internet AG (WKN 508903): ", GetStockPrice( "UTDI.DE" )
End Sub
'----------
' This function looks up the price of a stock symbol from yahoo.
' Pass in these parameters:
' cStockSymbol - a string, which is the stock symbol.
' Returns:
' A number which is the price of the stock.
'
Function GetStockPrice( ByVal cStockSymbol As String ) As Double
' Use this URL to get a stock price from Yahoo.
' For example, this URL...
' http://quote.yahoo.com/d/quotes.csv?s=SOW.DE&s=UTDI.DE&f=sl1d1t1c1ohgv&e=.csv
' would return a CSV file with the prices for both Software AG and United Internet AG.
' Similarly, we can form a URL for a single stock, based upon the parameter.
cURL = "http://quote.yahoo.com/d/quotes.csv?s=" + cStockSymbol + "&f=sl1d1t1c1ohgv&e=.csv"
' Open up a new spreadsheet from the above URL.
' Specify the CSV filter with options that decode the CSV format comming back from Yahoo.
' Specify the Hidden property so that the spreadsheet does not appear on the screen.
oCalcDoc = StarDesktop.loadComponentFromURL( cURL, "_blank", 0,_
Array( MakePropertyValue( "FilterName", "Text - txt - csv (StarCalc)" ),_
MakePropertyValue( "FilterOptions", "44,34,SYSTEM,1,1/10/2/10/3/10/4/10/5/10/6/10/7/10/8/10/9/10" ),_
MakePropertyValue( "Hidden", True ) ) )
' Get the first sheet of the Calc document.
oSheet = oCalcDoc.getSheets().getByIndex( 0 )
' Get the cell B1, which is the stock price. (Row 0, Col 1.)
nValue = oSheet.getCellByPosition( 1, 0 ).getValue()
' Be sure the close the spreadsheet, because it is hidden, and the user cannot close it.
oCalcDoc.close( True )
' Return the value.
GetStockPrice() = nValue
End Function
'----------
' Create and return a new com.sun.star.beans.PropertyValue.
'
Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
Dim oPropertyValue As New com.sun.star.beans.PropertyValue
If Not IsMissing( cName ) Then
oPropertyValue.Name = cName
EndIf
If Not IsMissing( uValue ) Then
oPropertyValue.Value = uValue
EndIf
MakePropertyValue() = oPropertyValue
End Function
Ich würde mich freuen, wenn mir jemand helfen könnte.
Mit freundlichen Grüßen,
Simeon
PS: Ich kenne die Möglichkeit über "Verknüpfung zu externen Daten..." Tabellen in OpenOffice Calc zu laden, würde sie aber gerne vermeiden - leider zerschießt mir diese Funktion allerdings immer die Zellenreferenzen zur Weiterverarbeitung der Daten.
PPS: Ich habe auch noch einen zweiten Code gefunden, der genau das macht, was ich will. Nur leider in Excel. Gibt es eine Möglichkeit, dass in OOoBasic zu übertragen?
Code: Alles auswählen
Function StockQuote(strTicker As String, Optional dtDate As Variant)
' Date is optional - if omitted, use today. If value is not a date, throw error.
If IsMissing(dtDate) Then
dtDate = Date
Else
If Not (IsDate(dtDate)) Then
StockQuote = CVErr(xlErrNum)
End If
End If
Dim dtPrevDate As Date
Dim strURL As String, strCSV As String, strRows() As String, strColumns() As String
Dim dbClose As Double
dtPrevDate = dtDate - 7
' Compile the request URL with start date and end date
strURL = "http://ichart.finance.yahoo.com/table.csv?s=" & strTicker & _
"&a=" & Month(dtPrevDate) - 1 & _
"&b=" & Day(dtPrevDate) & _
"&c=" & Year(dtPrevDate) & _
"&d=" & Month(dtDate) - 1 & _
"&e=" & Day(dtDate) & _
"&f=" & Year(dtDate) & _
"&g=d&ignore=.csv"
' Debug.Print strURL
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", strURL, False
http.Send
strCSV = http.responseText
' Debug.Print strCSV
' The most recent information is in row 2, just below the table headings.
' The price close is the 5th entry
strRows() = Split(strCSV, Chr(10)) ' split the CSV into rows
strColumns = Split(strRows(1), ",") ' split the relevant row into columns. 1 means 2nd row, starting at index 0
dbClose = strColumns(4) ' 4 means: 5th position, starting at index 0
' Debug.Print vbLf
' Debug.Print strRows(1)
' Debug.Print "dbClose: " & dbClose
StockQuote = dbClose
Set http = Nothing
End Function