Why can't use the copyRange to copy data in range?

Copy data in range A1:E1 from source sheet into target sheet with array.

 oTargetRange = targetDoc.getSheets().getByName("Sheet1").getCellRangeByPosition(0, 0 , 4,0 ) 
 oSourceRange = sourceSheet.getCellRangeByPosition(0, 0, 4,0)
 arr = oSourceRange.getDataArray()
 oTargetRange.setDataArray(arr)

It works fine,i want to use copyRange method

 oTargetRange = targetDoc.getSheets().getByName("Sheet1").getCellRangeByPosition(0, 0 , 4,0 ).getRangeAddress() 
 oSourceRange = sourceSheet.getCellRangeByPosition(0, 0, 4,0).getRangeAddress()
 sourceSheet.copyRange(oTargetRange, oSourceRange)

image
Why can’t use the copyRange to copy data in range?

In the copyRange method, the first parameter is of type com.sun.star.table.CellAddress.
So

oTargetRange = targetDoc.getSheets().getByName("Sheet1").getCellByPosition(0, 0).getCellAddress() 
1 Like

Error info :Property or method not found: getCellAddress

 oTargetRange = targetDoc.getSheets().getByName("Sheet1").getCellRangeByPosition(0, 0 , 4,0 ).getCellAddress() 
 oSourceRange = sourceSheet.getCellRangeByPosition(0, 0, 4,0).getRangeAddress()
 sourceSheet.copyRange(oTargetRange, oSourceRange)

If write as below:

 oTargetRange = targetDoc.getSheets().getByName("Sheet1").getCellByPosition(0, 0,4,0).getCellAddress() 
 oSourceRange = sourceSheet.getCellRangeByPosition(0, 0, 4,0).getRangeAddress()
 sourceSheet.copyRange(oTargetRange, oSourceRange)

No error info ,but no data written into target sheet’s cell or range.

Let’s check together.
Open the attached file and run the Test macro.

CopyRange.ods (9.2 KB)

Pleas download the test1.ods and run Test macro in it.

test1.ods (8.0 KB)

Sub Test
    Dim sourceSheet As Object
    Dim oTargetRange As Object
    Dim oSourceRange As Object
    Dim oDesk As Object
    Dim targetSheet As Object
    Dim sURL As String
    Dim sFilePath As String
          
    sourceSheet = ThisComponent.Sheets(1)
    oSourceRange = sourceSheet.getCellRangeByPosition(0, 0, 4,0).getRangeAddress() 
    oDesk = StarDesktop
    sURL = "private:factory/scalc"
    targetSheet = oDesk.loadComponentFromURL(sURL, "_blank", 0, Array())
    sFilePath = ConvertToURL( "/tmp/test2.ods")  
    oTargetRange = targetSheet.getSheets().getByName("Sheet1").getCellByPosition(0,0,4,0).getCellAddress()
    sourceSheet.copyRange(oTargetRange, oSourceRange)
    targetSheet.storeAsURL(sFilePath, Array())            
end sub

Why content in A1:E1 in test1.ods can’t write into test2.ods ? It seems that “sourceSheet.copyRange(oTargetRange, oSourceRange)” not executed as expect.

The purpose of the method is described in the documentation:

copies a cell range to another position in the document.

See also section “Copy data between documents” in book by A. Pitonyak OOME_4_1.odt

(post deleted by author)