Copy XSpreadsheet

I want to copy a XSpreadsheet.

I found a method to copy using “Spreadsheets.copyByName”.

But How could i get a sheet name from “XSpreadSheet”?

Or is there other way to copy?

@hercle896,

Appears you forgot to mention the programming language you are working with.

Most responses here will be for Basic or Python. Not much in the way of Java or JavaScript.

' Create a new document, copy the first sheet after the last one, the name for the copy is NewSheet. 
Sub TestCopyByName
  Dim oDoc, aName As String, aCopy As String, nDestination As Long
  
  oDoc=StarDesktop.loadComponentFromURL("private:factory/scalc", "_blank", 0, Array())
  aName=oDoc.Sheets(0).Name  ' name of 1-st sheet
  aCopy="NewSheet"
  nDestination=oDoc.Sheets.Count
  oDoc.Sheets.copyByName aName, aCopy, nDestination
End Sub

@sokol92,

a hint specific to the site’s engine quirks regarding syntax highlighting: to avoid the coloring from ' to the next ' instead of to the end of line, just enclose the whole comment into the ':

' Create a new document, copy the first sheet after the last one, the name for the copy is NewSheet. '
Sub TestCopyByName
    ...

Thank you, Mike, it would be difficult for me to guess myself (however, you guessed it). :slight_smile:

Thank you for your comment.

aName=oDoc.Sheets(0).Name ’ name of 1-st sheet

I asked how to get a sheet name from “XSpreadsheet”.

It meant I can’t retrieve a sheet name like “Sheets(0).Name”.

I can’t get it using above way.

The class of document is “XSpreadsheetDocument”.

======================

Sorry, I forgot to say that I’m developing for the LibreOffice Calc.

(But, I shown that by a tag.)

======================

I thought to copy to a new sheet from the original sheet.

But I guess it is not smart way, and it may can’t copy all data and objects.

As a result, I could get a sheet name.

	XNamed xn = UnoRuntime.queryInterface(com.sun.star.container.XNamed.class, sheet);
	String sheet_name = xn.getName();

But I don’t know what is “UnoRuntime.queryInterface”.

How I can know that XSpreadSheet can be converted to XName.