在 libre office calc 中反转列顺序

Reverse columns order in libre office calc

假设我有这个这个细胞:

然后出于某种原因,我想自动反转列的顺序,变成如下所示:

如有任何帮助,我们将不胜感激!

这是一种使用 Libreoffice Basic 和 Libreoffice API 的方法。

sub ReverseColumns()

 oThisWorkbook = ThisComponent
 oActiveSheet = oThisWorkbook.CurrentController.ActiveSheet

 oRow1 = oActiveSheet.getRows().getByIndex(0)
 aFilledCellsRow1 = oRow1.queryContentCells(1+2+4+16).getRangeAddresses()

 if ubound(aFilledCellsRow1) = -1 then exit sub

 lLastFilledColumnRow1 = aFilledCellsRow1(ubound(aFilledCellsRow1)).EndColumn

 c = 0
 for i = lLastFilledColumnRow1 to 1 step -1
  oCellTargetColumn = oActiveSheet.getCellByPosition(c, 0)
  oRangeAddressTargetColumn = oCellTargetColumn.RangeAddress
  oActiveSheet.insertCells(oRangeAddressTargetColumn, com.sun.star.sheet.CellInsertMode.COLUMNS)

  oCellTargetColumn = oActiveSheet.getCellByPosition(c, 0)
  oCellAddressTargetColumn = oCellTargetColumn.CellAddress

  oRangeSource = oActiveSheet.Columns.getByIndex(lLastFilledColumnRow1 + 1)
  oRangeAddressSource = oRangeSource.RangeAddress
  oActiveSheet.moveRange(oCellAddressTargetColumn, oRangeAddressSource)
  c = c + 1
 next

end sub

这首先确定第 1 行中最后填充的列。然后将进行列反转过程,直到该列为止。

要了解 Libreoffice 中的宏,请从这里开始:https://wiki.documentfoundation.org/Macros

万一这里有人想颠倒(而不是列)的顺序...我使用了宏代码并对其进行了编辑就是这样:

sub ReverseRows()

  oThisWorkbook = ThisComponent
  oActiveSheet = oThisWorkbook.CurrentController.ActiveSheet

  oColumn1 = oActiveSheet.getColumns().getByIndex(0)
  aFilledCellsColumn1 = oColumn1.queryContentCells(1+2+4+16).getRangeAddresses()

  if ubound(aFilledCellsColumn1) = -1 then exit sub

  lLastFilledRowColumn1 = aFilledCellsColumn1(ubound(aFilledCellsColumn1)).EndRow

  c = 0

  for i = lLastFilledRowColumn1 to 1 step -1
    oCellTargetRow = oActiveSheet.getCellByPosition(0, c)
    oRangeAddressTargetRow = oCellTargetRow.RangeAddress
    oActiveSheet.insertCells(oRangeAddressTargetRow, com.sun.star.sheet.CellInsertMode.ROWS)

    oCellTargetRow = oActiveSheet.getCellByPosition(0, c)
    oCellAddressTargetRow = oCellTargetRow.CellAddress

    oRangeSource = oActiveSheet.Rows.getByIndex(lLastFilledRowColumn1 + 1)
    oRangeAddressSource = oRangeSource.RangeAddress
    oActiveSheet.moveRange(oCellAddressTargetRow, oRangeAddressSource)
    c = c + 1
  next

end sub

快速步骤:

  • 在第 1 行上方插入新行
  • 用向右单调递增的整数索引填充行。
  • 然后 select 您的数据并降序排序,因此索引最高的右列排在第一位
  • 复制并粘贴结果


https://gr-plus.blogspot.com/2015/01/reverse-column-or-row-order-in-excel-or.html

不幸的是,第一个答案不起作用,因为它不是逐列排序,而是每列单独排序。

您不颠倒列的顺序,而是获取每列中从最高到最低的值。刚才试过了。在旧版 excel 中,您可以使用转置技巧,但 2021 年 libre office 不提供相同的功能

2021 年更好的解决方案是 INDEX- 函数。

=INDEX($A:$NN;1+$A30;25-A)

它采用您拥有的定义范围的数据,在带有美元符号的锁定网格中,并采用第一行减去两个列表中递增数字的值。

1,2,3...行和 1,2,3 列。在一个实例中锁定列,或在另一个实例中锁定行就可以了。下面是一个索引 table 示例,其中包含 Whosebug ASCII 格式。

| A30  |    B30                             |            C30                    |

| ---- |----------------------------------- |-----------------------------------|

| A31  | =INDEX($A:$NN;1+$A30;25-A  | =INDEX($A:$NN;1+$A30;25-B |

| A32  | =INDEX($A:$NN;1+$A31;25-A  | =INDEX($A:$NN;1+$A30;25-B |