Skip to content

Instantly share code, notes, and snippets.

@warvariuc
Last active March 13, 2024 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save warvariuc/a7aab5fc8405a0d724aeb91dc78e915a to your computer and use it in GitHub Desktop.
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.
Sub CopyHeaderWithEachRow()
Doc = ThisComponent
Sheets = Doc.Sheets
Controller = Doc.CurrentController
ActiveSheet = Controller.getActiveSheet()
Selection =Controller.getSelection()
SelAddress = selection.RangeAddress
NewSheetName = ActiveSheet.Name & "Copy"
If Sheets.hasByName(NewSheetName) Then
Sheets.removeByName(NewSheetName)
End If
Sheets.copyByName(ActiveSheet.Name, NewSheetName, ActiveSheet.RangeAddress.Sheet + 1)
NewSheet = Sheets.getByName(NewSheetName)
REM leave only values, without formulas which can break when insert rows
AllRange = NewSheet.GetCellRangeByName("A1:Z100")
AllRange.SetDataArray(AllRange.GetDataArray())
REM -------------------------------------------------------------------------------
HeaderRange = NewSheet.getCellRangeByPosition(0, 0, 100, 0)
rem 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))
rem https://wiki.documentfoundation.org/Macros/Basic/Calc/Ranges
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