You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by jo...@apache.org on 2008/06/05 05:12:35 UTC

svn commit: r663436 - in /poi/trunk/src: documentation/content/xdocs/ java/org/apache/poi/hssf/record/ testcases/org/apache/poi/hssf/record/

Author: josh
Date: Wed Jun  4 20:12:35 2008
New Revision: 663436

URL: http://svn.apache.org/viewvc?rev=663436&view=rev
Log:
Fix for bug 45123 - SharedFormulaRecord.convertSharedFormulas was ignoring token operand classes

Added:
    poi/trunk/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java
Modified:
    poi/trunk/src/documentation/content/xdocs/changes.xml
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/java/org/apache/poi/hssf/record/SharedFormulaRecord.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/AllRecordTests.java

Modified: poi/trunk/src/documentation/content/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/changes.xml?rev=663436&r1=663435&r2=663436&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/changes.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/changes.xml Wed Jun  4 20:12:35 2008
@@ -37,6 +37,7 @@
 
 		<!-- Don't forget to update status.xml too! -->
         <release version="3.1-final" date="2008-06-??">
+           <action dev="POI-DEVELOPERS" type="fix">45123 - Fixed SharedFormulaRecord.convertSharedFormulas() to propagate token operand classes</action>
            <action dev="POI-DEVELOPERS" type="fix">45087 - Correctly detect date formats like [Black]YYYY as being date based</action>
            <action dev="POI-DEVELOPERS" type="add">45060 - Improved token class transformation during formula parsing</action>
            <action dev="POI-DEVELOPERS" type="add">44840 - Improved handling of HSSFObjectData, especially for entries with data held not in POIFS</action>

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=663436&r1=663435&r2=663436&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Wed Jun  4 20:12:35 2008
@@ -34,6 +34,7 @@
 	<!-- Don't forget to update changes.xml too! -->
     <changes>
         <release version="3.1-final" date="2008-06-??">
+           <action dev="POI-DEVELOPERS" type="fix">45123 - Fixed SharedFormulaRecord.convertSharedFormulas() to propagate token operand classes</action>
            <action dev="POI-DEVELOPERS" type="fix">45087 - Correctly detect date formats like [Black]YYYY as being date based</action>
            <action dev="POI-DEVELOPERS" type="add">45060 - Improved token class transformation during formula parsing</action>
            <action dev="POI-DEVELOPERS" type="add">44840 - Improved handling of HSSFObjectData, especially for entries with data held not in POIFS</action>

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/SharedFormulaRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/SharedFormulaRecord.java?rev=663436&r1=663435&r2=663436&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/SharedFormulaRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/SharedFormulaRecord.java Wed Jun  4 20:12:35 2008
@@ -201,6 +201,10 @@
         if (ptgs != null)
           for (int k = 0; k < ptgs.size(); k++) {
             Ptg ptg = (Ptg) ptgs.get(k);
+            byte originalOperandClass = -1;
+            if (!ptg.isBaseToken()) {
+                originalOperandClass = ptg.getPtgClass();
+            }
             if (ptg instanceof RefNPtg) {
               RefNPtg refNPtg = (RefNPtg)ptg;
               ptg = new ReferencePtg(fixupRelativeRow(formulaRow,refNPtg.getRow(),refNPtg.isRowRelative()),
@@ -249,7 +253,11 @@
                                 areaNAPtg.isLastRowRelative(),
                                 areaNAPtg.isFirstColRelative(),
                                 areaNAPtg.isLastColRelative());
-            } 
+            }
+            if (!ptg.isBaseToken()) {
+                ptg.setClass(originalOperandClass);
+            }
+            
             newPtgStack.add(ptg);
         }
         return newPtgStack;

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/AllRecordTests.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/AllRecordTests.java?rev=663436&r1=663435&r2=663436&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/AllRecordTests.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/AllRecordTests.java Wed Jun  4 20:12:35 2008
@@ -95,6 +95,7 @@
 		result.addTestSuite(TestSeriesTextRecord.class);
 		result.addTestSuite(TestSeriesToChartGroupRecord.class);
 		result.addTestSuite(TestSheetPropertiesRecord.class);
+		result.addTestSuite(TestSharedFormulaRecord.class);
 		result.addTestSuite(TestStringRecord.class);
 		result.addTestSuite(TestSubRecord.class);
 		result.addTestSuite(TestSupBookRecord.class);

Added: poi/trunk/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java?rev=663436&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/TestSharedFormulaRecord.java Wed Jun  4 20:12:35 2008
@@ -0,0 +1,97 @@
+/* ====================================================================
+   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.
+==================================================================== */
+
+package org.apache.poi.hssf.record;
+
+import java.util.List;
+import java.util.Stack;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.ComparisonFailure;
+import junit.framework.TestCase;
+
+import org.apache.poi.hssf.record.formula.Ptg;
+import org.apache.poi.hssf.record.formula.RefAPtg;
+
+/**
+ * @author Josh Micich
+ */
+public final class TestSharedFormulaRecord extends TestCase {
+
+	/**
+	 * Binary data for an encoded formula.  Taken from attachment 22062 (bugzilla 45123/45421).
+	 * The shared formula is in Sheet1!C6:C21, with text "SUMPRODUCT(--(End_Acct=$C6),--(End_Bal))"
+	 * This data is found at offset 0x1A4A (within the shared formula record).
+	 * The critical thing about this formula is that it contains shared formula tokens (tRefN*,
+	 * tAreaN*) with operand class 'array'.
+	 */
+	private static final byte[] SHARED_FORMULA_WITH_REF_ARRAYS_DATA = {
+		0x1A, 0x00,
+		0x63, 0x02, 0x00, 0x00, 0x00,
+		0x6C, 0x00, 0x00, 0x02, (byte)0x80,  // tRefNA
+		0x0B,
+		0x15,
+		0x13,
+		0x13,
+		0x63, 0x03, 0x00, 0x00, 0x00,
+		0x15,
+		0x13,
+		0x13,
+		0x42, 0x02, (byte)0xE4, 0x00,
+	};
+	
+	/**
+	 * The method <tt>SharedFormulaRecord.convertSharedFormulas()</tt> converts formulas from
+	 * 'shared formula' to 'single cell formula' format.  It is important that token operand
+	 * classes are preserved during this transformation, because Excel may not tolerate the
+	 * incorrect encoding.  The formula here is one such example (Excel displays #VALUE!).
+	 */
+	public void testConvertSharedFormulasOperandClasses_bug45123() {
+		
+		TestcaseRecordInputStream in = new TestcaseRecordInputStream(0, SHARED_FORMULA_WITH_REF_ARRAYS_DATA);
+		short encodedLen = in.readShort();
+		Stack sharedFormula = Ptg.createParsedExpressionTokens(encodedLen, in);
+		
+		Stack convertedFormula = SharedFormulaRecord.convertSharedFormulas(sharedFormula, 100, 200);
+		
+		RefAPtg refPtg = (RefAPtg) convertedFormula.get(1);
+		assertEquals("$C101", refPtg.toFormulaString(null));
+		if (refPtg.getPtgClass() == Ptg.CLASS_REF) {
+			throw new AssertionFailedError("Identified bug 45123");
+		}
+		
+		confirmOperandClasses(toPtgArray(sharedFormula), toPtgArray(convertedFormula));
+	}
+
+	private static void confirmOperandClasses(Ptg[] originalPtgs, Ptg[] convertedPtgs) {
+		assertEquals(originalPtgs.length, convertedPtgs.length);
+		for (int i = 0; i < convertedPtgs.length; i++) {
+			Ptg originalPtg = originalPtgs[i];
+			Ptg convertedPtg = convertedPtgs[i];
+			if (originalPtg.getPtgClass() != convertedPtg.getPtgClass()) {
+				throw new ComparisonFailure("Different operand class for token[" + i + "]",
+						String.valueOf(originalPtg.getPtgClass()), String.valueOf(convertedPtg.getPtgClass()));
+			}
+		}
+	}
+
+	private static Ptg[] toPtgArray(List list) {
+		Ptg[] result = new Ptg[list.size()];
+		list.toArray(result);
+		return result;
+	}
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org