use of org.apache.poi.hssf.record.CFRuleBase in project poi by apache.
the class CFRecordsAggregate method createCFAggregate.
/**
* Create CFRecordsAggregate from a list of CF Records
* @param rs - the stream to read from
* @return CFRecordsAggregate object
*/
public static CFRecordsAggregate createCFAggregate(RecordStream rs) {
Record rec = rs.getNext();
if (rec.getSid() != CFHeaderRecord.sid && rec.getSid() != CFHeader12Record.sid) {
throw new IllegalStateException("next record sid was " + rec.getSid() + " instead of " + CFHeaderRecord.sid + " or " + CFHeader12Record.sid + " as expected");
}
CFHeaderBase header = (CFHeaderBase) rec;
int nRules = header.getNumberOfConditionalFormats();
CFRuleBase[] rules = new CFRuleBase[nRules];
for (int i = 0; i < rules.length; i++) {
rules[i] = (CFRuleBase) rs.getNext();
}
return new CFRecordsAggregate(header, rules);
}
use of org.apache.poi.hssf.record.CFRuleBase in project poi by apache.
the class CFRecordsAggregate method toString.
/**
* String representation of CFRecordsAggregate
*/
public String toString() {
StringBuilder buffer = new StringBuilder();
String type = "CF";
if (header instanceof CFHeader12Record) {
type = "CF12";
}
buffer.append("[").append(type).append("]\n");
if (header != null) {
buffer.append(header);
}
for (CFRuleBase cfRule : rules) {
buffer.append(cfRule);
}
buffer.append("[/").append(type).append("]\n");
return buffer.toString();
}
use of org.apache.poi.hssf.record.CFRuleBase in project poi by apache.
the class CFRecordsAggregate method updateFormulasAfterCellShift.
/**
* @return <code>false</code> if this whole {@link CFHeaderRecord} / {@link CFRuleRecord}s should be deleted
*/
public boolean updateFormulasAfterCellShift(FormulaShifter shifter, int currentExternSheetIx) {
CellRangeAddress[] cellRanges = header.getCellRanges();
boolean changed = false;
List<CellRangeAddress> temp = new ArrayList<CellRangeAddress>();
for (CellRangeAddress craOld : cellRanges) {
CellRangeAddress craNew = shiftRange(shifter, craOld, currentExternSheetIx);
if (craNew == null) {
changed = true;
continue;
}
temp.add(craNew);
if (craNew != craOld) {
changed = true;
}
}
if (changed) {
int nRanges = temp.size();
if (nRanges == 0) {
return false;
}
CellRangeAddress[] newRanges = new CellRangeAddress[nRanges];
temp.toArray(newRanges);
header.setCellRanges(newRanges);
}
for (CFRuleBase rule : rules) {
Ptg[] ptgs;
ptgs = rule.getParsedExpression1();
if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
rule.setParsedExpression1(ptgs);
}
ptgs = rule.getParsedExpression2();
if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
rule.setParsedExpression2(ptgs);
}
if (rule instanceof CFRule12Record) {
CFRule12Record rule12 = (CFRule12Record) rule;
ptgs = rule12.getParsedExpressionScale();
if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
rule12.setParsedExpressionScale(ptgs);
}
}
}
return true;
}
use of org.apache.poi.hssf.record.CFRuleBase in project poi by apache.
the class HSSFSheetConditionalFormatting method addConditionalFormatting.
/**
* Allows to add a new Conditional Formatting set to the sheet.
*
* @param regions - list of rectangular regions to apply conditional formatting rules
* @param cfRules - set of up to three conditional formatting rules
*
* @return index of the newly created Conditional Formatting object
*/
public int addConditionalFormatting(CellRangeAddress[] regions, HSSFConditionalFormattingRule[] cfRules) {
if (regions == null) {
throw new IllegalArgumentException("regions must not be null");
}
for (CellRangeAddress range : regions) range.validate(SpreadsheetVersion.EXCEL97);
if (cfRules == null) {
throw new IllegalArgumentException("cfRules must not be null");
}
if (cfRules.length == 0) {
throw new IllegalArgumentException("cfRules must not be empty");
}
if (cfRules.length > 3) {
throw new IllegalArgumentException("Number of rules must not exceed 3");
}
CFRuleBase[] rules = new CFRuleBase[cfRules.length];
for (int i = 0; i != cfRules.length; i++) {
rules[i] = cfRules[i].getCfRuleRecord();
}
CFRecordsAggregate cfra = new CFRecordsAggregate(regions, rules);
return _conditionalFormattingTable.add(cfra);
}
use of org.apache.poi.hssf.record.CFRuleBase in project poi by apache.
the class TestCFRecordsAggregate method testCantMixTypes.
public void testCantMixTypes() {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
CellRangeAddress[] cellRanges = { new CellRangeAddress(0, 1, 0, 0), new CellRangeAddress(0, 1, 2, 2) };
CFRuleBase[] rules = { CFRuleRecord.create(sheet, "7"), CFRule12Record.create(sheet, ComparisonOperator.BETWEEN, "2", "5") };
try {
new CFRecordsAggregate(cellRanges, rules);
fail("Shouldn't be able to mix between types");
} catch (IllegalArgumentException e) {
// expected here
}
rules = new CFRuleBase[] { CFRuleRecord.create(sheet, "7") };
CFRecordsAggregate agg = new CFRecordsAggregate(cellRanges, rules);
assertTrue(agg.getHeader().getNeedRecalculation());
try {
agg.addRule(CFRule12Record.create(sheet, "7"));
fail("Shouldn't be able to mix between types");
} catch (IllegalArgumentException e) {
// expected here
}
}
Aggregations