Difference between revisions of "Documentation/DevGuide/Spreadsheets/Access to Formulas, Values and Errors"
m |
|||
Line 10: | Line 10: | ||
{{DISPLAYTITLE:Access to Formulas, Values and Errors}} | {{DISPLAYTITLE:Access to Formulas, Values and Errors}} | ||
The cell interface <idl>com.sun.star.table.XCell</idl> provides methods to access the value, formula, content type, and error code of a single cell: | The cell interface <idl>com.sun.star.table.XCell</idl> provides methods to access the value, formula, content type, and error code of a single cell: | ||
− | + | <syntaxhighlight lang="idl"> | |
void setValue( [in] double nValue) | void setValue( [in] double nValue) | ||
double getValue() | double getValue() | ||
Line 17: | Line 17: | ||
com::sun::star::table::CellContentType getType() | com::sun::star::table::CellContentType getType() | ||
long getError() | long getError() | ||
− | + | </syntaxhighlight> | |
The value of a cell is a floating-point number. To set a formula to a cell, the whole formula string has to be passed including the leading equality sign. The function names must be in English. | The value of a cell is a floating-point number. To set a formula to a cell, the whole formula string has to be passed including the leading equality sign. The function names must be in English. | ||
Line 26: | Line 26: | ||
The following code fragment shows how to access and modify the content, and formatting of single cells. The <code>xRange</code> is an existing cell range (a <idl>com.sun.star.table.XCellRange</idl> interface, described in [[Documentation/DevGuide/Spreadsheets/Cell Ranges|Cell Ranges]]). The method <code>getCellByPosition()</code> is provided by this interface. | The following code fragment shows how to access and modify the content, and formatting of single cells. The <code>xRange</code> is an existing cell range (a <idl>com.sun.star.table.XCellRange</idl> interface, described in [[Documentation/DevGuide/Spreadsheets/Cell Ranges|Cell Ranges]]). The method <code>getCellByPosition()</code> is provided by this interface. | ||
<!--[SOURCE:Spreadsheet/GeneralTableSample.java]--> | <!--[SOURCE:Spreadsheet/GeneralTableSample.java]--> | ||
− | + | <syntaxhighlight lang="java"> | |
com.sun.star.beans.XPropertySet xPropSet = null; | com.sun.star.beans.XPropertySet xPropSet = null; | ||
com.sun.star.table.XCell xCell = null; | com.sun.star.table.XCell xCell = null; | ||
Line 62: | Line 62: | ||
com.sun.star.beans.XPropertySet.class, xCell); | com.sun.star.beans.XPropertySet.class, xCell); | ||
xPropSet.setPropertyValue("CellBackColor", new Integer(nValue)); | xPropSet.setPropertyValue("CellBackColor", new Integer(nValue)); | ||
− | + | </syntaxhighlight> | |
Note that since the only data attributes a cell has for representing numbers is value and formula, other types available in {{AOo}} Calc have to be translated before you can use them. See <idl>com.sun.star.util.NumberFormat</idl> for the standard formats. | Note that since the only data attributes a cell has for representing numbers is value and formula, other types available in {{AOo}} Calc have to be translated before you can use them. See <idl>com.sun.star.util.NumberFormat</idl> for the standard formats. | ||
Latest revision as of 15:31, 3 January 2021
The cell interface com.sun.star.table.XCell provides methods to access the value, formula, content type, and error code of a single cell:
void setValue( [in] double nValue) double getValue() void setFormula( [in] string aFormula) string getFormula() com::sun::star::table::CellContentType getType() long getError()
The value of a cell is a floating-point number. To set a formula to a cell, the whole formula string has to be passed including the leading equality sign. The function names must be in English.
The method getType()
returns a value of the enumeration com.sun.star.table.CellContentType indicating the type of the cell content.
The following code fragment shows how to access and modify the content, and formatting of single cells. The xRange
is an existing cell range (a com.sun.star.table.XCellRange interface, described in Cell Ranges). The method getCellByPosition()
is provided by this interface.
com.sun.star.beans.XPropertySet xPropSet = null; com.sun.star.table.XCell xCell = null; // *** Access and modify a VALUE CELL *** xCell = xRange.getCellByPosition(0, 0); // Set cell value. xCell.setValue(1234); // Get cell value. double nDblValue = xCell.getValue() * 2; xRange.getCellByPosition(0, 1).setValue(nDblValue); // *** Create a FORMULA CELL and query error type *** xCell = xRange.getCellByPosition(0, 2); // Set formula string. xCell.setFormula("=1/0"); // Get error type. boolean bValid = (xCell.getError() == 0); // Get formula string. String aText = "The formula " + xCell.getFormula() + " is "; aText += bValid ? "valid." : "erroneous."; // *** Insert a TEXT CELL using the XText interface *** xCell = xRange.getCellByPosition( 0, 3 ); com.sun.star.text.XText xCellText = (com.sun.star.text.XText) UnoRuntime.queryInterface( com.sun.star.text.XText.class, xCell ); com.sun.star.text.XTextCursor xTextCursor = xCellText.createTextCursor(); xCellText.insertString( xTextCursor, aText, false ); // *** Change cell properties *** int nValue = bValid ? 0x00FF00 : 0xFF4040; xPropSet = (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface( com.sun.star.beans.XPropertySet.class, xCell); xPropSet.setPropertyValue("CellBackColor", new Integer(nValue));
Note that since the only data attributes a cell has for representing numbers is value and formula, other types available in Apache OpenOffice Calc have to be translated before you can use them. See com.sun.star.util.NumberFormat for the standard formats.
NumberFormat | Mapping to double |
---|---|
DATE | 1 = one day; day 1 is 1899-12-31 |
TIME | Fraction of a 24h day (1 = 24 hours) |
PERCENT | 1 = 100% |
CURRENCY | 1 = $1, £1 etc |
LOGICAL | FALSE if zero, TRUE otherwise |
How this value will be shown to the user depends on the cell's Number Format.
Content on this page is licensed under the Public Documentation License (PDL). |