Difference between revisions of "Documentation/DevGuide/FirstSteps/Example: Working with a Spreadsheet Document"

From Apache OpenOffice Wiki
Jump to: navigation, search
Line 3: Line 3:
 
In this example, we will ask the remote service manager to give us the remote Desktop object and use its loadComponentFromURL() method to create a new spreadsheet document. From the document we get its sheets container where we insert and access a new sheet by name. In the new sheet, we enter values into A1 and A2 and summarize them in A3. The cell style of the summarizing cell gets the cell style Result, so that it appears in italics, bold and underlined. Finally, we make our new sheet the active sheet, so that the user can see it.
 
In this example, we will ask the remote service manager to give us the remote Desktop object and use its loadComponentFromURL() method to create a new spreadsheet document. From the document we get its sheets container where we insert and access a new sheet by name. In the new sheet, we enter values into A1 and A2 and summarize them in A3. The cell style of the summarizing cell gets the cell style Result, so that it appears in italics, bold and underlined. Finally, we make our new sheet the active sheet, so that the user can see it.
 
Add these import lines to the FirstConnection example above: [SOURCE:FirstSteps/FirstLoadComponent.java]
 
Add these import lines to the FirstConnection example above: [SOURCE:FirstSteps/FirstLoadComponent.java]
 +
 
   import com.sun.star.beans.PropertyValue;
 
   import com.sun.star.beans.PropertyValue;
 
   import com.sun.star.lang.XComponent;
 
   import com.sun.star.lang.XComponent;
Line 12: Line 13:
 
   import com.sun.star.frame.XModel;
 
   import com.sun.star.frame.XModel;
 
   import com.sun.star.frame.XController;
 
   import com.sun.star.frame.XController;
import com.sun.star.frame.XComponentLoader;
+
  import com.sun.star.frame.XComponentLoader;
 
Edit the useConnection method as follows:
 
Edit the useConnection method as follows:
protected void useConnection() throws java.lang.Exception {
+
  protected void useConnection() throws java.lang.Exception {
 
         try {
 
         try {
 
             // get the remote office component context
 
             // get the remote office component context
Line 26: Line 27:
 
             System.exit(1);
 
             System.exit(1);
 
         }
 
         }
 
+
 
try {
+
  try {
 
+
 
         // get the Desktop, we need its XComponentLoader interface to load a new document
 
         // get the Desktop, we need its XComponentLoader interface to load a new document
 
         Object desktop = xRemoteServiceManager.createInstanceWithContext(
 
         Object desktop = xRemoteServiceManager.createInstanceWithContext(
Line 36: Line 36:
 
         XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
 
         XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
 
             XComponentLoader.class, desktop);
 
             XComponentLoader.class, desktop);
 
+
 
 
         // create empty array of PropertyValue structs, needed for loadComponentFromURL
 
         // create empty array of PropertyValue structs, needed for loadComponentFromURL
 
         PropertyValue[] loadProps = new PropertyValue[0];
 
         PropertyValue[] loadProps = new PropertyValue[0];
Line 43: Line 43:
 
         XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL(
 
         XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL(
 
             "private:factory/scalc", "_blank", 0, loadProps);
 
             "private:factory/scalc", "_blank", 0, loadProps);
 
+
 
 
         // query its XSpreadsheetDocument interface, we want to use getSheets()
 
         // query its XSpreadsheetDocument interface, we want to use getSheets()
 
         XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)UnoRuntime.queryInterface(
 
         XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)UnoRuntime.queryInterface(
 
             XSpreadsheetDocument.class, xSpreadsheetComponent);
 
             XSpreadsheetDocument.class, xSpreadsheetComponent);
 
+
 
 
+
 
         // use getSheets to get spreadsheets container
 
         // use getSheets to get spreadsheets container
 
         XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
 
         XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
 
+
 
 
         //insert new sheet at position 0 and get it by name, then query its XSpreadsheet interface
 
         //insert new sheet at position 0 and get it by name, then query its XSpreadsheet interface
 
         xSpreadsheets.insertNewByName("MySheet", (short)0);
 
         xSpreadsheets.insertNewByName("MySheet", (short)0);
Line 57: Line 56:
 
         XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface(
 
         XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface(
 
             XSpreadsheet.class, sheet);
 
             XSpreadsheet.class, sheet);
 
+
 
          // use XSpreadsheet interface to get the cell A1 at position 0,0 and enter 21 as value
+
        // use XSpreadsheet interface to get the cell A1 at position 0,0 and enter 21 as value
 
         XCell xCell = xSpreadsheet.getCellByPosition(0, 0);
 
         XCell xCell = xSpreadsheet.getCellByPosition(0, 0);
 
         xCell.setValue(21);
 
         xCell.setValue(21);
 
+
 
// enter another value into the cell A2 at position 0,1
+
        // enter another value into the cell A2 at position 0,1
 
         xCell = xSpreadsheet.getCellByPosition(0, 1);
 
         xCell = xSpreadsheet.getCellByPosition(0, 1);
 
         xCell.setValue(21);
 
         xCell.setValue(21);
 
+
 
// sum up the two cells
+
        // sum up the two cells
 
         xCell = xSpreadsheet.getCellByPosition(0, 2);
 
         xCell = xSpreadsheet.getCellByPosition(0, 2);
 
         xCell.setFormula("=sum(A1:A2)");
 
         xCell.setFormula("=sum(A1:A2)");
 
+
 
 
         // we want to access the cell property CellStyle, so query the cell's XPropertySet interface
 
         // we want to access the cell property CellStyle, so query the cell's XPropertySet interface
 
         XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface(
 
         XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface(
 
             XPropertySet.class, xCell);
 
             XPropertySet.class, xCell);
 
+
 
 
         // assign the cell style "Result" to our formula, which is available out of the box
 
         // assign the cell style "Result" to our formula, which is available out of the box
 
         xCellProps.setPropertyValue("CellStyle", "Result");
 
         xCellProps.setPropertyValue("CellStyle", "Result");
 
+
 
 
         // we want to make our new sheet the current sheet, so we need to ask the model
 
         // we want to make our new sheet the current sheet, so we need to ask the model
 
         // for the controller: first query the XModel interface from our spreadsheet component
 
         // for the controller: first query the XModel interface from our spreadsheet component
Line 84: Line 83:
 
         // then get the current controller from the model
 
         // then get the current controller from the model
 
         XController xSpreadsheetController = xSpreadsheetModel.getCurrentController();
 
         XController xSpreadsheetController = xSpreadsheetModel.getCurrentController();
 
+
 
 
         // get the XSpreadsheetView interface from the controller, we want to call its method
 
         // get the XSpreadsheetView interface from the controller, we want to call its method
 
         // setActiveSheet
 
         // setActiveSheet
 
         XSpreadsheetView xSpreadsheetView = (XSpreadsheetView)UnoRuntime.queryInterface(
 
         XSpreadsheetView xSpreadsheetView = (XSpreadsheetView)UnoRuntime.queryInterface(
 
           XSpreadsheetView.class, xSpreadsheetController);
 
           XSpreadsheetView.class, xSpreadsheetController);
 
+
 
 
         // make our newly inserted sheet the active sheet using setActiveSheet
 
         // make our newly inserted sheet the active sheet using setActiveSheet
 
         xSpreadsheetView.setActiveSheet(xSpreadsheet);     
 
         xSpreadsheetView.setActiveSheet(xSpreadsheet);     
Line 97: Line 96:
 
         throw e;
 
         throw e;
 
     }           
 
     }           
}
+
  }
 
Alternatively, you can add FirstLoadComponent.java from the samples directory to your current project, it contains the changes shown above.
 
Alternatively, you can add FirstLoadComponent.java from the samples directory to your current project, it contains the changes shown above.
  
 
[[Category: Development Concepts]]
 
[[Category: Development Concepts]]

Revision as of 10:13, 16 May 2007

Template:DevGuide Template:Title override In this example, we will ask the remote service manager to give us the remote Desktop object and use its loadComponentFromURL() method to create a new spreadsheet document. From the document we get its sheets container where we insert and access a new sheet by name. In the new sheet, we enter values into A1 and A2 and summarize them in A3. The cell style of the summarizing cell gets the cell style Result, so that it appears in italics, bold and underlined. Finally, we make our new sheet the active sheet, so that the user can see it. Add these import lines to the FirstConnection example above: [SOURCE:FirstSteps/FirstLoadComponent.java]

 import com.sun.star.beans.PropertyValue;
 import com.sun.star.lang.XComponent;
 import com.sun.star.sheet.XSpreadsheetDocument;
 import com.sun.star.sheet.XSpreadsheets;
 import com.sun.star.sheet.XSpreadsheet;
 import com.sun.star.sheet.XSpreadsheetView;
 import com.sun.star.table.XCell;
 import com.sun.star.frame.XModel;
 import com.sun.star.frame.XController;
 import com.sun.star.frame.XComponentLoader;

Edit the useConnection method as follows:

 protected void useConnection() throws java.lang.Exception {
       try {
           // get the remote office component context
           xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
           System.out.println("Connected to a running office ...");
               
           xRemoteServiceManager = xRemoteContext.getServiceManager();
       }
       catch( Exception e) {
           e.printStackTrace();
           System.exit(1);
       }
 
 try {
       // get the Desktop, we need its XComponentLoader interface to load a new document
       Object desktop = xRemoteServiceManager.createInstanceWithContext(
           "com.sun.star.frame.Desktop", xRemoteContext);
       
       // query the XComponentLoader interface from the desktop
       XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
           XComponentLoader.class, desktop);
 
       // create empty array of PropertyValue structs, needed for loadComponentFromURL
       PropertyValue[] loadProps = new PropertyValue[0];
       
       // load new calc file
       XComponent xSpreadsheetComponent = xComponentLoader.loadComponentFromURL(
           "private:factory/scalc", "_blank", 0, loadProps);
 
       // query its XSpreadsheetDocument interface, we want to use getSheets()
       XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)UnoRuntime.queryInterface(
           XSpreadsheetDocument.class, xSpreadsheetComponent);
 
       // use getSheets to get spreadsheets container
       XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets();
 
       //insert new sheet at position 0 and get it by name, then query its XSpreadsheet interface
       xSpreadsheets.insertNewByName("MySheet", (short)0);
       Object sheet = xSpreadsheets.getByName("MySheet");
       XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface(
           XSpreadsheet.class, sheet);
 
       // use XSpreadsheet interface to get the cell A1 at position 0,0 and enter 21 as value
       XCell xCell = xSpreadsheet.getCellByPosition(0, 0);
       xCell.setValue(21);
 
       // enter another value into the cell A2 at position 0,1
       xCell = xSpreadsheet.getCellByPosition(0, 1);
       xCell.setValue(21);
 
       // sum up the two cells
       xCell = xSpreadsheet.getCellByPosition(0, 2);
       xCell.setFormula("=sum(A1:A2)");
 
       // we want to access the cell property CellStyle, so query the cell's XPropertySet interface
       XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface(
           XPropertySet.class, xCell);
 
       // assign the cell style "Result" to our formula, which is available out of the box
       xCellProps.setPropertyValue("CellStyle", "Result");
 
       // we want to make our new sheet the current sheet, so we need to ask the model
       // for the controller: first query the XModel interface from our spreadsheet component
       XModel xSpreadsheetModel = (XModel)UnoRuntime.queryInterface(
          XModel.class, xSpreadsheetComponent);
       
       // then get the current controller from the model
       XController xSpreadsheetController = xSpreadsheetModel.getCurrentController();
 
       // get the XSpreadsheetView interface from the controller, we want to call its method
       // setActiveSheet
       XSpreadsheetView xSpreadsheetView = (XSpreadsheetView)UnoRuntime.queryInterface(
          XSpreadsheetView.class, xSpreadsheetController);
 
       // make our newly inserted sheet the active sheet using setActiveSheet
       xSpreadsheetView.setActiveSheet(xSpreadsheet);     
   }
   catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1
       xRemoteContext = null;
       throw e;
   }          
 }

Alternatively, you can add FirstLoadComponent.java from the samples directory to your current project, it contains the changes shown above.

Personal tools