[英]Storing dates in an array in Libreoffice Basic
在过去的几天里,我一直在研究这个问题,但没有找到答案。 我有一个交易清单,可从中过滤一个子集。 对于子集,我将日期和金额存储在数组中,以用于XIRR计算。
输入列已使用'IsDate'测试,并且为True。 此外,在存储日期时,我使用“ ReDim保留(0至n)作为日期”。 但是,在测试结果时,数组不包含日期,但包含双打。
请参考下面的代码。
Function FundXIRRTotal(ByVal FundISIN As String, ByVal KiesDatum As Date) As Double
Dim wshT As Object
Dim wshS As Object
Dim wshP As Object
Dim wsh As Object
Dim LastRow As Long
Dim i As Long
Dim NumberOfTrans As Long
Dim Guess As Double
Dim arrValues() As Double
Dim arrDates() As Date
Dim oFA As Object
Set wshT = ThisComponent.Sheets.getByName("Transacties")
Set wshP = ThisComponent.Sheets.getByName("Portefeuille")
Set wshS = ThisComponent.Sheets.getByName("Instellingen")
'Find Last Used Row
wsh = wshT.createCursor
wsh.gotoEndOfUsedArea( False )
LastRow = wsh.getRangeAddress().EndRow
NumberOfTrans = 0
'Create Transaction Array
For i = 1 To LastRow
If wshT.getCellByPosition(3, i).String = FundISIN And CDate(wshT.getCellByPosition(0, i).Value) <= KiesDatum Then
ReDim Preserve arrValues(0 To NumberOfTrans) As Double
ReDim Preserve arrDates(0 To NumberOfTrans) As Date
arrDates(NumberOfTrans) = CDate(wshT.getCellByPosition(0, i).Value)
If wshT.getCellByPosition(1, i).String = wshS.getCellRangeByName("FundBuy").String Then
arrValues(NumberOfTrans) = -CDbl(wshT.getCellByPosition(9, i).Value / wshT.getCellByPosition(10, i).Value)
End If
If wshT.getCellByPosition(1, i).String = wshS.getCellRangeByName("FundSell").String Then
arrValues(NumberOfTrans) = CDbl(wshT.getCellByPosition(9, i).Value / wshT.getCellByPosition(10, i).Value)
End If
If wshT.getCellByPosition(1, i).String = wshS.getCellRangeByName("FundDeposit").String Then
arrValues(NumberOfTrans) = CDbl(wshT.getCellByPosition(9, i).Value / wshT.getCellByPosition(10, i).Value)
End If
If wshT.getCellByPosition(1, i).String = wshS.getCellRangeByName("FundDivCash").String Then
arrValues(NumberOfTrans) = CDbl(wshT.getCellByPosition(9, i).Value / wshT.getCellByPosition(10, i).Value)
End If
If wshT.getCellByPosition(1, i).String = wshS.getCellRangeByName("FundDivShares").String Then
arrValues(NumberOfTrans) = CDbl(wshT.getCellByPosition(9, i).Value / wshT.getCellByPosition(10, i).Value)
End If
If wshT.getCellByPosition(1, i).String = wshS.getCellRangeByName("FundSplit").String Then
arrValues(NumberOfTrans) = -CDbl(wshT.getCellByPosition(9, i).Value / wshT.getCellByPosition(10, i).Value)
End If
If wshT.getCellByPosition(1, i).String = wshS.getCellRangeByName("FundFee").String Then
arrValues(NumberOfTrans) = -CDbl(wshT.getCellByPosition(9, i).Value / wshT.getCellByPosition(10, i).Value)
End If
If wshT.getCellByPosition(1, i).String = wshS.getCellRangeByName("FundWarUit").String Then
arrValues(NumberOfTrans) = -CDbl(wshT.getCellByPosition(9, i).Value / wshT.getCellByPosition(10, i).Value)
End If
NumberOfTrans = NumberOfTrans + 1
End If
Next i
'Add current value to Array
'NumberOfTrans = NumberOfTrans + 1
ReDim Preserve arrValues(0 To NumberOfTrans) As Double
ReDim Preserve arrDates(0 To NumberOfTrans) As Date
arrValues(NumberOfTrans) = CDbl(Waarde(FundISIN, KiesDatum))
arrDates(NumberOfTrans) = CDate(KiesDatum)
If Portfolio(FundISIN, KiesDatum) = 0 Then
FundXIRRTotal = 0
Exit Function
End If
'Calculate XIRR
oFA = createUNOService("com.sun.star.sheet.FunctionAccess")
FundXIRRTotal = oFA.callFunction("XIRR", Array(arrValues()), Array(arrDates()))
End Function
我知道数据类型丢失的Array函数的问题。 我试图通过创建一个新的Array As Date并将其从arrDates复制到新的数组来克服这一问题。 但是没有运气,再次翻倍...
任何帮助,将不胜感激。
解决方法是,创建一个名为“ TempSheet”的工作表。 它可以被隐藏,因为它将仅用于临时处理。
在FundXIRRTotal中,将arrDates
写入arrDates
列A。然后将该单元格范围传递给oFA.callFunction()
。
以下是我用来测试此概念的工作代码。 将“ C1:C3”更改为TempSheet上包含arrDates内容的arrDates
,例如"TempSheet.A1:A" & Ubound(arrDates)
。
Function FundXIRRTotal(ByVal FundISIN As String, ByVal KiesDatum As Date) As Double
Dim wshT As Object
Dim i As Long
Dim arrValues(2, 0) As Double
Dim arrDates(2, 0) As Date
Dim args as Array
Dim oFA As Object
Set wshT = ThisComponent.Sheets.getByName("Transacties")
For i = 0 To 2
arrDates(i, 0) = wshT.getCellByPosition(2, i).Value
arrValues(i, 0) = wshT.getCellByPosition(3, i).Value
Next i
oFA = createUNOService("com.sun.star.sheet.FunctionAccess")
args = Array(arrValues, wshT.getCellRangeByName("C1:C3"))
FundXIRRTotal = oFA.callFunction("XIRR", args)
End Function
注意:我建议使用其他答案代替此答案。
数字是日期。 UNO FunctionAccess
调用将不接受OpenOffice Basic Date
类型,而是要求使用数字类型。
这是工作示例代码:
Function FundXIRRTotal(ByVal FundISIN As String, ByVal KiesDatum As Date) As Variant
Dim wshT As Object
Dim i As Long
Dim arrValues(0, 2) As Double
Dim unoDates(0, 2) As Double
Dim theDate As Double
Dim oFA As Object
Dim args(2) As Variant
Dim result As Variant
Set wshT = ThisComponent.Sheets.getByName("Transacties")
For i = 0 To 2
arrValues(0, i) = wshT.getCellByPosition(3, i).Value
'The date value will be a number, for example 42020 for 2015-01-16.
theDate = wshT.getCellByPosition(2, i).Value
unoDates(0, i) = theDate
Next i
oFA = createUNOService("com.sun.star.sheet.FunctionAccess")
args = Array(arrValues, unoDates)
result = oFA.callFunction("XIRR", args)
FundXIRRTotal = result
End Function
另请参阅https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=67996 。
顺便说一句,我也尝试使用com.sun.star.util.Date,但这似乎不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.