View | Details | Raw Unified | Return to bug 46547
Collapse All | Expand All

(-)a/src/java/org/apache/poi/hssf/model/RecordOrderer.java (+7 lines)
Lines 159-164 final class RecordOrderer { Link Here
159
			if (rb instanceof MergedCellsTable) {
159
			if (rb instanceof MergedCellsTable) {
160
				return i + 1;
160
				return i + 1;
161
			}
161
			}
162
			if (rb instanceof DataValidityTable) {
163
				continue;
164
			}
165
			
162
			Record rec = (Record) rb;
166
			Record rec = (Record) rb;
163
			switch (rec.getSid()) {
167
			switch (rec.getSid()) {
164
				case WindowTwoRecord.sid:
168
				case WindowTwoRecord.sid:
Lines 169-175 final class RecordOrderer { Link Here
169
				// MergedCellsTable usually here 
173
				// MergedCellsTable usually here 
170
				case UnknownRecord.LABELRANGES_015F:
174
				case UnknownRecord.LABELRANGES_015F:
171
				case UnknownRecord.PHONETICPR_00EF:
175
				case UnknownRecord.PHONETICPR_00EF:
176
					// ConditionalFormattingTable goes here
172
					return i + 1;
177
					return i + 1;
178
				// HyperlinkTable (not aggregated by POI yet)
179
				// DataValidityTable
173
			}
180
			}
174
		}
181
		}
175
		throw new RuntimeException("Did not find Window2 record");
182
		throw new RuntimeException("Did not find Window2 record");
(-)a/src/java/org/apache/poi/hssf/model/Sheet.java (-1 / +3 lines)
Lines 435-441 public final class Sheet implements Model { Link Here
435
     */
435
     */
436
    public void updateFormulasAfterCellShift(FormulaShifter shifter, int externSheetIndex) {
436
    public void updateFormulasAfterCellShift(FormulaShifter shifter, int externSheetIndex) {
437
        getRowsAggregate().updateFormulasAfterRowShift(shifter, externSheetIndex);
437
        getRowsAggregate().updateFormulasAfterRowShift(shifter, externSheetIndex);
438
        getConditionalFormattingTable().updateFormulasAfterCellShift(shifter, externSheetIndex);
438
        if (condFormatting != null) {
439
            getConditionalFormattingTable().updateFormulasAfterCellShift(shifter, externSheetIndex);
440
        }
439
        // TODO - adjust data validations
441
        // TODO - adjust data validations
440
    }
442
    }
441
443
(-)a/src/testcases/org/apache/poi/hssf/model/TestSheet.java (+46 lines)
Lines 35-49 import org.apache.poi.hssf.record.GutsRecord; Link Here
35
import org.apache.poi.hssf.record.IndexRecord;
35
import org.apache.poi.hssf.record.IndexRecord;
36
import org.apache.poi.hssf.record.MergeCellsRecord;
36
import org.apache.poi.hssf.record.MergeCellsRecord;
37
import org.apache.poi.hssf.record.Record;
37
import org.apache.poi.hssf.record.Record;
38
import org.apache.poi.hssf.record.RecordBase;
38
import org.apache.poi.hssf.record.RowRecord;
39
import org.apache.poi.hssf.record.RowRecord;
39
import org.apache.poi.hssf.record.StringRecord;
40
import org.apache.poi.hssf.record.StringRecord;
40
import org.apache.poi.hssf.record.UncalcedRecord;
41
import org.apache.poi.hssf.record.UncalcedRecord;
41
import org.apache.poi.hssf.record.WindowTwoRecord;
42
import org.apache.poi.hssf.record.WindowTwoRecord;
42
import org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate;
43
import org.apache.poi.hssf.record.aggregates.ColumnInfoRecordsAggregate;
44
import org.apache.poi.hssf.record.aggregates.ConditionalFormattingTable;
43
import org.apache.poi.hssf.record.aggregates.MergedCellsTable;
45
import org.apache.poi.hssf.record.aggregates.MergedCellsTable;
44
import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
46
import org.apache.poi.hssf.record.aggregates.PageSettingsBlock;
45
import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
47
import org.apache.poi.hssf.record.aggregates.RowRecordsAggregate;
46
import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
48
import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
49
import org.apache.poi.hssf.record.formula.FormulaShifter;
47
import org.apache.poi.hssf.usermodel.HSSFCell;
50
import org.apache.poi.hssf.usermodel.HSSFCell;
48
import org.apache.poi.hssf.usermodel.HSSFRow;
51
import org.apache.poi.hssf.usermodel.HSSFRow;
49
import org.apache.poi.hssf.usermodel.HSSFSheet;
52
import org.apache.poi.hssf.usermodel.HSSFSheet;
Lines 582-585 public final class TestSheet extends TestCase { Link Here
582
            throw e;
585
            throw e;
583
        }
586
        }
584
    }
587
    }
588
    
589
    /**
590
     * Prior to the fix for bug 46547, shifting formulas would have the side-effect 
591
     * of creating a {@link ConditionalFormattingTable}.  There was no impairment to
592
     * functionality since empty record aggregates are equivalent to missing record 
593
     * aggregates. However, since this unnecessary creation helped expose bug 46547b,
594
     * and since there is a slight performance hit the fix was made to avoid it.
595
     */
596
    public void testShiftFormulasAddCondFormat_bug46547() {
597
        // Create a sheet with data validity (similar to bugzilla attachment id=23131).
598
        Sheet sheet = Sheet.createSheet();
599
600
        List sheetRecs = sheet.getRecords();
601
        assertEquals(22, sheetRecs.size());
602
        
603
        FormulaShifter shifter = FormulaShifter.createForRowShift(0, 0, 0, 1);
604
        sheet.updateFormulasAfterCellShift(shifter, 0);
605
        if (sheetRecs.size() == 23 && sheetRecs.get(21) instanceof ConditionalFormattingTable) {
606
            throw new AssertionFailedError("Identified bug 46547a");
607
        }
608
        assertEquals(22, sheetRecs.size());
609
        
610
    }
611
    /**
612
     * Bug 46547 happened when attempting to add conditional formatting to a sheet
613
     * which already had data validity constraints.
614
     */
615
    public void testAddCondFormatAfterDataValidation_bug46547() {
616
        // Create a sheet with data validity (similar to bugzilla attachment id=23131).
617
        Sheet sheet = Sheet.createSheet();
618
        sheet.getOrCreateDataValidityTable();
619
620
        ConditionalFormattingTable cft;
621
        // attempt to add conditional formatting
622
        try {
623
624
            cft = sheet.getConditionalFormattingTable(); // lazy getter
625
        } catch (ClassCastException e) {
626
            throw new AssertionFailedError("Identified bug 46547b");
627
        }
628
        assertNotNull(cft);
629
    }
630
 
585
}
631
}

Return to bug 46547