openoffice calc sumproduct with a twist

时间:2018-02-06 04:34:02

标签: openoffice-calc openoffice-basic

我在VBA中的第一次尝试,除了使用简单的功能;在这里要求启动:

假设这(一部分)表

factor      b-count c-count d-count
A2          b2      c2      d2      ...
A3          b3      c3      d3      ...

假设这些是第一列和第A1到D3行,每个都包含数值。

如果factor1,我想要A(N)(列'A',行N >= 2)来保存第1行和第N行的sumproduct。 当factor不是1时,会发生扭曲。在那种情况下,我想要一个sumproduct count*round(value * factor)

示例:

1.5         2       1       0       4
=myfunc(2)  4       8       11      15
=myfunc(3)  11      20      28      36
=myfunc(4)  29      53      74      94

其中myfunc(2)应该导致
round(4*1,5)*2+round(8*1,5)*1+round(15*1,5)*4 = 6*2+12*1+23*4 = 12+12+92 = 116,myfunc(3)= 17*2+30+54*4 = 34+30+216 = 280,myfunc(4 )= 44*2+80+141*4 = 88+80+564 = 732等。

我可以在每个下面插入一行,将每个值乘以因子;但我会喜欢更高档的东西。

基本上认为(双关语无意):

col='B'
sum=0
do while (col)(N)>0
    sum=sum+(col)(1)*round((col)(N)*A1;0)
    col=col+1
loop
A(n)=sum

其中(col)(N)指的是列col和行N中的单元格。

学习手册不够重要;但如果有人可以从袖口做到这一点,那将会很棒。

另一点:我读过自定义函数必须存储在“标准库”中;
但我怎么也找不到如何做到这一点。谁会指出我正确的手册页?

1 个答案:

答案 0 :(得分:1)

转到工具 - >宏 - >组织宏 - > OpenOffice Basic 。选择我的宏 - > config.cache_classes - >第1单元(即Standard库的含义),然后按编辑

粘贴以下代码。

Standard

在A2中输入此公式并拖动以填充到A4。

Function SumProductOfTwoRows(firstColumn As Long, row As Long, firstRow As Long)
    'For example: =SUMPRODUCTOFTWOROWS(COLUMN(); ROW(); ROW($A$1))
    firstColumn = firstColumn - 1  'column A is index 0
    row = row - 1  'row 1 is index 0
    firstRow = firstRow - 1  'row 1 is index 0
    oSheet = ThisComponent.CurrentController.ActiveSheet
    sum = 0
    column = firstColumn + 1
    factor = oSheet.getCellByPosition(firstColumn, firstRow).getValue()
    Do
        value = oSheet.getCellByPosition(column, row).getValue()
        count = oSheet.getCellByPosition(column, firstRow).getValue()
        If value = 0 Then Exit Do
        sum = sum + count * CLng(value * factor)
        column = column + 1
    Loop
    SumProductOfTwoRows = sum
End Function

结果:

116, 280, 732

这种用户定义的函数在重新打开文件时会产生错误。为避免错误,请参阅https://stackoverflow.com/a/39254907/5100564上的答案。