Jump to content

Copy content cell from Spreadsheet to other

From The Document Foundation Wiki



Description

Copy content cell from Spreadsheet to other. The spreadsheet source should be open.

Basic code

Sub main()
DOC_NAME_SOURCE = "source.ods"
SHEET_NAME_SOURCE = "source"
CELL_NAME_SOURCE = "A1"

    doc_source = get_doc(DOC_NAME_SOURCE)
    sheet_source = doc_source.Sheets.getByName(SHEET_NAME_SOURCE)
    cell_source = sheet_source.getCellRangeByName(CELL_NAME_SOURCE)

    doc = ThisComponent
    sheet = doc.CurrentController.ActiveSheet
    cell = sheet.getCellRangeByName(CELL_NAME_SOURCE)
 
    If cell_source.Type = 1 Then
        cell.Value = cell_source.Value
    ElseIf cell_source.Type = 2 Then
        cell.String = cell_source.String
    Else
        cell.Formula = cell_source.Formula
    End If
End Sub

Function get_doc(title)
    docs = StarDesktop.getComponents()
    enumeration = docs.createEnumeration()
    Do While enumeration.hasMoreElements()
        doc = enumeration.nextElement()
        If doc.Title = title Then
            Exit Do
        End If
    Loop
    get_doc = doc
End Function