基本嵌套循环:过滤行

BASIC nested loops: filtering rows

我正在使用 LibreOffice Calc 存储 16 种 RGB 颜色(红色在 B 列中,绿色在 C 中,蓝色在 D 中),然后我想用每种颜色绘制 16 个单元格的背景。每种颜色在一行中。

目前我已经能够部分实现这一点:不仅绘制了 16 个单元格,而且绘制了 48 个 (16x3)!看来我需要 "group" 嵌套循环。这是我的 code/macro:

function bgcolor()

Dim Doc, Sheet, CellPaint As Object
Dim CellR, CellG, CellB As String

Doc = ThisComponent
Sheet = Doc.Sheets.getByIndex(0)

For i = 1 to 16 step 1
    For j = 1 to 3 step 1
        CellR = Sheet.getCellByPosition(j+0,i).getValue()
        CellG = Sheet.getCellByPosition(j+1,i).getValue()
        CellB = Sheet.getCellByPosition(j+2,i).getValue()
        CellPaint = Sheet.getCellByPosition(j+6,i)
        CellPaint.CellBackColor = RGB(CellR,CellG,CellB)
    next j
next i

bgcolor=CellPaint.CellBackColor

end function

这是结果:

第一列颜色 (H) 正是我所需要的。其他两列出现在那里,所以:如何修复我的嵌套循环?可能我需要使用简单的 if 语句来过滤它们,但我不确定。

EDIT - 供参考:解决方案应该类似于 this one,但是我读过它在 OpenOffice 上是不可能的(我假设在 LibreOffice 上都不可能)。

谢谢!

去掉内层循环 -- 你不需要横向迭代:

function bgcolor()

  Dim Doc, Sheet, CellPaint As Object
  Dim CellR, CellG, CellB As String

  Doc = ThisComponent
  Sheet = Doc.Sheets.getByIndex(0)

  For i = 1 to 16 step 1
    CellR = Sheet.getCellByPosition(1,i).getValue()
    CellG = Sheet.getCellByPosition(2,i).getValue()
    CellB = Sheet.getCellByPosition(3,i).getValue()
    CellPaint = Sheet.getCellByPosition(7,i)
    CellPaint.CellBackColor = RGB(CellR,CellG,CellB)
  next i

  bgcolor=CellPaint.CellBackColor

end function