mirror of https://github.com/apache/poi.git
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
This commit is contained in:
parent
015b8ab668
commit
99fc06a4c6
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue