From d2e7c9c8328aa8771018f393263836106ee0f34a Mon Sep 17 00:00:00 2001 From: Josh Micich Date: Tue, 18 Nov 2008 17:01:57 +0000 Subject: [PATCH] Fixed XSSFName and test cases git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@718651 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/usermodel/XSSFName.java | 18 ++++++++++--- .../xssf/usermodel/AllXSSFUsermodelTests.java | 27 ++++++++++++------- .../poi/xssf/usermodel/TestXSSFName.java | 2 +- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java index d4b87ab6d..ebdfdd723 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java @@ -16,10 +16,11 @@ ==================================================================== */ package org.apache.poi.xssf.usermodel; +import org.apache.poi.hssf.record.formula.Ptg; +import org.apache.poi.ss.formula.FormulaParser; +import org.apache.poi.ss.formula.FormulaType; import org.apache.poi.ss.usermodel.Name; import org.apache.poi.ss.util.AreaReference; -import org.apache.poi.util.POILogFactory; -import org.apache.poi.util.POILogger; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName; /** @@ -163,7 +164,7 @@ public final class XSSFName implements Name { * @deprecated (Nov 2008) Misleading name. Use {@link #setFormula(String)} instead. */ public void setReference(String ref){ - setFormula(ref); + setFormula(ref); } /** * Returns the reference of this named range, such as Sales!C20:C30. @@ -181,7 +182,16 @@ public final class XSSFName implements Name { * @throws IllegalArgumentException if the specified reference is unparsable */ public void setFormula(String formulaText) { - // TODO parse formula and throw IllegalArgumentException if problem + XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(workbook); + Ptg[] ptgs; + try { + ptgs = FormulaParser.parse(formulaText, fpb, FormulaType.CELL); // TODO - use type NAMEDRANGE + } catch (RuntimeException e) { + if (e.getClass().getName().startsWith(FormulaParser.class.getName())) { + throw new IllegalArgumentException("Unparsable formula '" + formulaText + "'"); + } + throw e; + } ctName.setStringValue(formulaText); } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java index ca15cc8ae..39d621865 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java @@ -35,26 +35,33 @@ public final class AllXSSFUsermodelTests { public static Test suite() { TestSuite result = new TestSuite(AllXSSFUsermodelTests.class.getName()); - result.addTestSuite(TestXSSFBorder.class); - result.addTestSuite(TestXSSFBugs.class); - result.addTestSuite(TestXSSFCellFill.class); - result.addTestSuite(TestXSSFHeaderFooter.class); - result.addTestSuite(TestXSSFSheetComments.class); - result.addTestSuite(TestColumnHelper.class); - result.addTestSuite(TestHeaderFooterHelper.class); result.addTestSuite(TestFormulaEvaluatorOnXSSF.class); + result.addTestSuite(TestSheetHiding.class); + result.addTestSuite(TestXSSFBugs.class); result.addTestSuite(TestXSSFCell.class); result.addTestSuite(TestXSSFCellStyle.class); result.addTestSuite(TestXSSFComment.class); result.addTestSuite(TestXSSFDialogSheet.class); + result.addTestSuite(TestXSSFDrawing.class); + result.addTestSuite(TestXSSFFont.class); result.addTestSuite(TestXSSFFormulaEvaluation.class); result.addTestSuite(TestXSSFHeaderFooter.class); + result.addTestSuite(TestXSSFHyperlink.class); + result.addTestSuite(TestXSSFName.class); + result.addTestSuite(TestXSSFPicture.class); + result.addTestSuite(TestXSSFPictureData.class); + result.addTestSuite(TestXSSFPrintSetup.class); + result.addTestSuite(TestXSSFRichTextString.class); result.addTestSuite(TestXSSFRow.class); result.addTestSuite(TestXSSFSheet.class); result.addTestSuite(TestXSSFWorkbook.class); - - result.addTestSuite(TestXSSFFont.class); - result.addTestSuite(TestXSSFPrintSetup.class); + + result.addTestSuite(TestXSSFBorder.class); + result.addTestSuite(TestXSSFCellFill.class); + result.addTestSuite(TestXSSFSheetComments.class); + + result.addTestSuite(TestColumnHelper.class); + result.addTestSuite(TestHeaderFooterHelper.class); return result; } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java index 9a9f0849f..3273bc03a 100755 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java @@ -47,7 +47,7 @@ public class TestXSSFName extends TestCase { assertEquals(ref1, name1.getReference()); assertEquals("Test1", name1.getSheetName()); - String ref2 = "Testing Named Ranges!$A$1:$B$1"; + String ref2 = "'Testing Named Ranges'!$A$1:$B$1"; name1.setReference(ref2); //XSSFName#setReference stores the reference in canonical form and puts the sheet name in single quotes assertEquals("'Testing Named Ranges'!$A$1:$B$1", name1.getReference());