From a3381dadcd23e55226e0ee32fe0b8d3931f60c88 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 3 Mar 2008 16:55:00 +0000 Subject: [PATCH] Patch from Josh from bug #44508 - Fix formula evaluation with evaluateInCell on boolean formulas git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@633169 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 2 + .../apache/poi/hssf/usermodel/HSSFCell.java | 30 ++++++++++++- .../poi/hssf/usermodel/TestBug44508.java | 42 +++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/scratchpad/testcases/org/apache/poi/hssf/usermodel/TestBug44508.java diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 23143a1bde..c32f5ffc6e 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,6 +36,7 @@ + 44508 - Fix formula evaluation with evaluateInCell on boolean formulas 44510 - Fix how DVALRecord works with dropdowns 44495 - Handle named cell ranges in formulas that have lower case parts 44491 - Don't have the new-style "HPSF properties are always available" affect the old-style use of HPSF alongside HSSF diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index ae32a1cc7a..bdb62ad43b 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,6 +33,8 @@ + 44508 - Fix formula evaluation with evaluateInCell on boolean formulas + 44510 - Fix how DVALRecord works with dropdowns 44495 - Handle named cell ranges in formulas that have lower case parts 44491 - Don't have the new-style "HPSF properties are always available" affect the old-style use of HPSF alongside HSSF 44471 - Crystal Reports generates files with short StyleRecords, which isn't allowed in the spec. Work around this diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java index d07e7937fc..05cee4e8d2 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java @@ -452,7 +452,7 @@ public class HSSFCell boolRec.setColumn(col); if (setValue) { - boolRec.setValue(getBooleanCellValue()); + boolRec.setValue(convertCellValueToBoolean()); } boolRec.setXFIndex(styleIndex); boolRec.setRow(row); @@ -825,6 +825,34 @@ public class HSSFCell } (( BoolErrRecord ) record).setValue(value); } + /** + * Chooses a new boolean value for the cell when its type is changing.

+ * + * Usually the caller is calling setCellType() with the intention of calling + * setCellValue(boolean) straight afterwards. This method only exists to give + * the cell a somewhat reasonable value until the setCellValue() call (if at all). + * TODO - perhaps a method like setCellTypeAndValue(int, Object) should be introduced to avoid this + */ + private boolean convertCellValueToBoolean() { + + switch (cellType) { + case CELL_TYPE_BOOLEAN: + return (( BoolErrRecord ) record).getBooleanValue(); + case CELL_TYPE_STRING: + return Boolean.valueOf(((StringRecord)record).getString()).booleanValue(); + case CELL_TYPE_NUMERIC: + return ((NumberRecord)record).getValue() != 0; + + // All other cases convert to false + // These choices are not well justified. + case CELL_TYPE_FORMULA: + // should really evaluate, but HSSFCell can't call HSSFFormulaEvaluator + case CELL_TYPE_ERROR: + case CELL_TYPE_BLANK: + return false; + } + throw new RuntimeException("Unexpected cell type (" + cellType + ")"); + } /** * get the value of the cell as a boolean. For strings, numbers, and errors, we throw an exception. diff --git a/src/scratchpad/testcases/org/apache/poi/hssf/usermodel/TestBug44508.java b/src/scratchpad/testcases/org/apache/poi/hssf/usermodel/TestBug44508.java new file mode 100644 index 0000000000..3362f3c3e7 --- /dev/null +++ b/src/scratchpad/testcases/org/apache/poi/hssf/usermodel/TestBug44508.java @@ -0,0 +1,42 @@ +package org.apache.poi.hssf.usermodel; +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +import junit.framework.TestCase; + +public class TestBug44508 extends TestCase { + protected String cwd = System.getProperty("HSSF.testdata.path"); + + public void testEvaluateBooleanInCell_bug44508() { + HSSFWorkbook wb = new HSSFWorkbook(); + HSSFSheet sheet = wb.createSheet(); + wb.setSheetName(0, "Sheet1"); + HSSFRow row = sheet.createRow(0); + HSSFCell cell = row.createCell((short)0); + + cell.setCellFormula("1=1"); + + HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(sheet, wb); + fe.setCurrentRow(row); + try { + fe.evaluateInCell(cell); + } catch (NumberFormatException e) { + fail("Identified bug 44508"); + } + assertEquals(true, cell.getBooleanCellValue()); + } +} -- 2.39.5