Problem copying multi-line text in Calc using macro

I’m writing a macro which copies data from a series of named ranges in one sheet into corresponding ranges in another sheet. It is working well, except for this weird behaviour - some of the cells contain text with line breaks, and when they are copied into the new sheet the text is displayed all on one line. So for instance:

Address line 1

Address line 2

becomes…

Address line 1Address line 2

I have checked by copying the contents of the cell into a hex editor, and the hex 0A characters for the line breaks are being copied across into the target cell. When I select the target cell and click in the edit box, the text in the cell is displayed as multiple lines, but when I click out of the edit box it reverts to one line.

Here’s the piece of code I’m using to copy the text across:

Newdoc = StarDesktop.loadComponentFromURL(qtemplate, “_blank”, 0, Dummy)

Master = ThisComponent

range = Master.Sheets(0).getCellRangebyName(“company”)

arr = range.getDataArray()

range = Newdoc.Sheets(0).getCellRangebyName(“company”)

range.setDataArray(arr)

Can anyone suggest a solution?

Edit: If I save the sheet with the cells containing the problematic text, then re-open it, and copy the text from that cell into the hex editor, the hex 0A characters are gone. I seem to be dealing with Schrodinger’s line breaks here, they are both there and not there.

I solved it by addressing the cell as a single cell rather than a range…

cell = Master.Sheets(0).getCellRangebyName(“company”)

s = cell.string

cell = Newdoc.Sheets(0).getCellRangebyName(“company”)

cell.string = s