Search in sources :

Example 1 with InternalWorkbook

use of org.apache.poi.hssf.model.InternalWorkbook in project poi by apache.

the class HSSFWorkbook method getForceFormulaRecalculation.

/**
     * Whether Excel will be asked to recalculate all formulas when the  workbook is opened.
     *
     * @since 3.8
     */
@Override
public boolean getForceFormulaRecalculation() {
    InternalWorkbook iwb = getWorkbook();
    RecalcIdRecord recalc = (RecalcIdRecord) iwb.findFirstRecordBySid(RecalcIdRecord.sid);
    return recalc != null && recalc.getEngineId() != 0;
}
Also used : RecalcIdRecord(org.apache.poi.hssf.record.RecalcIdRecord) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook)

Example 2 with InternalWorkbook

use of org.apache.poi.hssf.model.InternalWorkbook in project poi by apache.

the class HSSFPicture method getImageDimension.

/**
     * Return the dimension of the embedded image in pixel
     *
     * @return image dimension in pixels
     */
@Override
public Dimension getImageDimension() {
    InternalWorkbook iwb = getPatriarch().getSheet().getWorkbook().getWorkbook();
    EscherBSERecord bse = iwb.getBSERecord(getPictureIndex());
    byte[] data = bse.getBlipRecord().getPicturedata();
    int type = bse.getBlipTypeWin32();
    return ImageUtils.getImageDimension(new ByteArrayInputStream(data), type);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord)

Example 3 with InternalWorkbook

use of org.apache.poi.hssf.model.InternalWorkbook in project poi by apache.

the class TestBugs method bug30978.

/**
     * Test that we can delete sheets without
     *  breaking the build in named ranges
     *  used for printing stuff.
     */
@Test
public void bug30978() throws Exception {
    HSSFWorkbook wb1 = openSample("30978-alt.xls");
    assertEquals(1, wb1.getNumberOfNames());
    assertEquals(3, wb1.getNumberOfSheets());
    // Check all names fit within range, and use
    //  DeletedArea3DPtg
    InternalWorkbook w = wb1.getWorkbook();
    assertNames(wb1, w);
    // Delete the 2nd sheet
    wb1.removeSheetAt(1);
    // Re-check
    assertEquals(1, wb1.getNumberOfNames());
    assertEquals(2, wb1.getNumberOfSheets());
    assertNames(wb1, w);
    // Save and re-load
    HSSFWorkbook wb2 = writeOutAndReadBack(wb1);
    wb1.close();
    w = wb2.getWorkbook();
    assertEquals(1, wb2.getNumberOfNames());
    assertEquals(2, wb2.getNumberOfSheets());
    assertNames(wb2, w);
    wb2.close();
}
Also used : InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Test(org.junit.Test)

Example 4 with InternalWorkbook

use of org.apache.poi.hssf.model.InternalWorkbook in project poi by apache.

the class HSSFName method setNameName.

/**
     * Sets the name of the named range
     *
     * <p>The following is a list of syntax rules that you need to be aware of when you create and edit names.</p>
     * <ul>
     *   <li><strong>Valid characters</strong>
     *   The first character of a name must be a letter, an underscore character (_), or a backslash (\).
     *   Remaining characters in the name can be letters, numbers, periods, and underscore characters.
     *   </li>
     *   <li><strong>Cell references disallowed</strong>
     *   Names cannot be the same as a cell reference, such as Z$100 or R1C1.</li>
     *   <li><strong>Spaces are not valid</strong>
     *   Spaces are not allowed as part of a name. Use the underscore character (_) and period (.) as word separators, such as, Sales_Tax or First.Quarter.
     *   </li>
     *   <li><strong>Name length</strong>
     *    A name can contain up to 255 characters.
     *   </li>
     *   <li><strong>Case sensitivity</strong>
     *   Names can contain uppercase and lowercase letters.
     *   </li>
     * </ul>
     *
     * <p>
     * A name must always be unique within its scope. POI prevents you from defining a name that is not unique
     * within its scope. However you can use the same name in different scopes. Example:
     * <pre><blockquote>
     * //by default names are workbook-global
     * HSSFName name;
     * name = workbook.createName();
     * name.setNameName("sales_08");
     *
     * name = workbook.createName();
     * name.setNameName("sales_08"); //will throw an exception: "The workbook already contains this name (case-insensitive)"
     *
     * //create sheet-level name
     * name = workbook.createName();
     * name.setSheetIndex(0); //the scope of the name is the first sheet
     * name.setNameName("sales_08");  //ok
     *
     * name = workbook.createName();
     * name.setSheetIndex(0);
     * name.setNameName("sales_08");  //will throw an exception: "The sheet already contains this name (case-insensitive)"
     *
     * </blockquote></pre>
    * </p>
     *
     * @param nameName named range name to set
     * @throws IllegalArgumentException if the name is invalid or the name already exists (case-insensitive)
     */
public void setNameName(String nameName) {
    validateName(nameName);
    InternalWorkbook wb = _book.getWorkbook();
    _definedNameRec.setNameText(nameName);
    int sheetNumber = _definedNameRec.getSheetNumber();
    //Check to ensure no other names have the same case-insensitive name
    final int lastNameIndex = wb.getNumNames() - 1;
    for (int i = lastNameIndex; i >= 0; i--) {
        NameRecord rec = wb.getNameRecord(i);
        if (rec != _definedNameRec) {
            if (rec.getNameText().equalsIgnoreCase(nameName) && sheetNumber == rec.getSheetNumber()) {
                String msg = "The " + (sheetNumber == 0 ? "workbook" : "sheet") + " already contains this name: " + nameName;
                _definedNameRec.setNameText(nameName + "(2)");
                throw new IllegalArgumentException(msg);
            }
        }
    }
    // Update our comment, if there is one
    if (_commentRec != null) {
        _commentRec.setNameText(nameName);
        _book.getWorkbook().updateNameCommentRecordCache(_commentRec);
    }
}
Also used : NameRecord(org.apache.poi.hssf.record.NameRecord) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook)

Example 5 with InternalWorkbook

use of org.apache.poi.hssf.model.InternalWorkbook in project poi by apache.

the class TestEventWorkbookBuilder method testFormulas.

public void testFormulas() {
    FormulaRecord[] fRecs = mockListen.getFormulaRecords();
    // Check our formula records
    assertEquals(6, fRecs.length);
    InternalWorkbook stubWB = listener.getStubWorkbook();
    assertNotNull(stubWB);
    HSSFWorkbook stubHSSF = listener.getStubHSSFWorkbook();
    assertNotNull(stubHSSF);
    // Check these stubs have the right stuff on them
    assertEquals("Sheet1", stubWB.getSheetName(0));
    assertEquals("Sheet1", stubHSSF.getSheetName(0));
    assertEquals("S2", stubWB.getSheetName(1));
    assertEquals("S2", stubHSSF.getSheetName(1));
    assertEquals("Sh3", stubWB.getSheetName(2));
    assertEquals("Sh3", stubHSSF.getSheetName(2));
    // Check we can get the formula without breaking
    for (FormulaRecord fRec : fRecs) {
        HSSFFormulaParser.toFormulaString(stubHSSF, fRec.getParsedExpression());
    }
    // Peer into just one formula, and check that
    //  all the ptgs give back the right things
    Ptg[] ptgs = fRecs[0].getParsedExpression();
    assertEquals(1, ptgs.length);
    assertTrue(ptgs[0] instanceof Ref3DPtg);
    Ref3DPtg ptg = (Ref3DPtg) ptgs[0];
    HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(stubHSSF);
    assertEquals("Sheet1!A1", ptg.toFormulaString(book));
    // Now check we get the right formula back for
    //  a few sample ones
    FormulaRecord fr;
    // Sheet 1 A2 is on same sheet
    fr = fRecs[0];
    assertEquals(1, fr.getRow());
    assertEquals(0, fr.getColumn());
    assertEquals("Sheet1!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
    // Sheet 1 A5 is to another sheet
    fr = fRecs[3];
    assertEquals(4, fr.getRow());
    assertEquals(0, fr.getColumn());
    assertEquals("'S2'!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
    // Sheet 1 A7 is to another sheet, range
    fr = fRecs[5];
    assertEquals(6, fr.getRow());
    assertEquals(0, fr.getColumn());
    assertEquals("SUM(Sh3!A1:A4)", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
    // Now, load via Usermodel and re-check
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("3dFormulas.xls");
    assertEquals("Sheet1!A1", wb.getSheetAt(0).getRow(1).getCell(0).getCellFormula());
    assertEquals("SUM(Sh3!A1:A4)", wb.getSheetAt(0).getRow(6).getCell(0).getCellFormula());
}
Also used : Ptg(org.apache.poi.ss.formula.ptg.Ptg) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) Ref3DPtg(org.apache.poi.ss.formula.ptg.Ref3DPtg) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFEvaluationWorkbook(org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook)

Aggregations

InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)18 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)5 Test (org.junit.Test)5 NameRecord (org.apache.poi.hssf.record.NameRecord)4 EscherBSERecord (org.apache.poi.ddf.EscherBSERecord)3 InternalSheet (org.apache.poi.hssf.model.InternalSheet)3 Area3DPtg (org.apache.poi.ss.formula.ptg.Area3DPtg)3 Ptg (org.apache.poi.ss.formula.ptg.Ptg)3 AutoFilterInfoRecord (org.apache.poi.hssf.record.AutoFilterInfoRecord)2 RecalcIdRecord (org.apache.poi.hssf.record.RecalcIdRecord)2 HSSFEvaluationWorkbook (org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook)2 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 EscherBlipRecord (org.apache.poi.ddf.EscherBlipRecord)1 EscherRecord (org.apache.poi.ddf.EscherRecord)1 BackupRecord (org.apache.poi.hssf.record.BackupRecord)1 CommonObjectDataSubRecord (org.apache.poi.hssf.record.CommonObjectDataSubRecord)1 DrawingGroupRecord (org.apache.poi.hssf.record.DrawingGroupRecord)1 ExtendedFormatRecord (org.apache.poi.hssf.record.ExtendedFormatRecord)1