From f804dd9e7b95b9bca025bfb6a46d3dcc4ac2ae12 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 20 Jul 2011 09:42:31 +0000 Subject: [PATCH] Fixed bug 51533 - Avoid exception when changing name of a sheet containing shared formulas git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1148673 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../usermodel/helpers/XSSFFormulaUtils.java | 2 +- .../poi/xssf/usermodel/TestXSSFWorkbook.java | 4 ++++ .../poi/hssf/usermodel/TestHSSFWorkbook.java | 5 ++++ .../poi/ss/usermodel/BaseTestWorkbook.java | 24 +++++++++++++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 2452027a51..967461af68 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51533 - Avoid exception when changing name of a sheet containing shared formulas Support for appending images to existing drawings in HSSF Added initial support for bookmarks in HWFP 46250 - Fixed cloning worksheets with images diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFFormulaUtils.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFFormulaUtils.java index cc354f277a..5161160c77 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFFormulaUtils.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFFormulaUtils.java @@ -120,7 +120,7 @@ public final class XSSFFormulaUtils { CTCellFormula f = cell.getCTCell().getF(); if (f != null) { String formula = f.getStringValue(); - if (formula != null) { + if (formula != null && formula.length() > 0) { int sheetIndex = _wb.getSheetIndex(cell.getSheet()); Ptg[] ptgs = FormulaParser.parse(formula, _fpwb, FormulaType.CELL, sheetIndex); String updatedFormula = FormulaRenderer.toFormulaString(frwb, ptgs); diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java index 5642f26f25..526ab9224a 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java @@ -428,4 +428,8 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook { assertFalse(wb.getForceFormulaRecalculation()); } + public void testChangeSheetNameWithSharedFormulas() { + changeSheetNameWithSharedFormulas("shared_formulas.xlsx"); + } + } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java index 4832f9f014..68ec3b2260 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java @@ -713,4 +713,9 @@ public final class TestHSSFWorkbook extends BaseTestWorkbook { assertEquals(3, bse.getRef()); } } + + public void testChangeSheetNameWithSharedFormulas() { + changeSheetNameWithSharedFormulas("shared_formulas.xls"); + } + } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java index 9ba714c748..b0bd1910ad 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestWorkbook.java @@ -568,4 +568,28 @@ public abstract class BaseTestWorkbook extends TestCase { assertEquals(6.0, evaluator.evaluate(cell2).getNumberValue()); } + public void changeSheetNameWithSharedFormulas(String sampleFile){ + Workbook wb = _testDataProvider.openSampleWorkbook(sampleFile); + + FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); + + Sheet sheet = wb.getSheetAt(0); + + for (int rownum = 1; rownum <= 40; rownum++) { + Cell cellA = sheet.getRow(1).getCell(0); + Cell cellB = sheet.getRow(1).getCell(1); + + assertEquals(cellB.getStringCellValue(), evaluator.evaluate(cellA).getStringValue()); + } + + wb.setSheetName(0, "Renamed by POI"); + evaluator.clearAllCachedResultValues(); + + for (int rownum = 1; rownum <= 40; rownum++) { + Cell cellA = sheet.getRow(1).getCell(0); + Cell cellB = sheet.getRow(1).getCell(1); + + assertEquals(cellB.getStringCellValue(), evaluator.evaluate(cellA).getStringValue()); + } + } } -- 2.39.5