Difference between revisions of "Documentation/DevGuide/Spreadsheets/Example: Adding a New Spreadsheet"
From Apache OpenOffice Wiki
< Documentation | DevGuide
OOoWikiBot (Talk | contribs) m (Robot: Changing Category:Spreadsheet Documents) |
|||
(4 intermediate revisions by 3 users not shown) | |||
Line 5: | Line 5: | ||
|NextPage=Documentation/DevGuide/Spreadsheets/Example: Editing Spreadsheet Cells | |NextPage=Documentation/DevGuide/Spreadsheets/Example: Editing Spreadsheet Cells | ||
}} | }} | ||
− | {{DISPLAYTITLE:Example: Adding a New Spreadsheet}} | + | {{Documentation/DevGuideLanguages|Documentation/DevGuide/Spreadsheets/{{SUBPAGENAME}}}} |
+ | {{DISPLAYTITLE:Example: Adding a New Spreadsheet}} | ||
The following helper method opens a new spreadsheet document component. The method <code>getRemoteServiceManager()</code> retrieves a connection. Refer to chapter [[Documentation/DevGuide/FirstSteps/First Steps|First Steps]] for additional information. | The following helper method opens a new spreadsheet document component. The method <code>getRemoteServiceManager()</code> retrieves a connection. Refer to chapter [[Documentation/DevGuide/FirstSteps/First Steps|First Steps]] for additional information. | ||
− | + | <syntaxhighlight lang="java"> | |
import com.sun.star.lang.XComponent; | import com.sun.star.lang.XComponent; | ||
import com.sun.star.frame.XComponentLoader; | import com.sun.star.frame.XComponentLoader; | ||
Line 24: | Line 25: | ||
return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps); | return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps); | ||
} | } | ||
− | + | </syntaxhighlight> | |
Our helper returns a <idl>com.sun.star.lang.XComponent</idl> interface for the recently loaded document. Now the XComponent is passed to the following method <code>insertSpreadsheet()</code> to add a new spreadsheet to our document. | Our helper returns a <idl>com.sun.star.lang.XComponent</idl> interface for the recently loaded document. Now the XComponent is passed to the following method <code>insertSpreadsheet()</code> to add a new spreadsheet to our document. | ||
<!--[SOURCE:Spreadsheet/SpreadsheetDocHelper.java]--> | <!--[SOURCE:Spreadsheet/SpreadsheetDocHelper.java]--> | ||
− | + | <syntaxhighlight lang="java"> | |
− | import com.sun.star.sheet. | + | import com.sun.star.sheet.XSpreadsheetDocument; |
import com.sun.star.sheet.XSpreadsheet; | import com.sun.star.sheet.XSpreadsheet; | ||
Line 39: | Line 40: | ||
@return The XSpreadsheet interface of the new sheet. | @return The XSpreadsheet interface of the new sheet. | ||
*/ | */ | ||
− | public XSpreadsheet insertSpreadsheet( | + | public XSpreadsheet insertSpreadsheet(XComponent xSheetComponent, String aName, short nIndex) { |
− | + | ||
XSpreadsheetDocument xDocument = (XSpreadsheetDocument)UnoRuntime.queryInterface( | XSpreadsheetDocument xDocument = (XSpreadsheetDocument)UnoRuntime.queryInterface( | ||
XSpreadsheetDocument.class, xSheetComponent); | XSpreadsheetDocument.class, xSheetComponent); | ||
Line 50: | Line 50: | ||
try { | try { | ||
xSheets.insertNewByName(aName, nIndex); | xSheets.insertNewByName(aName, nIndex); | ||
− | xSheet = xSheets.getByName(aName); | + | xSheet = (com.sun.star.sheet.XSpreadsheet) UnoRuntime.queryInterface( |
+ | com.sun.star.sheet.XSpreadsheet.class, xSheets.getByName(aName)); | ||
} catch (Exception ex) { | } catch (Exception ex) { | ||
} | } | ||
Line 56: | Line 57: | ||
return xSheet; | return xSheet; | ||
} | } | ||
− | + | </syntaxhighlight> | |
{{PDL1}} | {{PDL1}} | ||
− | [[Category:Documentation/ | + | [[Category:Documentation/Developer's Guide/Spreadsheet Documents]] |
Latest revision as of 15:01, 3 January 2021
The following helper method opens a new spreadsheet document component. The method getRemoteServiceManager()
retrieves a connection. Refer to chapter First Steps for additional information.
import com.sun.star.lang.XComponent; import com.sun.star.frame.XComponentLoader; import com.sun.star.beans.PropertyValue; ... protected XComponent newSpreadsheetComponent() throws java.lang.Exception { String loadUrl = "private:factory/scalc"; xRemoteServiceManager = this.getRemoteServiceManager(unoUrl); Object desktop = xRemoteServiceManager.createInstanceWithContext( "com.sun.star.frame.Desktop", xRemoteContext); XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface( XComponentLoader.class, desktop); PropertyValue[] loadProps = new PropertyValue[0]; return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps); }
Our helper returns a com.sun.star.lang.XComponent interface for the recently loaded document. Now the XComponent is passed to the following method insertSpreadsheet()
to add a new spreadsheet to our document.
import com.sun.star.sheet.XSpreadsheetDocument; import com.sun.star.sheet.XSpreadsheet; ... /** Inserts a new empty spreadsheet with the specified name. @param xSheetComponent The XComponent interface of a loaded document object @param aName The name of the new sheet. @param nIndex The insertion index. @return The XSpreadsheet interface of the new sheet. */ public XSpreadsheet insertSpreadsheet(XComponent xSheetComponent, String aName, short nIndex) { XSpreadsheetDocument xDocument = (XSpreadsheetDocument)UnoRuntime.queryInterface( XSpreadsheetDocument.class, xSheetComponent); // Collection of sheets com.sun.star.sheet.XSpreadsheets xSheets = xDocument.getSheets(); com.sun.star.sheet.XSpreadsheet xSheet = null; try { xSheets.insertNewByName(aName, nIndex); xSheet = (com.sun.star.sheet.XSpreadsheet) UnoRuntime.queryInterface( com.sun.star.sheet.XSpreadsheet.class, xSheets.getByName(aName)); } catch (Exception ex) { } return xSheet; }
Content on this page is licensed under the Public Documentation License (PDL). |