Last active
November 23, 2024 10:56
-
-
Save warvariuc/a7aab5fc8405a0d724aeb91dc78e915a to your computer and use it in GitHub Desktop.
Take current sheet, copy it as a new one, remove formulas leaving only values, insert first row (header) before each other. Useful for printing the table to able to cut with scissors each row with its own header.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub CopyHeaderWithEachRow() | |
Doc = ThisComponent | |
Controller = Doc.CurrentController | |
ActiveSheet = Controller.getActiveSheet() | |
NewSheetName = ActiveSheet.Name | |
While Doc.Sheets.hasByName(NewSheetName) | |
NewSheetName = NewSheetName & " Copy" | |
Wend | |
'Sheets.insertNewByName(NewSheetName, ActiveSheet.RangeAddress.Sheet + 1) | |
Doc.Sheets.copyByName(ActiveSheet.Name, NewSheetName, ActiveSheet.RangeAddress.Sheet + 1) | |
NewSheet = Doc.Sheets.getByName(NewSheetName) | |
' unfreeze all rows/columns | |
Controller.select(NewSheet) | |
Controller.freezeAtPosition(0, 0) | |
' leave only values, without formulas which can break when we insert rows | |
AllRange = NewSheet.GetCellRangeByName("A1:Z100") | |
AllRange.SetDataArray(AllRange.GetDataArray()) | |
' ------------------------------------------------------------------------------- | |
HeaderRange = NewSheet.getCellRangeByPosition(0, 0, 100, 0) | |
' we have no more than 50 | |
For i = 0 To 50 | |
NewSheet.Rows.insertByIndex(2 + i*3, 1) | |
delBorderLine(NewSheet.getCellRangeByPosition(0, 2 + i*3, 100, 2 + i*3)) | |
NewSheet.Rows.insertByIndex(2+ i*3, 1) | |
delBorderLine(NewSheet.getCellRangeByPosition(0, 2 + i*3, 100, 2 + i*3)) | |
targetCell = NewSheet.getCellByPosition(0, 2 + i*3 + 1) | |
NewSheet.copyRange(targetCell.CellAddress, HeaderRange.RangeAddress) | |
Next i | |
End Sub | |
Sub delBorderLine(selection As Variant) | |
Dim border_line As New com.sun.star.table.BorderLine | |
With border_line | |
.Color = 0 | |
.InnerLineWidth = 0 | |
.OuterLineWidth = 0 | |
.LineDistance = 0 | |
End With | |
With selection | |
.TopBorder = border_line | |
.BottomBorder = border_line | |
.LeftBorder = border_line | |
.RightBorder = border_line | |
End With | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment