Search in sources :

Example 1 with Name

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

the class FormulaParser method addName.

/**
     * Adds a name (named range or user defined function) to underlying workbook's names table
     * @param functionName
     */
private void addName(String functionName) {
    final Name name = _book.createName();
    name.setFunction(true);
    name.setNameName(functionName);
    name.setSheetIndex(_sheetIndex);
}
Also used : Name(org.apache.poi.ss.usermodel.Name)

Example 2 with Name

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

the class LinkedDropDownLists method buildDataSheet.

/**
     * Called to populate the named areas/regions. The contents of the cells on
     * row one will be used to populate the first drop down list. The contents of
     * the cells on rows two, three and four will be used to populate the second
     * drop down list, just which row will be determined by the choice the user
     * makes in the first drop down list.
     * 
     * In all cases, the approach is to create a row, create and populate cells
     * with data and then specify a name that identifies those cells. With the
     * exception of the first range, the names that are chosen for each range
     * of cells are quite important. In short, each of the options the user 
     * could select in the first drop down list is used as the name for another
     * range of cells. Thus, in this example, the user can select either 
     * 'Animal', 'Vegetable' or 'Mineral' in the first drop down and so the
     * sheet contains ranges named 'ANIMAL', 'VEGETABLE' and 'MINERAL'.
     * 
     * @param dataSheet An instance of a class that implements the Sheet Sheet
     *        interface (HSSFSheet or XSSFSheet).
     */
private static final void buildDataSheet(Sheet dataSheet) {
    Row row = null;
    Cell cell = null;
    Name name = null;
    // The first row will hold the data for the first validation.
    row = dataSheet.createRow(10);
    cell = row.createCell(0);
    cell.setCellValue("Animal");
    cell = row.createCell(1);
    cell.setCellValue("Vegetable");
    cell = row.createCell(2);
    cell.setCellValue("Mineral");
    name = dataSheet.getWorkbook().createName();
    name.setRefersToFormula("$A$11:$C$11");
    name.setNameName("CHOICES");
    // The next three rows will hold the data that will be used to
    // populate the second, or linked, drop down list.
    row = dataSheet.createRow(11);
    cell = row.createCell(0);
    cell.setCellValue("Lion");
    cell = row.createCell(1);
    cell.setCellValue("Tiger");
    cell = row.createCell(2);
    cell.setCellValue("Leopard");
    cell = row.createCell(3);
    cell.setCellValue("Elephant");
    cell = row.createCell(4);
    cell.setCellValue("Eagle");
    cell = row.createCell(5);
    cell.setCellValue("Horse");
    cell = row.createCell(6);
    cell.setCellValue("Zebra");
    name = dataSheet.getWorkbook().createName();
    name.setRefersToFormula("$A$12:$G$12");
    name.setNameName("ANIMAL");
    row = dataSheet.createRow(12);
    cell = row.createCell(0);
    cell.setCellValue("Cabbage");
    cell = row.createCell(1);
    cell.setCellValue("Cauliflower");
    cell = row.createCell(2);
    cell.setCellValue("Potato");
    cell = row.createCell(3);
    cell.setCellValue("Onion");
    cell = row.createCell(4);
    cell.setCellValue("Beetroot");
    cell = row.createCell(5);
    cell.setCellValue("Asparagus");
    cell = row.createCell(6);
    cell.setCellValue("Spinach");
    cell = row.createCell(7);
    cell.setCellValue("Chard");
    name = dataSheet.getWorkbook().createName();
    name.setRefersToFormula("$A$13:$H$13");
    name.setNameName("VEGETABLE");
    row = dataSheet.createRow(13);
    cell = row.createCell(0);
    cell.setCellValue("Bauxite");
    cell = row.createCell(1);
    cell.setCellValue("Quartz");
    cell = row.createCell(2);
    cell.setCellValue("Feldspar");
    cell = row.createCell(3);
    cell.setCellValue("Shist");
    cell = row.createCell(4);
    cell.setCellValue("Shale");
    cell = row.createCell(5);
    cell.setCellValue("Mica");
    name = dataSheet.getWorkbook().createName();
    name.setRefersToFormula("$A$14:$F$14");
    name.setNameName("MINERAL");
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell) Name(org.apache.poi.ss.usermodel.Name)

Example 3 with Name

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

the class XSSFPivotCacheDefinition method getPivotArea.

/**
     * Find the 2D base data area for the pivot table, either from its direct reference or named table/range.
     * @return AreaReference representing the current area defined by the pivot table
     * @throws IllegalArgumentException if the ref attribute is not contiguous or the name attribute is not found.
     */
@Beta
public AreaReference getPivotArea(Workbook wb) throws IllegalArgumentException {
    final CTWorksheetSource wsSource = ctPivotCacheDefinition.getCacheSource().getWorksheetSource();
    final String ref = wsSource.getRef();
    final String name = wsSource.getName();
    if (ref == null && name == null) {
        throw new IllegalArgumentException("Pivot cache must reference an area, named range, or table.");
    }
    // this is the XML format, so tell the reference that.
    if (ref != null) {
        return new AreaReference(ref, SpreadsheetVersion.EXCEL2007);
    }
    assert (name != null);
    // named range or table?
    final Name range = wb.getName(name);
    if (range != null) {
        return new AreaReference(range.getRefersToFormula(), SpreadsheetVersion.EXCEL2007);
    }
    // not a named range, check for a table.
    // do this second, as tables are sheet-specific, but named ranges are not, and may not have a sheet name given.
    final XSSFSheet sheet = (XSSFSheet) wb.getSheet(wsSource.getSheet());
    for (XSSFTable table : sheet.getTables()) {
        // TODO: case-sensitive?
        if (name.equals(table.getName())) {
            return new AreaReference(table.getStartCellReference(), table.getEndCellReference());
        }
    }
    throw new IllegalArgumentException("Name '" + name + "' was not found.");
}
Also used : AreaReference(org.apache.poi.ss.util.AreaReference) CTWorksheetSource(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheetSource) Name(org.apache.poi.ss.usermodel.Name) QName(javax.xml.namespace.QName) Beta(org.apache.poi.util.Beta)

Example 4 with Name

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

the class TestFormulaParser method testZeroRowRefs.

/**
     * Zero is not a valid row number so cell references like 'A0' are not valid.
     * Actually, they should be treated like defined names.
     * <br/>
     * In addition, leading zeros (on the row component) should be removed from cell
     * references during parsing.
     */
@Test
public void testZeroRowRefs() throws IOException {
    // bad because zero is not a valid row number
    String badCellRef = "B0";
    // this should get parsed as "B1"
    String leadingZeroCellRef = "B000001";
    HSSFWorkbook wb = new HSSFWorkbook();
    try {
        HSSFFormulaParser.parse(badCellRef, wb);
        fail("Identified bug 47312b - Shouldn't be able to parse cell ref '" + badCellRef + "'.");
    } catch (FormulaParseException e) {
        // expected during successful test
        confirmParseException(e, "Specified named range '" + badCellRef + "' does not exist in the current workbook.");
    }
    Ptg[] ptgs;
    try {
        ptgs = HSSFFormulaParser.parse(leadingZeroCellRef, wb);
        assertEquals("B1", ((RefPtg) ptgs[0]).toFormulaString());
    } catch (FormulaParseException e) {
        confirmParseException(e, "Specified named range '" + leadingZeroCellRef + "' does not exist in the current workbook.");
        // close but no cigar
        fail("Identified bug 47312c - '" + leadingZeroCellRef + "' should parse as 'B1'.");
    }
    // create a defined name called 'B0' and try again
    Name n = wb.createName();
    n.setNameName("B0");
    n.setRefersToFormula("1+1");
    ptgs = HSSFFormulaParser.parse("B0", wb);
    confirmTokenClasses(ptgs, NamePtg.class);
    wb.close();
}
Also used : FormulaParseException(org.apache.poi.ss.formula.FormulaParseException) 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) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) StringPtg(org.apache.poi.ss.formula.ptg.StringPtg) ErrPtg(org.apache.poi.ss.formula.ptg.ErrPtg) Ptg(org.apache.poi.ss.formula.ptg.Ptg) Area3DPtg(org.apache.poi.ss.formula.ptg.Area3DPtg) NamePtg(org.apache.poi.ss.formula.ptg.NamePtg) MemAreaPtg(org.apache.poi.ss.formula.ptg.MemAreaPtg) ConcatPtg(org.apache.poi.ss.formula.ptg.ConcatPtg) UnaryPlusPtg(org.apache.poi.ss.formula.ptg.UnaryPlusPtg) 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) 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) MissingArgPtg(org.apache.poi.ss.formula.ptg.MissingArgPtg) MemFuncPtg(org.apache.poi.ss.formula.ptg.MemFuncPtg) PowerPtg(org.apache.poi.ss.formula.ptg.PowerPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) ParenthesisPtg(org.apache.poi.ss.formula.ptg.ParenthesisPtg) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Name(org.apache.poi.ss.usermodel.Name) TestHSSFName(org.apache.poi.hssf.usermodel.TestHSSFName) HSSFName(org.apache.poi.hssf.usermodel.HSSFName) Test(org.junit.Test)

Example 5 with Name

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

the class TestFormulas method testFormulasWithUnderscore.

/**
     * Verify that FormulaParser handles defined names beginning with underscores,
     * see Bug #49640
     */
@Test
public void testFormulasWithUnderscore() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    try {
        Name nm1 = wb.createName();
        nm1.setNameName("_score1");
        nm1.setRefersToFormula("A1");
        Name nm2 = wb.createName();
        nm2.setNameName("_score2");
        nm2.setRefersToFormula("A2");
        Sheet sheet = wb.createSheet();
        Cell cell = sheet.createRow(0).createCell(2);
        cell.setCellFormula("_score1*SUM(_score1+_score2)");
        assertEquals("_score1*SUM(_score1+_score2)", cell.getCellFormula());
    } finally {
        wb.close();
    }
}
Also used : Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Name(org.apache.poi.ss.usermodel.Name) Test(org.junit.Test)

Aggregations

Name (org.apache.poi.ss.usermodel.Name)16 Test (org.junit.Test)11 Sheet (org.apache.poi.ss.usermodel.Sheet)5 Workbook (org.apache.poi.ss.usermodel.Workbook)5 Cell (org.apache.poi.ss.usermodel.Cell)4 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)3 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)3 InternalSheet (org.apache.poi.hssf.model.InternalSheet)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 AreaPtg (org.apache.poi.ss.formula.ptg.AreaPtg)2 Ptg (org.apache.poi.ss.formula.ptg.Ptg)2 Row (org.apache.poi.ss.usermodel.Row)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 UnicodeString (org.apache.poi.hssf.record.common.UnicodeString)1 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)1 HSSFName (org.apache.poi.hssf.usermodel.HSSFName)1 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)1