Search in sources :

Example 1 with CFRuleBase

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);
}
Also used : CFRuleBase(org.apache.poi.hssf.record.CFRuleBase) Record(org.apache.poi.hssf.record.Record) CFRuleRecord(org.apache.poi.hssf.record.CFRuleRecord) CFRule12Record(org.apache.poi.hssf.record.CFRule12Record) CFHeader12Record(org.apache.poi.hssf.record.CFHeader12Record) CFHeaderRecord(org.apache.poi.hssf.record.CFHeaderRecord) CFHeaderBase(org.apache.poi.hssf.record.CFHeaderBase)

Example 2 with CFRuleBase

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();
}
Also used : CFHeader12Record(org.apache.poi.hssf.record.CFHeader12Record) CFRuleBase(org.apache.poi.hssf.record.CFRuleBase)

Example 3 with CFRuleBase

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;
}
Also used : CFRuleBase(org.apache.poi.hssf.record.CFRuleBase) Ptg(org.apache.poi.ss.formula.ptg.Ptg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) CFRule12Record(org.apache.poi.hssf.record.CFRule12Record) ArrayList(java.util.ArrayList) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 4 with CFRuleBase

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);
}
Also used : CFRuleBase(org.apache.poi.hssf.record.CFRuleBase) CFRecordsAggregate(org.apache.poi.hssf.record.aggregates.CFRecordsAggregate) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 5 with CFRuleBase

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
    }
}
Also used : CFRuleBase(org.apache.poi.hssf.record.CFRuleBase) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Aggregations

CFRuleBase (org.apache.poi.hssf.record.CFRuleBase)6 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)4 CFRule12Record (org.apache.poi.hssf.record.CFRule12Record)3 ArrayList (java.util.ArrayList)2 CFHeader12Record (org.apache.poi.hssf.record.CFHeader12Record)2 CFHeaderBase (org.apache.poi.hssf.record.CFHeaderBase)2 CFHeaderRecord (org.apache.poi.hssf.record.CFHeaderRecord)2 CFRuleRecord (org.apache.poi.hssf.record.CFRuleRecord)2 Record (org.apache.poi.hssf.record.Record)2 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 RecordStream (org.apache.poi.hssf.model.RecordStream)1 CFRecordsAggregate (org.apache.poi.hssf.record.aggregates.CFRecordsAggregate)1 AreaErrPtg (org.apache.poi.ss.formula.ptg.AreaErrPtg)1 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)1 Ptg (org.apache.poi.ss.formula.ptg.Ptg)1