如果条件为真,如何循环 LibreOffice Calc 电子表格中的行范围、比较单元格值、设置单元格值和删除行

Posted

技术标签:

【中文标题】如果条件为真,如何循环 LibreOffice Calc 电子表格中的行范围、比较单元格值、设置单元格值和删除行【英文标题】:How to loop on range of rows in LibreOffice Calc spreadsheet, comparing cell values, setting cell values and deleting row, if condition is true 【发布时间】:2020-03-02 18:27:52 【问题描述】:

我的 LibreOffice Calc 电子表格中有以下要求:


ForEach Row 'r' 在选定范围内, 从范围的最后一行开始, 并一次向后(向上)移动一排,

进行一些单元格值比较,并在此基础上跳过该行, 或设置一些单元格值,并删除选定的行, 然后继续执行相同的过程,将行放在其上方。


即,

代表CellValue(Column[A], Row[r])A[r],

并表示之前(正上方)的行, 如A[r-1],

我需要做以下事情:

  FOR (r = LastRowInSelectedRange; r>1; r=r-1) 

    IF FollowingConditionsAreTrue (
      (r > 1)
      AND (A[r] IsEqualTo A[r-1])
      AND (B[r] IsEqualTo C[r-1])
      AND (E[r] IsEqualTo E[r-1])
    ) ThenDoTheFollowing 

      SET C[r-1] = C[r]
      DeleteRow(r)

     EndIF

   EndFOR

问题: 我们如何在 LibreOffice Calc 中实现这一点?

【问题讨论】:

【参考方案1】:

以下应该可以解决问题。它假定您的数据在名为Sheet1 的工作表中的A1:E10 范围内。要使用不同的范围,请更改相应的行。如果要手动选择范围,请注释掉oCellRange = oSheet.getCellRangeByName("A1:E10") 行并取消注释oCellRange = oDoc.getCurrentSelection() 行。这仅在您有一个选择而不是多个选择时才有效。

Sub iterateThroughRange()

    Dim oDoc As Object
    oDoc =  ThisComponent

    Dim oSheet As Object
    oSheet = oDoc.getSheets().getByName("Sheet1")

    Dim oCellRange As Object    
    oCellRange = oSheet.getCellRangeByName("A1:E10") 

    'The above line gets a named range. To get a slected area instead
    'comment it out and uncomment the following line:
    'oCellRange = oDoc.getCurrentSelection() 

    Dim oTableRows As Object
    oTableRows = oCellRange.getRows()

    Dim nRows As Integer
    nRows = oTableRows.getCount()

    Dim oTableColumns As Object
    oTableColumns = oCellRange.getColumns()

    Dim nColumns As Integer
    nColumns = oTableColumns.getCount() 

    Dim oThisRow As Object, oPreviousRow As Object
    Dim oThisRowData() As Variant, oPreviousRowData() As Variant
    Dim oThisRowAddress As Variant

    For r = nRows - 1 To 1 Step - 1

        oThisRow = oCellRange.getCellRangeByPosition(0, r, nColumns - 1, r)
        oThisRowData = oThisRow.getDataArray()         

        oPreviousRow = oCellRange.getCellRangeByPosition(0, r - 1, nColumns - 1, r - 1)
        oPreviousRowData = oPreviousRow.getDataArray()

        'Column A = index 0
        'Column B = index 1
        'Column C = index 2
        'Column E = index 4

        If oThisRowData(0)(0) = oPreviousRowData(0)(0) AND _
           oThisRowData(0)(1) = oPreviousRowData(0)(2) AND _
           oThisRowData(0)(4) = oPreviousRowData(0)(4) Then

            oPreviousRowData(0)(2) = oThisRowData(0)(2)
            oPreviousRow.setDataArray(oPreviousRowData)

            'The following two lines delete the range and move the cells up
            oThisRowAddress = oThisRow.getRangeAddress()
            oSheet.removeRange(oThisRowAddress, com.sun.star.sheet.CellDeleteMode.UP)

            'To delete the entire row instead of just the affected cells,
            'comment out the above two lines and uncomment the following line:
            'oTableRows.removeByIndex(r, 1)

        End If

    Next r

End Sub

【讨论】:

以上是关于如果条件为真,如何循环 LibreOffice Calc 电子表格中的行范围、比较单元格值、设置单元格值和删除行的主要内容,如果未能解决你的问题,请参考以下文章

C#语言中循环分类总结

for 循环

while循环

libreoffice calc 如果函数返回 true 而不是 value

Python:如果条件为真,则跳过 For 循环中的迭代

repeat语句(pascal)