Search in sources :

Example 1 with Table

use of org.apache.poi.ss.usermodel.Table in project poi by apache.

the class TestTableStyles method testCustomStyle.

@Test
public void testCustomStyle() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("tableStyle.xlsx");
    Table table = wb.getTable("Table1");
    assertNotNull("missing table", table);
    TableStyleInfo style = table.getStyle();
    assertNotNull("Missing table style info", style);
    assertNotNull("Missing table style", style.getStyle());
    assertEquals("Wrong name", "TestTableStyle", style.getName());
    assertEquals("Wrong name", "TestTableStyle", style.getStyle().getName());
    DifferentialStyleProvider firstColumn = style.getStyle().getStyle(TableStyleType.firstColumn);
    assertNotNull("no first column style", firstColumn);
    FontFormatting font = firstColumn.getFontFormatting();
    assertNotNull("no first col font", font);
    assertTrue("wrong first col bold", font.isBold());
    wb.close();
}
Also used : Table(org.apache.poi.ss.usermodel.Table) DifferentialStyleProvider(org.apache.poi.ss.usermodel.DifferentialStyleProvider) TableStyleInfo(org.apache.poi.ss.usermodel.TableStyleInfo) FontFormatting(org.apache.poi.ss.usermodel.FontFormatting) Test(org.junit.Test)

Example 2 with Table

use of org.apache.poi.ss.usermodel.Table in project poi by apache.

the class FormulaParser method parseStructuredReference.

/**
     * Parses a structured reference, returns it as area reference.
     * Examples:
     * <pre>
     * Table1[col]
     * Table1[[#Totals],[col]]
     * Table1[#Totals]
     * Table1[#All]
     * Table1[#Data]
     * Table1[#Headers]
     * Table1[#Totals]
     * Table1[#This Row]
     * Table1[[#All],[col]]
     * Table1[[#Headers],[col]]
     * Table1[[#Totals],[col]]
     * Table1[[#All],[col1]:[col2]]
     * Table1[[#Data],[col1]:[col2]]
     * Table1[[#Headers],[col1]:[col2]]
     * Table1[[#Totals],[col1]:[col2]]
     * Table1[[#Headers],[#Data],[col2]]
     * Table1[[#This Row], [col1]]
     * Table1[ [col1]:[col2] ]
     * </pre>
     * @param tableName
     * @return Area Reference for the given table
     */
private ParseNode parseStructuredReference(String tableName) {
    if (!(_ssVersion.equals(SpreadsheetVersion.EXCEL2007))) {
        throw new FormulaParseException("Structured references work only on XSSF (Excel 2007+)!");
    }
    Table tbl = _book.getTable(tableName);
    if (tbl == null) {
        throw new FormulaParseException("Illegal table name: '" + tableName + "'");
    }
    String sheetName = tbl.getSheetName();
    int startCol = tbl.getStartColIndex();
    int endCol = tbl.getEndColIndex();
    int startRow = tbl.getStartRowIndex();
    int endRow = tbl.getEndRowIndex();
    // Do NOT return before done reading all the structured reference tokens from the input stream.
    // Throwing exceptions is okay.
    int savePtr0 = _pointer;
    GetChar();
    boolean isTotalsSpec = false;
    boolean isThisRowSpec = false;
    boolean isDataSpec = false;
    boolean isHeadersSpec = false;
    boolean isAllSpec = false;
    // The number of special quantifiers
    int nSpecQuantifiers = 0;
    while (true) {
        int savePtr1 = _pointer;
        String specName = parseAsSpecialQuantifier();
        if (specName == null) {
            resetPointer(savePtr1);
            break;
        }
        if (specName.equals(specAll)) {
            isAllSpec = true;
        } else if (specName.equals(specData)) {
            isDataSpec = true;
        } else if (specName.equals(specHeaders)) {
            isHeadersSpec = true;
        } else if (specName.equals(specThisRow)) {
            isThisRowSpec = true;
        } else if (specName.equals(specTotals)) {
            isTotalsSpec = true;
        } else {
            throw new FormulaParseException("Unknown special quantifier " + specName);
        }
        nSpecQuantifiers++;
        if (look == ',') {
            GetChar();
        } else {
            break;
        }
    }
    boolean isThisRow = false;
    SkipWhite();
    if (look == '@') {
        isThisRow = true;
        GetChar();
    }
    // parse column quantifier
    String startColumnName;
    String endColumnName = null;
    int nColQuantifiers = 0;
    int savePtr1 = _pointer;
    startColumnName = parseAsColumnQuantifier();
    if (startColumnName == null) {
        resetPointer(savePtr1);
    } else {
        nColQuantifiers++;
        if (look == ',') {
            throw new FormulaParseException("The formula " + _formulaString + "is illegal: you should not use ',' with column quantifiers");
        } else if (look == ':') {
            GetChar();
            endColumnName = parseAsColumnQuantifier();
            nColQuantifiers++;
            if (endColumnName == null) {
                throw new FormulaParseException("The formula " + _formulaString + "is illegal: the string after ':' must be column quantifier");
            }
        }
    }
    if (nColQuantifiers == 0 && nSpecQuantifiers == 0) {
        resetPointer(savePtr0);
        savePtr0 = _pointer;
        startColumnName = parseAsColumnQuantifier();
        if (startColumnName != null) {
            nColQuantifiers++;
        } else {
            resetPointer(savePtr0);
            String name = parseAsSpecialQuantifier();
            if (name != null) {
                if (name.equals(specAll)) {
                    isAllSpec = true;
                } else if (name.equals(specData)) {
                    isDataSpec = true;
                } else if (name.equals(specHeaders)) {
                    isHeadersSpec = true;
                } else if (name.equals(specThisRow)) {
                    isThisRowSpec = true;
                } else if (name.equals(specTotals)) {
                    isTotalsSpec = true;
                } else {
                    throw new FormulaParseException("Unknown special quantifier " + name);
                }
                nSpecQuantifiers++;
            } else {
                throw new FormulaParseException("The formula " + _formulaString + " is illegal");
            }
        }
    } else {
        Match(']');
    }
    if (isTotalsSpec && !tbl.isHasTotalsRow()) {
        return new ParseNode(ErrPtg.REF_INVALID);
    }
    if ((isThisRow || isThisRowSpec) && (_rowIndex < startRow || endRow < _rowIndex)) {
        // structured reference is trying to reference a row above or below the table with [#This Row] or [@]
        if (_rowIndex >= 0) {
            return new ParseNode(ErrPtg.VALUE_INVALID);
        } else {
            throw new FormulaParseException("Formula contained [#This Row] or [@] structured reference but this row < 0. " + "Row index must be specified for row-referencing structured references.");
        }
    }
    int actualStartRow = startRow;
    int actualEndRow = endRow;
    int actualStartCol = startCol;
    int actualEndCol = endCol;
    if (nSpecQuantifiers > 0) {
        //Selecting rows
        if (nSpecQuantifiers == 1 && isAllSpec) {
        //do nothing
        } else if (isDataSpec && isHeadersSpec) {
            if (tbl.isHasTotalsRow()) {
                actualEndRow = endRow - 1;
            }
        } else if (isDataSpec && isTotalsSpec) {
            actualStartRow = startRow + 1;
        } else if (nSpecQuantifiers == 1 && isDataSpec) {
            actualStartRow = startRow + 1;
            if (tbl.isHasTotalsRow()) {
                actualEndRow = endRow - 1;
            }
        } else if (nSpecQuantifiers == 1 && isHeadersSpec) {
            actualEndRow = actualStartRow;
        } else if (nSpecQuantifiers == 1 && isTotalsSpec) {
            actualStartRow = actualEndRow;
        } else if ((nSpecQuantifiers == 1 && isThisRowSpec) || isThisRow) {
            //The rowNum is 0 based
            actualStartRow = _rowIndex;
            actualEndRow = _rowIndex;
        } else {
            throw new FormulaParseException("The formula " + _formulaString + " is illegal");
        }
    } else {
        if (isThisRow) {
            // there is a @
            //The rowNum is 0 based
            actualStartRow = _rowIndex;
            actualEndRow = _rowIndex;
        } else {
            // Really no special quantifiers
            actualStartRow++;
            if (tbl.isHasTotalsRow())
                actualEndRow--;
        }
    }
    if (nColQuantifiers == 2) {
        if (startColumnName == null || endColumnName == null) {
            throw new IllegalStateException("Fatal error");
        }
        int startIdx = tbl.findColumnIndex(startColumnName);
        int endIdx = tbl.findColumnIndex(endColumnName);
        if (startIdx == -1 || endIdx == -1) {
            throw new FormulaParseException("One of the columns " + startColumnName + ", " + endColumnName + " doesn't exist in table " + tbl.getName());
        }
        actualStartCol = startCol + startIdx;
        actualEndCol = startCol + endIdx;
    } else if (nColQuantifiers == 1 && !isThisRow) {
        if (startColumnName == null) {
            throw new IllegalStateException("Fatal error");
        }
        int idx = tbl.findColumnIndex(startColumnName);
        if (idx == -1) {
            throw new FormulaParseException("The column " + startColumnName + " doesn't exist in table " + tbl.getName());
        }
        actualStartCol = startCol + idx;
        actualEndCol = actualStartCol;
    }
    CellReference topLeft = new CellReference(actualStartRow, actualStartCol);
    CellReference bottomRight = new CellReference(actualEndRow, actualEndCol);
    SheetIdentifier sheetIden = new SheetIdentifier(null, new NameIdentifier(sheetName, true));
    Ptg ptg = _book.get3DReferencePtg(new AreaReference(topLeft, bottomRight), sheetIden);
    return new ParseNode(ptg);
}
Also used : NumberPtg(org.apache.poi.ss.formula.ptg.NumberPtg) ArrayPtg(org.apache.poi.ss.formula.ptg.ArrayPtg) AttrPtg(org.apache.poi.ss.formula.ptg.AttrPtg) PercentPtg(org.apache.poi.ss.formula.ptg.PercentPtg) RangePtg(org.apache.poi.ss.formula.ptg.RangePtg) AddPtg(org.apache.poi.ss.formula.ptg.AddPtg) EqualPtg(org.apache.poi.ss.formula.ptg.EqualPtg) UnaryMinusPtg(org.apache.poi.ss.formula.ptg.UnaryMinusPtg) NameXPtg(org.apache.poi.ss.formula.ptg.NameXPtg) RefPtg(org.apache.poi.ss.formula.ptg.RefPtg) DividePtg(org.apache.poi.ss.formula.ptg.DividePtg) GreaterThanPtg(org.apache.poi.ss.formula.ptg.GreaterThanPtg) MultiplyPtg(org.apache.poi.ss.formula.ptg.MultiplyPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) ErrPtg(org.apache.poi.ss.formula.ptg.ErrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) NamePtg(org.apache.poi.ss.formula.ptg.NamePtg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) NotEqualPtg(org.apache.poi.ss.formula.ptg.NotEqualPtg) ValueOperatorPtg(org.apache.poi.ss.formula.ptg.ValueOperatorPtg) ConcatPtg(org.apache.poi.ss.formula.ptg.ConcatPtg) UnaryPlusPtg(org.apache.poi.ss.formula.ptg.UnaryPlusPtg) GreaterEqualPtg(org.apache.poi.ss.formula.ptg.GreaterEqualPtg) LessThanPtg(org.apache.poi.ss.formula.ptg.LessThanPtg) BoolPtg(org.apache.poi.ss.formula.ptg.BoolPtg) IntersectionPtg(org.apache.poi.ss.formula.ptg.IntersectionPtg) AbstractFunctionPtg(org.apache.poi.ss.formula.ptg.AbstractFunctionPtg) IntPtg(org.apache.poi.ss.formula.ptg.IntPtg) LessEqualPtg(org.apache.poi.ss.formula.ptg.LessEqualPtg) UnionPtg(org.apache.poi.ss.formula.ptg.UnionPtg) FuncVarPtg(org.apache.poi.ss.formula.ptg.FuncVarPtg) SubtractPtg(org.apache.poi.ss.formula.ptg.SubtractPtg) FuncPtg(org.apache.poi.ss.formula.ptg.FuncPtg) OperandPtg(org.apache.poi.ss.formula.ptg.OperandPtg) MissingArgPtg(org.apache.poi.ss.formula.ptg.MissingArgPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) OperationPtg(org.apache.poi.ss.formula.ptg.OperationPtg) PowerPtg(org.apache.poi.ss.formula.ptg.PowerPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) AreaReference(org.apache.poi.ss.util.AreaReference) Table(org.apache.poi.ss.usermodel.Table) CellReference(org.apache.poi.ss.util.CellReference)

Aggregations

Table (org.apache.poi.ss.usermodel.Table)2 AbstractFunctionPtg (org.apache.poi.ss.formula.ptg.AbstractFunctionPtg)1 AddPtg (org.apache.poi.ss.formula.ptg.AddPtg)1 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)1 ArrayPtg (org.apache.poi.ss.formula.ptg.ArrayPtg)1 AttrPtg (org.apache.poi.ss.formula.ptg.AttrPtg)1 BoolPtg (org.apache.poi.ss.formula.ptg.BoolPtg)1 ConcatPtg (org.apache.poi.ss.formula.ptg.ConcatPtg)1 DividePtg (org.apache.poi.ss.formula.ptg.DividePtg)1 EqualPtg (org.apache.poi.ss.formula.ptg.EqualPtg)1 ErrPtg (org.apache.poi.ss.formula.ptg.ErrPtg)1 FuncPtg (org.apache.poi.ss.formula.ptg.FuncPtg)1 FuncVarPtg (org.apache.poi.ss.formula.ptg.FuncVarPtg)1 GreaterEqualPtg (org.apache.poi.ss.formula.ptg.GreaterEqualPtg)1 GreaterThanPtg (org.apache.poi.ss.formula.ptg.GreaterThanPtg)1 IntPtg (org.apache.poi.ss.formula.ptg.IntPtg)1 IntersectionPtg (org.apache.poi.ss.formula.ptg.IntersectionPtg)1 LessEqualPtg (org.apache.poi.ss.formula.ptg.LessEqualPtg)1 LessThanPtg (org.apache.poi.ss.formula.ptg.LessThanPtg)1 MemAreaPtg (org.apache.poi.ss.formula.ptg.MemAreaPtg)1