Table Auto Formats

From Apache OpenOffice Wiki
Jump to: navigation, search



Table auto formats are used to apply different formats to a cell range. A table auto format is a collection of cell styles used to format all cells of a range. The style applied is dependent on the position of the cell.

The table auto format contains separate information about four different row types and four different column types:

  • First row (header), first data area row, second data area row, last row (footer)
  • First column, first data area column, second data area column, last column

The row or column types for the data area (between first and last row/column) are repeated in sequence. Each cell of the formatted range belongs to one of the row types and column types, resulting in 16 different auto-format fields. In the example below, the highlighted cell has the formatting of the first data area row and last column field. Additionally, this example shows the indexes of all the auto format fields. These indexes are used to access the field with the interface com.sun.star.container.XIndexAccess.

First column
Second data area column
First data area column
Last Column
First row (header)
0
2
1
3
First data area row
4
6
5
7
Second data area row
8
10
9
11
Last row (footer)
12
14
13
15


TableAutoFormat

A table auto format is represented by the service com.sun.star.sheet.TableAutoFormat. It contains exactly 16 auto format fields (service com.sun.star.sheet.TableAutoFormatField). Each auto format field contains all properties of a single cell.

The cell range interface com.sun.star.table.XAutoFormattable contains the method autoFormat() that applies a table auto format to a cell range. The cell range must have a size of at least 3x3 cells. The boolean properties of the table auto format determine the formatting properties are copied to the cells. The default setting of all the properties is true.

Documentation note.png In the current implementation it is not possible to modify the cell borders of a table auto format (the property TableBorder is missing). Nevertheless, the property IncludeBorder controls whether the borders of default auto formats are applied to the cells.

The collection of all table auto formats is represented by the service com.sun.star.sheet.TableAutoFormats. There is only one instance of this collection in the whole application. It contains all default and user-defined auto formats that are used in spreadsheets and tables of the word-processing application. It is possible to iterate through all table auto formats with an enumeration, or to access them directly using their index or their name.

TableAutoFormats

The following example shows how to insert a new table auto format, fill it with properties, apply it to a cell range and remove it from the format collection.

  public void doAutoFormatSample(
              com.sun.star.lang.XMultiServiceFactory xServiceManager,
              com.sun.star.sheet.XSpreadsheetDocument xDocument) throws RuntimeException, Exception { 
      // get the global collection of table auto formats, use global service manager
      Object aAutoFormatsObj = xServiceManager.createInstance("com.sun.star.sheet.TableAutoFormats");
      com.sun.star.container.XNameContainer xAutoFormatsNA = (com.sun.star.container.XNameContainer)
          UnoRuntime.queryInterface(com.sun.star.container.XNameContainer.class, aAutoFormatsObj);
 
      // create a new table auto format and insert into the container
      String aAutoFormatName = "Temp_Example";
      boolean bExistsAlready = xAutoFormatsNA.hasByName(aAutoFormatName);
      Object aAutoFormatObj = null;
      if (bExistsAlready)
          // auto format already exists -> use it
          aAutoFormatObj = xAutoFormatsNA.getByName(aAutoFormatName);
      else {
          // create a new auto format (with document service manager!)
          com.sun.star.lang.XMultiServiceFactory xDocServiceManager =
              (com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface(
                  com.sun.star.lang.XMultiServiceFactory.class, xDocument);
          aAutoFormatObj = xDocServiceManager.createInstance("com.sun.star.sheet.TableAutoFormat");
          xAutoFormatsNA.insertByName(aAutoFormatName, aAutoFormatObj);
      }
      // index access to the auto format fields
      com.sun.star.container.XIndexAccess xAutoFormatIA = (com.sun.star.container.XIndexAccess)
          UnoRuntime.queryInterface(com.sun.star.container.XIndexAccess.class, aAutoFormatObj);
 
      // set properties of all auto format fields
      for (int nRow = 0; nRow < 4; ++nRow) {
          int nRowColor = 0;
          switch (nRow) {
              case 0: nRowColor = 0x999999; break;
              case 1: nRowColor = 0xFFFFCC; break;
              case 2: nRowColor = 0xEEEEEE; break;
              case 3: nRowColor = 0x999999; break;
          }
 
          for (int nColumn = 0; nColumn < 4; ++nColumn) {
              int nColor = nRowColor;
              if ((nColumn == 0) || (nColumn == 3))
                  nColor -= 0x333300;
 
              // get the auto format field and apply properties
              Object aFieldObj = xAutoFormatIA.getByIndex(4 * nRow + nColumn);
              com.sun.star.beans.XPropertySet xPropSet = (com.sun.star.beans.XPropertySet)
                  UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, aFieldObj);
              xPropSet.setPropertyValue("CellBackColor", new Integer(nColor));
          }
      }   
 
      // set the auto format to the second spreadsheet
      com.sun.star.sheet.XSpreadsheets xSheets = xDocument.getSheets();
      com.sun.star.container.XIndexAccess xSheetsIA = (com.sun.star.container.XIndexAccess)
          UnoRuntime.queryInterface(com.sun.star.container.XIndexAccess.class, xSheets);
 
      com.sun.star.sheet.XSpreadsheet xSheet =
          (com.sun.star.sheet.XSpreadsheet) xSheetsIA.getByIndex(1);
 
      com.sun.star.table.XCellRange xCellRange = xSheet.getCellRangeByName("A5:H25");
      com.sun.star.table.XAutoFormattable xAutoForm = (com.sun.star.table.XAutoFormattable)
          UnoRuntime.queryInterface(com.sun.star.table.XAutoFormattable.class, xCellRange);
 
      xAutoForm.autoFormat(aAutoFormatName);
 
      // remove the auto format
      if (!bExistsAlready)
          xAutoFormatsNA.removeByName(aAutoFormatName);
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages