OO-Snippets: How to assign an auto-format to columns

Commons

Keywordsauto-format
LanguageJava
ApplicationCalc
AuthorsTom Schindl (initial)
Supported Versions1.1.4  1.1.x  
Supported OSAll  
Question How can I assign an autoformat to cells
Answer

You need to retrieve a cell-tange and retrieve its com.sun.star.table.XAutoFormattable interface

Code-Snippet-Listing (snippet-source)

/* --------------------------------------------------------
 * We assume that you got somewhere an xSpreadsheetDocument
 * --------------------------------------------------------
 */
Object sheets = xSpreadsheetDocument.getSheets();
XIndexAccess xIndexedSheets = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, sheets);
Object sheet = xIndexedSheets.getByIndex(0);
XSpreadsheet xSpreadSheet_ = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, sheet);

// get a range with 10x10 range 
XCellRange rangeToFormat = xSpreadSheet.getCellRangeByPosition(0,0,9,9) 

// now retrieve the com.sun.star.table.XAutoFormattable-Interface
XAutoFormattable xAutoForm = (XAutoFormattable)UnoRuntime.queryInterface(XAutoFormattable.class, rangeToFormat);

/* 
 * Although this is an runtime exception we are catching it because
 * if the format is missing because any reason don't want to stop
 * the processing
 */
try {
    xAutoForm.autoFormat("OEUSH-Abbucher-Bericht");
} catch (IllegalArgumentException e) {
    e.printStackTrace();
}

Changelog

DateUserModification
2005-03-10tomsontomInitial version

and