Difference between revisions of "Documentation/DevGuide/Spreadsheets/Example: Adding a New Spreadsheet"
From Apache OpenOffice Wiki
		< Documentation | DevGuide
		
		
|  (correct code snippet) | |||
| Line 8: | Line 8: | ||
|   {{DISPLAYTITLE:Example: Adding a New Spreadsheet}} |   {{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 25: | 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.XSpreadsheetDocument; |    import com.sun.star.sheet.XSpreadsheetDocument; | ||
|    import com.sun.star.sheet.XSpreadsheet;   |    import com.sun.star.sheet.XSpreadsheet;   | ||
| Line 57: | Line 57: | ||
|        return xSheet; |        return xSheet; | ||
|    } |    } | ||
| − | + | </syntaxhighlight> | |
| {{PDL1}} | {{PDL1}} | ||
| [[Category:Documentation/Developer's Guide/Spreadsheet Documents]] | [[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). | 

