2017-02-03 27 views
1

假設我有這此細胞:反向列自由報辦公室計算的訂購

Initial Enteries

然後由於某種原因,我想自動反轉列的順序成爲這樣的事情:

enter image description here

任何幫助將不勝感激!

+0

這個問題是如何與編程有關的?在舊列「A」前面插入一個新的空列「A」。剪切/粘貼現在的最後一列'D'。現在將列B剪切/粘貼到現在空的列D中。刪除現在空的列'B'。 –

+0

@AxelRichter感謝您的評論。但我認爲存在誤解。這3列僅僅是一個例子。我想爲大量的列做這個。所以剪切和粘貼不是我想要的。這應該可能通過編程。 – SirSaleh

+0

那麼應該使用哪種編程語言? –

回答

1

所以這裏是一個使用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

+0

我不知道我在哪裏做錯了,這個宏停下來爲我'BASIC語法錯誤。預計:Sub.' – SirSaleh

+0

錯誤的意思是,關鍵字'Sub'是預期的但未找到。你已經看到我的代碼從第一行的'Sub ReverseColumns()'開始? –

+0

是的!我在我的代碼中檢查它!我也有,但是錯誤仍然存​​在。 – SirSaleh

0

如果有人在這裏想扭轉(而不是列)的順序......我把宏代碼posted by @Axel-Richter和編輯它,所以它只是這樣做:

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