vbalibreoffice-calclibreoffice-basic

Copy values and formats with a BASIC-Script


I want to copy the values, the formats and all conditional formats from an LibreOffice-Calc worksheet-range (A1:H100) to another (new/empty) worksheet of the same workbook.

This is, what I do actually:

sub blatt_einfuegen

  oActiveSheet = ThisComponent.CurrentController.ActiveSheet
  oTabellen = ThisComponent.Sheets
  sNewSheetName=oActiveSheet.getCellRangeByName("C1").String
  
  
  ' Prüfen, ob eine Tabelle mit dem Namen des Strings aus Zelle C1 existiert.
  If NOT oTabellen.hasbyName(sNewSheetName) Then
    ' Tabelle mit dem Namene sNewSheetName existiert nicht, also wird sie eingefügt.
    oTabellen.insertNewByName (sNewSheetName, 3)
  End If
  
  DIM oStart as Object, oZiel as Object

  oStart = ThisComponent.sheets.getByName("Aktuell")     '<--- Blatt wo die Daten stehen
  oZiel = ThisComponent.sheets.getByName(sNewSheetName)  '<--- Blatt wohin die Daten kopiert werden sollen

  ' Zellinhalte übertragen
  Dim aDataArray()  
  aDataArray = oStart.getCellRangeByName("A1:H100").getDataArray
  oZiel.getCellRangeByName("A1:H100").setDataArray(aDataArray)
 
  ' Spaltenbreiten übertragen
  For j = 0 To 6
    oZiel.Columns.getByIndex(j).Width = oStart.Columns.getByIndex(j).Width
  Next j

  Call CopyFormatBetweenSheets(sNewSheetName)
  
End Sub
Sub CopyFormatBetweenSheets(sName$)
    Dim oSourceSheet As Object
    Dim oTargetSheet As Object
    Dim oSourceRange As Object
    Dim oTargetRange As Object
    Dim i As Integer
    Dim j As Integer
    
    ' Get the source sheet (where the source range is located)
    oSourceSheet = ThisComponent.Sheets.getByName("Aktuell")
    
    ' Get the target sheet (where the target range is located)
    oTargetSheet = ThisComponent.Sheets.getByName(sName)    
    
    ' Define the source range (range with the formatting to be copied)
    oSourceRange = oSourceSheet.getCellRangeByName("A1:H100")
    
    ' Define the target range (range where the formatting will be applied)
    oTargetRange = oTargetSheet.getCellRangeByName("A1:H100")

    ' Copy the formatting from the source range to the target range
    For i = 0 To oSourceRange.Rows.getCount() - 1
       For j = 0 To oSourceRange.Columns.getCount() - 1
            oTargetRange.getCellByPosition(j, i).CharFontName = oSourceRange.getCellByPosition(j, i).CharFontName
            oTargetRange.getCellByPosition(j, i).CharHeight = oSourceRange.getCellByPosition(j, i).CharHeight
            oTargetRange.getCellByPosition(j, i).CharWeight = oSourceRange.getCellByPosition(j, i).CharWeight
            oTargetRange.getCellByPosition(j, i).NumberFormat = oSourceRange.getCellByPosition(j, i).NumberFormat
            oTargetRange.getCellByPosition(j, i).CharUnderline = oSourceRange.getCellByPosition(j, i).CharUnderline
            oTargetRange.getCellByPosition(j, i).HoriJustify = oSourceRange.getCellByPosition(j, i).HoriJustify
            oTargetRange.getCellByPosition(j, i).VertJustify = oSourceRange.getCellByPosition(j, i).VertJustify
            oTargetRange.getCellByPosition(j, i).ConditionalFormat = oSourceRange.getCellByPosition(j, i).ConditionalFormat

            ' Add more properties as needed (e.g., CharColor, CellBackColor, etc.)
       Next j
    Next i
End Sub

It copies the values, the formats, but only the first conditional format, what is defined in the cells from oSourceRange.

Is it possible to copy all of the conditional formats of the cells or is there a better way for copying the oSourceRange values, formats and conditional formats to the oTargetRange?

The values are copyied by an array-method (.setDataArray) - Is there a method for doing this with the fomats?


Solution

  • I found another answer using direct copy method. However, this only works within the same document, which was your requirement so it's OK.

    sub blatt_einfuegen
        oActiveSheet = ThisComponent.CurrentController.ActiveSheet
        oTabellen = ThisComponent.Sheets
        sNewSheetName=oActiveSheet.getCellRangeByName("C1").String
        ' Prüfen, ob eine Tabelle mit dem Namen des Strings aus Zelle C1 existiert.
        If NOT oTabellen.hasbyName(sNewSheetName) Then
            ' Tabelle mit dem Namene sNewSheetName existiert nicht, also wird sie eingefügt.
            oTabellen.insertNewByName (sNewSheetName, 3)
        End If
        DIM oStart as Object, oZiel as Object
    
        CopyCells("Aktuell", sNewSheetName)
      
    End Sub
    
    sub CopyCells(sinSource$, sinDest$)
        rem --------------------------------------------------------------
        dim oFromSheet as object
        dim oToSheet as object
        dim oCell as object
        dim oCellRange as object
        dim oCellRangeAddr as object
        dim oCols as object
        rem --------------------------------------------------------------
        oFromSheet = ThisComponent.Sheets.getByName(sinSource)
        oToSheet = ThisComponent.Sheets.getByName(sinDest)
    
        rem Select the region to copy
        oCellRange = oFromSheet.getCellRangeByName("A1:H100")
        oCellRangeAddr = oCellRange.getRangeAddress()
      
        rem Get the address of the destination
        oCell = oToSheet.getCellByPosition(0,0).getCellAddress()
        
        rem Execute the copy
        oToSheet.copyRange(oCell, oCellRangeAddr)
        
        rem Copy column width
        Dim lCol as long
        For lCol = 0 to oCellRange.Columns.getCount() - 1
            oToSheet.Columns(lCol).Width = oFromSheet.Columns(lCol).Width
        Next lCol
        
    end sub