Calc: move/copy sheet - where is it?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
ptoye
Posts: 225
Joined: Mon Apr 21, 2008 11:07 am
Location: Reading, England

Calc: move/copy sheet - where is it?

Post by ptoye »

Where is the OO basic method to do the same as the "move/copy sheet" GUI action. I've been poking around the API documentation but can't find it.

Peter
Peter
OO 4.1.1 on MS Windows 7 64-bit
User avatar
kingfisher
Volunteer
Posts: 2127
Joined: Tue Nov 20, 2007 10:53 am

Re: Calc: move/copy sheet - where is it?

Post by kingfisher »

The methods belong to 'Sheets':

ThisComponent.Sheets.copybyName | movebyName
Apache OpenOffice 4.1.12 on Linux
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Calc: move/copy sheet - where is it?

Post by Villeroy »

Yep, interface com.sun.star.sheet.XSpreadsheets provides the methods to insert, copy and move a sheet within the same collection of sheets.
The following proposal copies from one document to another. The source document needs to be stored to disk. May be there is a workaround for unsaved documents. I don't know.

Code: Select all

REM  *****  BASIC  *****
REM copy first sheet of this document to position 2 of a new one
Sub Main
REM source document
Dim sURL$, sLinkSheetName$
   sURL = thisComponent.getURL()
   sLinkSheetName = thisComponent.Sheets.getByIndex(0).getName()
   
REM target document
Dim doc, sheets, sName$, pos%
   doc = StarDesktop.loadComponentFromURL("private:factory/scalc", "_default",0, Array())
   sheets = doc.getSheets()
   sName = getUniqueName(sheets, "Copied")
   pos = 1

REM new sheet
Dim sh
   sheets.insertNewByName(sName, pos)
   sh = sheets.getByName(sName)
   
REM link the new sheet
   sh.link(sURL, sLinkSheetName, "calc8", "", com.sun.star.sheet.SheetLinkMode.NORMAL)
   
REM break link
   sh.setLinkMode(com.sun.star.sheet.SheetLinkMode.NONE)
End Sub

Function getUniqueName(oContainer,sName$)
Dim i%,sNew$
   sNew = sName
   Do while oContainer.hasByName(sNew)
      i = i +1
      sNew = sName &"_"& i      
   loop
   getUniqueName = sNew
End Function
Copied references to other sheets behave analog to the discussion with huw in [Tutorial] Absolute, relative and mixed references
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
ptoye
Posts: 225
Joined: Mon Apr 21, 2008 11:07 am
Location: Reading, England

[SOLVED] Calc: move/copy sheet - where is it?

Post by ptoye »

Thanks Villeroy. I should have said that source and destination are in different documents, but you seem to have guessed.

It seems a bit long-winded and not very intuitive - is that how the move/copy feature works in the GUI do you know?
Peter
OO 4.1.1 on MS Windows 7 64-bit
User avatar
Villeroy
Volunteer
Posts: 31348
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Calc: move/copy sheet - where is it?

Post by Villeroy »

Get used to play on the naked bones of the API. Quite often you have to find your own technique to get the right sound.
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
User avatar
RichieRH
Posts: 36
Joined: Sun Oct 19, 2014 6:16 pm

Re: Calc: move/copy sheet - where is it?

Post by RichieRH »

Hi everyone,

I used the code from the Villeroy code.And Everything works fine except that I can't export an image from the source document to the target document..What is wrong?Or is it possible to do such thing?
Casue when I use it manually from the calc (without macro thing), by this follow instructions -- right click on a sheet tabs -- >choose copy/move ---> choose a new workbook-->click ok, then all the items (including an object or image) in the source document are being move/copy to the target document....

Does anyone have a solution of my problem??
Debian 8 Jessie
Linux Mint 17.3 XFCE
Country : Indonesia
Learning is a need
mauriciobaeza
Posts: 56
Joined: Thu Apr 22, 2010 5:03 am

Re: Calc: move/copy sheet - where is it?

Post by mauriciobaeza »

You can test EasyDev, you can help me validate this function.
https://github.com/UniversoLibreMexicoA ... v2.3.0.oxt
http://easydev.readthedocs.org/en/lates ... o-document

Code: Select all

Sub CopySheets()
	
	util = createUnoService("org.universolibre.EasyDev")
	source = createUnoStruct("org.universolibre.EasyDev.CellRangeAddress")
	target = createUnoStruct("org.universolibre.EasyDev.CellRangeAddress")

	doc = ThisComponent
	source.Doc = doc
	
	new_doc = util.newDoc("")
	target.Doc = new_doc
	
	util.sheetCopyToDoc(source, target, -1, True, True)
		
End Sub
img020.png
Best regards
______________________________________________
Everything not given is lost
AOO 4.1 / LibO 4.3 on ArchLinux with Gnome3
Please, I do not answer private questions, you use the forum
User avatar
RichieRH
Posts: 36
Joined: Sun Oct 19, 2014 6:16 pm

Re: Calc: move/copy sheet - where is it?

Post by RichieRH »

mauriciobaeza wrote:You can test EasyDev, you can help me validate this function.
https://github.com/UniversoLibreMexicoA ... v2.3.0.oxt
http://easydev.readthedocs.org/en/lates ... o-document

Code: Select all

Sub CopySheets()
	
	util = createUnoService("org.universolibre.EasyDev")
	source = createUnoStruct("org.universolibre.EasyDev.CellRangeAddress")
	target = createUnoStruct("org.universolibre.EasyDev.CellRangeAddress")

	doc = ThisComponent
	source.Doc = doc
	
	new_doc = util.newDoc("")
	target.Doc = new_doc
	
	util.sheetCopyToDoc(source, target, -1, True, True)
		
End Sub
img020.png
Best regards

Okay tks for the code mauriciobaeza
You are my hero
Debian 8 Jessie
Linux Mint 17.3 XFCE
Country : Indonesia
Learning is a need
Post Reply