Copy content cell from Spreadsheet to other

    From The Document Foundation Wiki
    < Macros‎ | Calc
    This page contains changes which are not marked for translation.




    Description

    Copy row data from sheet to other. Copy data in next free cell.

    Basic code

    def main():
        SHEET_SOURCE = "Source"
        RANGE_SOURCE = "A2:E2"
        SHEET_TARGET = "Target"
        CELL_TARGET = "A1"
    
        doc = XSCRIPTCONTEXT.getDocument()
    
        sheet_source = doc.Sheets[SHEET_SOURCE]
        sheet_target = doc.Sheets[SHEET_TARGET]
        range_source = sheet_source[RANGE_SOURCE]
        cell_target = sheet_target[CELL_TARGET]
    
        next_free_cell = get_next_free_cell(cell_target)
        sheet_source.copyRange(next_free_cell.CellAddress, range_source.RangeAddress)
    
        # ~ If you clean source then, 31 clean only data, not format
        range_source.clearContents(31)
    
        return
    
    
    def get_next_free_cell(cell):
        cursor = cell.Spreadsheet.createCursorByRange(cell)
        cursor.gotoEnd()
        col = cell.CellAddress.Column
        row = cursor.RangeAddress.EndRow + 1
        next_cell = cell.Spreadsheet[row, col]
        return next_cell