Arrays 在libreofficebasic的数组中存储日期

Arrays 在libreofficebasic的数组中存储日期,arrays,date,macros,libreoffice,basic,Arrays,Date,Macros,Libreoffice,Basic,在过去的几天里,我一直在研究这个问题,但没有找到答案。我有一个事务列表,从中筛选一个子集。对于子集,我将日期和金额存储在数组中,用于XIRR计算 使用“IsDate”测试输入列,结果为真。此外,我在存储日期时使用“ReDim Preserve(0到n)As Date”。但是,在测试结果时,数组不包含日期,而是双精度的 请参考下面的代码 Function FundXIRRTotal(ByVal FundISIN As String, ByVal KiesDatum As Date) As Doub

在过去的几天里,我一直在研究这个问题,但没有找到答案。我有一个事务列表,从中筛选一个子集。对于子集,我将日期和金额存储在数组中,用于XIRR计算

使用“IsDate”测试输入列,结果为真。此外,我在存储日期时使用“ReDim Preserve(0到n)As Date”。但是,在测试结果时,数组不包含日期,而是双精度的

请参考下面的代码

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
函数FundXIRRTotal(ByVal FundISIN为字符串,ByVal KiesDatum为日期)为双精度
作为对象的暗淡wshT
将wshS设置为对象
将wshP调暗为对象
将wsh设置为对象
最后一排一样长
我想我会坚持多久
暗数等于长数
我猜是双重的
将值()设置为双精度
日期()作为日期
作为物体的物体
Set wshT=ThisComponent.Sheets.getByName(“交易”)
设置wshP=ThisComponent.Sheets.getByName(“portefueille”)
设置wshS=ThisComponent.Sheets.getByName(“Instellingen”)
'查找最后使用的行
wsh=wshT.createCursor
wsh.gotoEndOfUsedArea(错误)
LastRow=wsh.getRangeAddress().EndRow
NumberOfTrans=0
'创建事务数组
对于i=1到最后一行

如果wshT.getCellByPosition(3,i).String=FundISIN和CDate(wshT.getCellByPosition(0,i).Value)作为解决方法,请创建一个名为“TempSheet”的工作表。它可以隐藏,因为它只用于临时处理

在FundXirTotal中,将
arrDates
写入临时工作表列A。然后将该单元格范围传递到oFA.callFunction()

下面是我用来测试这个概念的工作代码。将“C1:C3”更改为包含
arrDates
内容的TempSheet上的范围,例如
“TempSheet.A1:A”&Ubound(arrDates)

函数FundXIRRTotal(ByVal FundISIN为字符串,ByVal KiesDatum为日期)为双精度
作为对象的暗淡wshT
我想我会坚持多久
将值(2,0)设置为双精度
日期(2,0)作为日期
Dim args作为数组
作为物体的物体
Set wshT=ThisComponent.Sheets.getByName(“交易”)
对于i=0到2
arrDates(i,0)=wshT.getCellByPosition(2,i).Value
arrValues(i,0)=wshT.getCellByPosition(3,i).Value
接下来我
oFA=createUNOService(“com.sun.star.sheet.FunctionAccess”)
args=数组(arrValues,wshT.getCellRangeByName(“C1:C3”))
FundXIRRTotal=oFA.callFunction(“XIRR”,args)
端函数

注意:我推荐我的另一个答案,而不是这个。

作为一种解决方法,创建一个名为“TempSheet”的工作表。它可以隐藏,因为它只用于临时处理

在FundXirTotal中,将
arrDates
写入临时工作表列A。然后将该单元格范围传递到oFA.callFunction()

下面是我用来测试这个概念的工作代码。将“C1:C3”更改为包含
arrDates
内容的TempSheet上的范围,例如
“TempSheet.A1:A”&Ubound(arrDates)

函数FundXIRRTotal(ByVal FundISIN为字符串,ByVal KiesDatum为日期)为双精度
作为对象的暗淡wshT
我想我会坚持多久
将值(2,0)设置为双精度
日期(2,0)作为日期
Dim args作为数组
作为物体的物体
Set wshT=ThisComponent.Sheets.getByName(“交易”)
对于i=0到2
arrDates(i,0)=wshT.getCellByPosition(2,i).Value
arrValues(i,0)=wshT.getCellByPosition(3,i).Value
接下来我
oFA=createUNOService(“com.sun.star.sheet.FunctionAccess”)
args=数组(arrValues,wshT.getCellRangeByName(“C1:C3”))
FundXIRRTotal=oFA.callFunction(“XIRR”,args)
端函数
注意:我推荐我的另一个答案,而不是这个。

数字是日期。UNO
FunctionAccess
调用将不接受OpenOffice Basic
Date
类型,而是需要一个数字类型

以下是工作示例代码:

函数FundXIRRTotal(ByVal FundISIN作为字符串,ByVal KiesDatum作为日期)作为变量
作为对象的暗淡wshT
我想我会坚持多久
尺寸值(0,2)为双精度
将日期(0,2)设置为双精度
使日期变暗为双倍
作为物体的物体
变光参数(2)作为变型
作为变量的模糊结果
Set wshT=ThisComponent.Sheets.getByName(“交易”)
对于i=0到2
arrValues(0,i)=wshT.getCellByPosition(3,i).Value
'日期值将是一个数字,例如2015-01-16的42020。
theDate=wshT.getCellByPosition(2,i).值
无日期(0,i)=日期
接下来我
oFA=createUNOService(“com.sun.star.sheet.FunctionAccess”)
args=数组(arrValues,unodate)
结果=oFA.callFunction(“XIRR”,args)
FundXIRRTotal=结果
端函数
另见

顺便说一下,我也试过使用,但似乎不起作用。

数字就是日期。UNO
FunctionAccess
调用将不接受OpenOffice Basic
Date
类型,而是需要一个数字类型

以下是工作示例代码:

函数FundXIRRTotal(ByVal FundISIN作为字符串,ByVal KiesDatum作为日期)作为变量
作为对象的暗淡wshT
我想我会坚持多久
尺寸值(0,2)为双精度
将日期(0,2)设置为双精度
使日期变暗为双倍
作为物体的物体
变光参数(2)作为变型
作为变量的模糊结果
Set wshT=ThisComponent.Sheets.getByName(“交易”)
对于i=0到2
arrValues(0,i)=wshT.getCellByPosition(3,i).Value
'日期值将是一个数字,例如2015-01-16的42020。
theDate=wshT.getCellByPosition(2,i).值
无日期(0,i)=日期
接下来我
oFA=createUNOService(“com.sun.star.sheet.FunctionAccess”)
args=数组(arrValues,unodate)
结果=oFA.callFunction(“XIRR”,args)
FundXIRRTotal=结果
端函数
另见


顺便说一句,我也尝试过使用,但似乎不起作用。

如果您将生成的双精度数组放入电子表格,