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

    Sub Main()
    	SHEET_SOURCE = "Source"
    	RANGE_SOURCE = "A2:E2"
    	SHEET_TARGET = "Target"
    	CELL_TARGET = "A1"
    	
    	doc = ThisComponent
    	source = doc.Sheets.getByName(SHEET_SOURCE)
    	target = doc.Sheets.getByName(SHEET_TARGET)
    	
    	range = source.getCellRangeByName(RANGE_SOURCE)
    	cell = target.getCellRangeByName(CELL_TARGET)
    	next_free_cell = get_next_free_cell(cell)
    	
    	source.copyRange(next_free_cell.CellAddress, range.RangeAddress)
    	
    	'If you clean source then, 31 clean only data, not format
    	range.clearContents(31)
    	
    End Sub
    
    
    Function get_next_free_cell(cell)
    	cursor = cell.SpreadSheet.createCursorByRange(cell)
    	cursor.gotoEnd()
    	col = cell.CellAddress.Column
    	row = cursor.RangeAddress.EndRow + 1
    	get_next_free_cell = cell.SpreadSheet.getCellByPosition(col, row)
    End Function