Search in sources :

Example 6 with XSSFEvaluationWorkbook

use of org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook in project poi by apache.

the class XSSFRowShifter method updateConditionalFormatting.

public void updateConditionalFormatting(FormulaShifter shifter) {
    XSSFSheet xsheet = (XSSFSheet) sheet;
    XSSFWorkbook wb = xsheet.getWorkbook();
    int sheetIndex = wb.getSheetIndex(sheet);
    //don't care, structured references not allowed in conditional formatting
    final int rowIndex = -1;
    XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
    CTWorksheet ctWorksheet = xsheet.getCTWorksheet();
    CTConditionalFormatting[] conditionalFormattingArray = ctWorksheet.getConditionalFormattingArray();
    // iterate backwards due to possible calls to ctWorksheet.removeConditionalFormatting(j)
    for (int j = conditionalFormattingArray.length - 1; j >= 0; j--) {
        CTConditionalFormatting cf = conditionalFormattingArray[j];
        ArrayList<CellRangeAddress> cellRanges = new ArrayList<CellRangeAddress>();
        for (Object stRef : cf.getSqref()) {
            String[] regions = stRef.toString().split(" ");
            for (String region : regions) {
                cellRanges.add(CellRangeAddress.valueOf(region));
            }
        }
        boolean changed = false;
        List<CellRangeAddress> temp = new ArrayList<CellRangeAddress>();
        for (CellRangeAddress craOld : cellRanges) {
            CellRangeAddress craNew = shiftRange(shifter, craOld, sheetIndex);
            if (craNew == null) {
                changed = true;
                continue;
            }
            temp.add(craNew);
            if (craNew != craOld) {
                changed = true;
            }
        }
        if (changed) {
            int nRanges = temp.size();
            if (nRanges == 0) {
                ctWorksheet.removeConditionalFormatting(j);
                continue;
            }
            List<String> refs = new ArrayList<String>();
            for (CellRangeAddress a : temp) refs.add(a.formatAsString());
            cf.setSqref(refs);
        }
        for (CTCfRule cfRule : cf.getCfRuleArray()) {
            String[] formulaArray = cfRule.getFormulaArray();
            for (int i = 0; i < formulaArray.length; i++) {
                String formula = formulaArray[i];
                Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex, rowIndex);
                if (shifter.adjustFormula(ptgs, sheetIndex)) {
                    String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
                    cfRule.setFormulaArray(i, shiftedFmla);
                }
            }
        }
    }
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) CTCfRule(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCfRule) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) ArrayList(java.util.ArrayList) CTConditionalFormatting(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTConditionalFormatting) XSSFEvaluationWorkbook(org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress)

Example 7 with XSSFEvaluationWorkbook

use of org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook in project poi by apache.

the class TestFormulaParser method testParseExternalReferencesWithUnquotedSheetName.

// trivial case for bug 60219: FormulaParser can't parse external references when sheet name is quoted
@Test
public void testParseExternalReferencesWithUnquotedSheetName() throws Exception {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFEvaluationWorkbook fpwb = XSSFEvaluationWorkbook.create(wb);
    Ptg[] ptgs = FormulaParser.parse("[1]Sheet1!A1", fpwb, FormulaType.CELL, -1);
    // org.apache.poi.ss.formula.ptg.Ref3DPxg [ [workbook=1] sheet=Sheet 1 ! A1]
    assertEquals("Ptgs length", 1, ptgs.length);
    assertTrue("Ptg class", ptgs[0] instanceof Ref3DPxg);
    Ref3DPxg pxg = (Ref3DPxg) ptgs[0];
    assertEquals("External workbook number", 1, pxg.getExternalWorkbookNumber());
    assertEquals("Sheet name", "Sheet1", pxg.getSheetName());
    assertEquals("Row", 0, pxg.getRow());
    assertEquals("Column", 0, pxg.getColumn());
    wb.close();
}
Also used : XSSFEvaluationWorkbook(org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook) Ptg(org.apache.poi.ss.formula.ptg.Ptg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Ref3DPxg(org.apache.poi.ss.formula.ptg.Ref3DPxg) Test(org.junit.Test)

Aggregations

XSSFEvaluationWorkbook (org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook)7 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)7 Ptg (org.apache.poi.ss.formula.ptg.Ptg)6 Test (org.junit.Test)4 AbstractFunctionPtg (org.apache.poi.ss.formula.ptg.AbstractFunctionPtg)3 AreaErrPtg (org.apache.poi.ss.formula.ptg.AreaErrPtg)3 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)3 StringPtg (org.apache.poi.ss.formula.ptg.StringPtg)3 Ref3DPxg (org.apache.poi.ss.formula.ptg.Ref3DPxg)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)2 ArrayList (java.util.ArrayList)1 FormulaParseException (org.apache.poi.ss.formula.FormulaParseException)1 NameXPxg (org.apache.poi.ss.formula.ptg.NameXPxg)1 Name (org.apache.poi.ss.usermodel.Name)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1 CTCfRule (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCfRule)1 CTConditionalFormatting (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTConditionalFormatting)1 CTWorksheet (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet)1