Search in sources :

Example 11 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class FormulaRecordAggregate method getArrayFormulaRange.

public CellRangeAddress getArrayFormulaRange() {
    if (_sharedFormulaRecord != null) {
        throw new IllegalStateException("not an array formula cell.");
    }
    CellReference expRef = _formulaRecord.getFormula().getExpReference();
    if (expRef == null) {
        throw new IllegalStateException("not an array formula cell.");
    }
    ArrayRecord arec = _sharedValueManager.getArrayRecord(expRef.getRow(), expRef.getCol());
    if (arec == null) {
        throw new IllegalStateException("ArrayRecord was not found for the locator " + expRef.formatAsString());
    }
    CellRangeAddress8Bit a = arec.getRange();
    return new CellRangeAddress(a.getFirstRow(), a.getLastRow(), a.getFirstColumn(), a.getLastColumn());
}
Also used : ArrayRecord(org.apache.poi.hssf.record.ArrayRecord) CellRangeAddress8Bit(org.apache.poi.hssf.util.CellRangeAddress8Bit) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) CellReference(org.apache.poi.ss.util.CellReference)

Example 12 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class ConditionalFormattingEvaluator method getRules.

/**
     * lazy load by sheet since reading can be expensive
     * 
     * @param sheet
     * @return unmodifiable list of rules
     */
protected List<EvaluationConditionalFormatRule> getRules(Sheet sheet) {
    final String sheetName = sheet.getSheetName();
    List<EvaluationConditionalFormatRule> rules = formats.get(sheetName);
    if (rules == null) {
        if (formats.containsKey(sheetName)) {
            return Collections.emptyList();
        }
        final SheetConditionalFormatting scf = sheet.getSheetConditionalFormatting();
        final int count = scf.getNumConditionalFormattings();
        rules = new ArrayList<EvaluationConditionalFormatRule>(count);
        formats.put(sheetName, rules);
        for (int i = 0; i < count; i++) {
            ConditionalFormatting f = scf.getConditionalFormattingAt(i);
            //optimization, as this may be expensive for lots of ranges
            final CellRangeAddress[] regions = f.getFormattingRanges();
            for (int r = 0; r < f.getNumberOfRules(); r++) {
                ConditionalFormattingRule rule = f.getRule(r);
                rules.add(new EvaluationConditionalFormatRule(workbookEvaluator, sheet, f, i, rule, r, regions));
            }
        }
        // need them in formatting and priority order so logic works right
        Collections.sort(rules);
    }
    return Collections.unmodifiableList(rules);
}
Also used : SheetConditionalFormatting(org.apache.poi.ss.usermodel.SheetConditionalFormatting) ConditionalFormatting(org.apache.poi.ss.usermodel.ConditionalFormatting) SheetConditionalFormatting(org.apache.poi.ss.usermodel.SheetConditionalFormatting) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) ConditionalFormattingRule(org.apache.poi.ss.usermodel.ConditionalFormattingRule)

Example 13 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class XSSFCell method convertSharedFormula.

/**
     * Creates a non shared formula from the shared formula counterpart
     *
     * @param si Shared Group Index
     * @return non shared formula created for the given shared formula and this cell
     */
private String convertSharedFormula(int si, XSSFEvaluationWorkbook fpb) {
    XSSFSheet sheet = getSheet();
    CTCellFormula f = sheet.getSharedFormula(si);
    if (f == null) {
        throw new IllegalStateException("Master cell of a shared formula with sid=" + si + " was not found");
    }
    String sharedFormula = f.getStringValue();
    //Range of cells which the shared formula applies to
    String sharedFormulaRange = f.getRef();
    CellRangeAddress ref = CellRangeAddress.valueOf(sharedFormulaRange);
    int sheetIndex = sheet.getWorkbook().getSheetIndex(sheet);
    SharedFormula sf = new SharedFormula(SpreadsheetVersion.EXCEL2007);
    Ptg[] ptgs = FormulaParser.parse(sharedFormula, fpb, FormulaType.CELL, sheetIndex, getRowIndex());
    Ptg[] fmla = sf.convertSharedFormulas(ptgs, getRowIndex() - ref.getFirstRow(), getColumnIndex() - ref.getFirstColumn());
    return FormulaRenderer.toFormulaString(fpb, fmla);
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) RichTextString(org.apache.poi.ss.usermodel.RichTextString) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) CTCellFormula(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula) SharedFormula(org.apache.poi.ss.formula.SharedFormula)

Example 14 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class XSSFCell method notifyArrayFormulaChanging.

/**
     * The purpose of this method is to validate the cell state prior to modification
     *
     * @see #notifyArrayFormulaChanging()
     */
void notifyArrayFormulaChanging(String msg) {
    if (isPartOfArrayFormulaGroup()) {
        CellRangeAddress cra = getArrayFormulaRange();
        if (cra.getNumberOfCells() > 1) {
            throw new IllegalStateException(msg);
        }
        //un-register the single-cell array formula from the parent XSSFSheet
        getRow().getSheet().removeArrayFormula(this);
    }
}
Also used : CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 15 with CellRangeAddress

use of org.apache.poi.ss.util.CellRangeAddress in project poi by apache.

the class XSSFConditionalFormatting method setFormattingRanges.

@Override
public void setFormattingRanges(CellRangeAddress[] ranges) {
    if (ranges == null) {
        throw new IllegalArgumentException("cellRanges must not be null");
    }
    final StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (CellRangeAddress range : ranges) {
        if (!first) {
            sb.append(" ");
        } else {
            first = false;
        }
        sb.append(range.formatAsString());
    }
    _cf.setSqref(Collections.singletonList(sb.toString()));
}
Also used : CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Aggregations

CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)194 Test (org.junit.Test)73 Row (org.apache.poi.ss.usermodel.Row)33 Cell (org.apache.poi.ss.usermodel.Cell)31 Sheet (org.apache.poi.ss.usermodel.Sheet)22 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)21 ArrayList (java.util.ArrayList)19 Workbook (org.apache.poi.ss.usermodel.Workbook)18 HSSFConditionalFormattingRule (org.apache.poi.hssf.usermodel.HSSFConditionalFormattingRule)16 ConditionalFormattingRule (org.apache.poi.ss.usermodel.ConditionalFormattingRule)16 FileOutputStream (java.io.FileOutputStream)15 SheetConditionalFormatting (org.apache.poi.ss.usermodel.SheetConditionalFormatting)15 XSSFCellStyle (org.apache.poi.xssf.usermodel.XSSFCellStyle)14 XSSFFont (org.apache.poi.xssf.usermodel.XSSFFont)14 XSSFColor (org.apache.poi.xssf.usermodel.XSSFColor)13 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)13 HSSFConditionalFormatting (org.apache.poi.hssf.usermodel.HSSFConditionalFormatting)12 CellReference (org.apache.poi.ss.util.CellReference)12 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)8 Ptg (org.apache.poi.ss.formula.ptg.Ptg)8