[python] how to copy cell and data with format?

Keyboard macros or custom scripts

[python] how to copy cell and data with format?

Postby strongleg » Sat Nov 04, 2017 2:58 pm

I'm trying to copy some cells (or may be rows or the whole sheet is better) with its format to other sheet. I have tried the way in the following post:
https://forum.openoffice.org/en/forum/v ... at#p420130
eg: I use the Cursor to get data and paste it to other sheet.
But with this way, data I got is pure data without format information(font, cell border, etc). so the dest cell has no format.

So how can i copy and paste cells(or row or sheet) with format information? thanks!
OpenOffice 2.4 on Ubuntu 9.04
Posts: 9
Joined: Sat Oct 28, 2017 4:41 pm

Re: [python] how to copy cell and data with format?

Postby FJCC » Sat Nov 04, 2017 5:32 pm

If you are going to write macros, you need to get the MRI extension so you can look at the properties and methods of your objects.

You can use the copyRange() method of a sheet to copy cell and keep the formatting.
Code: Select all   Expand viewCollapse view
def CopyRange():
  doc = XSCRIPTCONTEXT.getDocument()
  sheets = doc.Sheets
  sheet1 = sheets.getByName('Sheet1')
  cursor = sheet1.createCursor()
  RngAddr = cursor.getRangeAddress()
  sheet2 = sheets.getByName('Sheet2')
  cell = sheet2.getCellRangeByName('A1')
  cellAddr = cell.CellAddress #use this as the upper left of the paste range
  sheet1.copyRange(cellAddr, RngAddr)
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Posts: 6160
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [python] how to copy cell and data with format?

Postby Villeroy » Sat Nov 04, 2017 5:39 pm

Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 16.04, OpenOffice 4.x & LibreOffice 5.x
User avatar
Posts: 24653
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 5 guests