[Solved] Copy/paste range between files

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
andyroberts1234
Posts: 75
Joined: Fri Feb 11, 2011 2:29 pm

[Solved] Copy/paste range between files

Post by andyroberts1234 »

I have some working code (below) altered from http://www.oooforum.org/forum/viewtopic.phtml?t=1766 that copies from cell contents from cell A1 from the Test2.ods doc into A1 of the Test.ods doc.
It also copies cell A2 to A2.

Code: Select all

Sub copypaste 
Dim mNoArgs() REM Empty Sequence 
Dim sUrl As String 
oDesktop = createUnoService("com.sun.star.frame.Desktop") 
sUrl =  "file:///C:/Users/DMRC/Documents/Andy R/OpenOffice/Test.ods" 
tUrl = "file:///C:/Users/DMRC/Documents/Andy R/OpenOffice/Test2.ods" 
oThisDocument = oDesktop.LoadComponentFromURL(tUrl,"_blank",0,mNoArgs)  
oThatDocument = oDesktop.LoadComponentFromURL(sUrl,"_blank",0,mNoArgs) 
oThatDocument.Sheets(0).getcellbyposition(0,0).value=oThisDocument.Sheets(0).getcellbyposition(0,0).value
oThatDocument.Sheets(0).getcellbyposition(0,1).value=oThisDocument.Sheets(0).getcellbyposition(0,1).value
End Sub
What I want to do is copy a range ("D1:O26"), however when I change the getcellbyposition(0,1) to getcellrangebyname("D1:O26") I get the property or method not found error:value presumably because it is a multiple range. Is there a simple change that I've missed to paste a defined range rather than single cells one at a time? The range contains text, formulae (including some array formulae) and values.

Many thanks
Last edited by andyroberts1234 on Mon Feb 14, 2011 4:54 pm, edited 1 time in total.
Andy

OOO 3.3.0 / Windows 7 64-bit
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Copy/paste range between files

Post by Charlie Young »

I'm not sure this isn't overly complicated. and there are some considerations I'm glossing over for the moment. It is an interesting problem though, so I think it's worth tackling.

Working in just one document, one would like to use sheet.CopyRange, but I don't see how that can be made to work across documents. My approach (for now) assumes that the sheet, row, and column coordinates of the source and destination are the same. A real monkey wrench in the works here is the array formulae, which I have attempted to deal with by writing a function to get the entire cellrange of an array formula given one cell in it. Note also that array formulae here should be entirely within the source range.

This has not been exhaustively tested and should be treated with caution, and I have yet to deal with any formatting properties.

Code: Select all

Sub copypaste
	Dim mNoArgs() REM Empty Sequence
	Dim oThisDocument
	Dim oThatDocument
	Dim DestinationRange As Object
	Dim SourceRange As Object
	Dim fCells
	Dim oCell As Object
	Dim aRange As Object
	
	Dim oSheet
	Dim sUrl As String
	Dim tUrl As String
	oSheet = ThisComponent.Sheets(0)
	oDesktop = createUnoService("com.sun.star.frame.Desktop")
	sUrl =  "file:///C:/Users/DMRC/Documents/Andy R/OpenOffice/Test.ods"
	tUrl = "file:///C:/Users/DMRC/Documents/Andy R/OpenOffice/Test2.ods"
	oThisDocument = oDesktop.LoadComponentFromURL(sUrl,"_blank",0,mNoArgs) 
	oThatDocument = oDesktop.LoadComponentFromURL(tUrl,"_blank",0,mNoArgs)
	
	SourceRange = oThisDocument.Sheets(0).getCellRangeByName("D1:O26")
	DestinationRange = oThatDocument.Sheets(0).getCellRangeByName(SourceRange.AbsoluteName)
	DestinationRange.setDataArray(SourceRange.DataArray)	
	DestinationRange.setFormulaArray(SourceRange.FormulaArray)	
	
	fCells = SourceRange.queryFormulaCells(7)
	for each oCell in fCells.Cells
		if oCell.ArrayFormula <> "" then
			aRange = oThatDocument.Sheets(0).getCellRangeByName(getArrayFormulaRange(OCell,SourceRange).AbsoluteName)
			aRange.setArrayFormula(Mid(oCell.ArrayFormula,2,len(oCell.ArrayFormula)-2)
		endif
	next
End Sub

Function getArrayFormulaRange(oCell As Object, oRange As Object) As Object
	Dim fString As String
	Dim i As Long
	Dim j As Long
	Dim StartRow As Long
	Dim EndRow As Long
	Dim StartCol As Long
	Dim EndCol As Long
	Dim FirstArrayRow As Long
	Dim LastArrayRow As Long
	Dim FirstArrayCol As Long
	Dim LastArrayCol As Long
	Dim inArray As Boolean
	Dim fRange As Object
	
	StartRow = oRange.RangeAddress.StartRow
	EndRow = oRange.RangeAddress.EndRow
	StartCol = oRange.RangeAddress.StartColumn
	EndCol = oRange.RangeAddress.EndColumn
	
	fString = oCell.ArrayFormula
	
	FirstArrayRow = oCell.CellAddress.Row - StartRow
	FirstArrayCol = oCell.CellAddress.Column - StartCol
	LastArrayRow = FirstArrayRow
	LastArrayCol = FirstArrayCol
	
	inArray = True
	i = FirstArrayRow
	j = FirstArrayCol
	do while i >= 0 And inArray
		if oRange.getCellByPosition(j,i).ArrayFormula = fString then
			FirstArrayRow = i
			i = i - 1
		else
			inArray = False
		endif
	loop
	
	inArray = True
	i = LastArrayRow
	
	do while i <= EndRow - StartRow  And inArray
		if oRange.getCellByPosition(j,i).ArrayFormula = fString then
			LastArrayRow = i
			i = i + 1
		else
			inArray = False
		endif
	loop
	
	inArray = True
	i = FirstArrayRow
	j = FirstArrayCol
	do while j >= 0 And inArray
		if oRange.getCellByPosition(j,i).ArrayFormula = fString then
			FirstArrayCol = j
			j = j - 1
		else
			inArray = False
		endif
	loop
	
	inArray = True
	j = LastArrayCol
	
	do while j <= EndCol - StartCol  And inArray
		if oRange.getCellByPosition(j,i).ArrayFormula = fString then
			LastArrayCol = j
			j = j + 1
		else
			inArray = False
		endif
	loop
	
	getArrayFormulaRange = oRange.getCellRangeByPosition(FirstArrayCol,FirstArrayRow,LastArrayCol,LastArrayRow)
End Function
Apache OpenOffice 4.1.1
Windows XP
andyroberts1234
Posts: 75
Joined: Fri Feb 11, 2011 2:29 pm

Re: Copy/paste range between files

Post by andyroberts1234 »

Hi Charlie,

Thanks for your reply, I'm just having a look/try out of the code now- will post reply once it's tested.

Just a quick note for those wanting to do the same thing using single cells (with my code above) this does not work with array formulae.

Andy
Andy

OOO 3.3.0 / Windows 7 64-bit
andyroberts1234
Posts: 75
Joined: Fri Feb 11, 2011 2:29 pm

Re: Copy/paste range between files

Post by andyroberts1234 »

I have been testing it out and is working as hoped.

Many thanks- who'd of thought those array formulae would be such a nuisance! :super:

Presumably the same workaround is needed for single cells too? I don't need this, just curious (and may help someone else).
Andy

OOO 3.3.0 / Windows 7 64-bit
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [SOLVED] Copy/paste range between files

Post by Villeroy »

How to copy a sheet analog to Insert>SheetFromFile: http://www.oooforum.org/forum/viewtopic.phtml?t=70072

I don't understand what the array formulas are supposed to do. However, the API provides a method to get the range of an array formula: http://api.openoffice.org/docs/common/r ... rrentArray
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply