From 99fc06a4c6b67aa0505ecc9ca0f0574819288152 Mon Sep 17 00:00:00 2001 From: Danny Mui Date: Mon, 2 Jun 2003 02:47:44 +0000 Subject: [PATCH] Fixed support for Cloning SharedFormulas and FormulaAggregates leaving out other values after it. git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353115 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/record/FormulaRecord.java | 4 +- .../aggregates/FormulaRecordAggregate.java | 28 ++++++- .../poi/hssf/record/formula/ExpPtg.java | 5 +- .../org/apache/poi/hssf/model/SheetTest.java | 83 +++++++++++++++++++ 4 files changed, 116 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/FormulaRecord.java b/src/java/org/apache/poi/hssf/record/FormulaRecord.java index ad926be474..c1ed74a9d5 100644 --- a/src/java/org/apache/poi/hssf/record/FormulaRecord.java +++ b/src/java/org/apache/poi/hssf/record/FormulaRecord.java @@ -607,8 +607,8 @@ public class FormulaRecord if (field_8_parsed_expr != null) size = field_8_parsed_expr.size(); for (int i=0; i< size; i++) { - Ptg ptg = (Ptg)((Ptg)field_8_parsed_expr.get(i)).clone(); - rec.field_8_parsed_expr.set(i, ptg); + Ptg ptg = (Ptg)((Ptg)field_8_parsed_expr.get(i)).clone(); + rec.field_8_parsed_expr.add(i, ptg); } rec.all_data = all_data; return rec; diff --git a/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java index 7d5b0b048d..9a7575c21e 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/FormulaRecordAggregate.java @@ -82,6 +82,19 @@ public class FormulaRecordAggregate this.stringRecord = stringRecord; } + /** + * Used only in the clone + * @param formulaRecord + * @param stringRecord + * @param sharedRecord + */ + public FormulaRecordAggregate( FormulaRecord formulaRecord, StringRecord stringRecord, SharedFormulaRecord sharedRecord) + { + this.formulaRecord = formulaRecord; + this.stringRecord = stringRecord; + this.sharedFormulaRecord = sharedRecord; + } + protected void validateSid( short id ) @@ -221,7 +234,10 @@ public class FormulaRecordAggregate * @see java.lang.Object#clone() */ public Object clone() { - return new FormulaRecordAggregate((FormulaRecord) this.formulaRecord.clone(), (StringRecord) this.stringRecord.clone()); + StringRecord clonedString = (stringRecord == null) ? null : (StringRecord)stringRecord.clone(); + SharedFormulaRecord clonedShared = (sharedFormulaRecord == null) ? null : (SharedFormulaRecord)sharedFormulaRecord.clone(); + + return new FormulaRecordAggregate((FormulaRecord) this.formulaRecord.clone(), clonedString, clonedShared); } @@ -241,4 +257,14 @@ public class FormulaRecordAggregate this.sharedFormulaRecord = sharedFormulaRecord; } + /* + * Setting to true so that this value does not abort the whole ValueAggregation + * (non-Javadoc) + * @see org.apache.poi.hssf.record.Record#isInValueSection() + */ + public boolean isInValueSection() { + + return true; + } + } diff --git a/src/java/org/apache/poi/hssf/record/formula/ExpPtg.java b/src/java/org/apache/poi/hssf/record/formula/ExpPtg.java index 576bce3316..b100a1a762 100644 --- a/src/java/org/apache/poi/hssf/record/formula/ExpPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/ExpPtg.java @@ -110,7 +110,10 @@ public class ExpPtg public byte getDefaultOperandClass() {return Ptg.CLASS_VALUE;} public Object clone() { - throw new RuntimeException("NO IDEA SHARED FORMULA EXP PTG"); + //can't clone one that doesnt have data can we?? + if (this.existing == null) throw new RuntimeException("NO IDEA SHARED FORMULA EXP PTG"); + + return new ExpPtg(this.existing, 0); } } diff --git a/src/testcases/org/apache/poi/hssf/model/SheetTest.java b/src/testcases/org/apache/poi/hssf/model/SheetTest.java index 9a78fd63c9..a2e997d8f7 100644 --- a/src/testcases/org/apache/poi/hssf/model/SheetTest.java +++ b/src/testcases/org/apache/poi/hssf/model/SheetTest.java @@ -6,9 +6,18 @@ import java.util.List; import junit.framework.TestCase; +import org.apache.poi.hssf.record.BOFRecord; +import org.apache.poi.hssf.record.BlankRecord; import org.apache.poi.hssf.record.ColumnInfoRecord; +import org.apache.poi.hssf.record.EOFRecord; +import org.apache.poi.hssf.record.FormulaRecord; +import org.apache.poi.hssf.record.LabelSSTRecord; import org.apache.poi.hssf.record.RowRecord; +import org.apache.poi.hssf.record.SharedFormulaRecord; import org.apache.poi.hssf.record.StringRecord; +import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate; +import org.apache.poi.hssf.record.aggregates.ValueRecordsAggregate; +import org.apache.poi.hssf.record.formula.ExpPtg; /** * @author Tony Poppleton @@ -147,6 +156,80 @@ public class SheetTest extends TestCase } + /** + * Make sure that a sheet with the sharedformula works when cloned, it used to not fail + * + */ + public void testSharedFormulaClone() { + Sheet sheet; + List records = new ArrayList(); + + FormulaRecord formula = new FormulaRecord(); + byte[] array = {(byte)0,(byte)0,(byte)0,(byte)0,(byte)0}; + ExpPtg expPtg = new ExpPtg(array, 0); + //an empty expptg will not be cloned + + formula.setExpressionLength((short)5); + formula.pushExpressionToken(expPtg); + + records.add(new RowRecord()); + records.add(formula); + records.add(new SharedFormulaRecord()); + + + sheet = Sheet.createSheet(records, 0); + + sheet.cloneSheet(); + + } + + + /** + * FormulaRecordAggregates within blanks/values cause values afterwards to not be aggregated. + * This usually occurs during clones + * + */ + public void testValueAggregateWithSharedFormulas() { + Sheet sheet; + List records = new ArrayList(); + + FormulaRecord formula = new FormulaRecord(); + StringRecord stringRecord = new StringRecord(); + FormulaRecordAggregate formulaAggregate = new FormulaRecordAggregate(formula, stringRecord); + + formula.setExpressionLength((short)5); + formula.pushExpressionToken(new ExpPtg()); + BlankRecord blank = new BlankRecord(); + blank.setColumn((short)0); + blank.setRow(1); + + records.add(new BOFRecord()); + records.add(new RowRecord()); + + blank.setColumn((short)0); + blank.setRow(1); + + records.add(blank); + records.add(new LabelSSTRecord()); + records.add(formula); + records.add(new SharedFormulaRecord()); + records.add(formulaAggregate); + + blank = new BlankRecord(); + blank.setColumn((short)0); + blank.setRow(2); + + + records.add(blank); + records.add(new EOFRecord()); + + sheet = Sheet.createSheet(records, 0); + ValueRecordsAggregate valueAggregate = sheet.cells; + + assertEquals("Aggregated cells not correct, the blank record following sharedformula is not here", 3, valueAggregate.getPhysicalNumberOfCells()); + + + } public static void main(String [] args) { System.out