Bug 57929 - Why HSSFWorkbook.removePrintArea(index) invalid?
Summary: Why HSSFWorkbook.removePrintArea(index) invalid?
Status: NEEDINFO
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.12-FINAL
Hardware: PC All
: P2 major (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-05-15 04:45 UTC by nova
Modified: 2016-06-20 08:06 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description nova 2015-05-15 04:45:31 UTC
Hi:
    I have a question, Why HSSFWorkbook.removePrintArea(index) invalid?  I use poi version 3.12.

Code:

 public static void main( String[] args )
    {

        try {
            String targetFile = "C:\\Users\\syf\\Desktop\\test_1_oo.xls";
            Workbook wb = null;
            FileInputStream fis = new FileInputStream(targetFile);
            wb = new HSSFWorkbook(fis);
            System.out.println(wb.getPrintArea(0));
            System.out.println(wb.getPrintArea(1));

            wb.removePrintArea(0);
            wb.setPrintArea(1, 0, 0, 0, 0);

            System.out.println("-------------------");
            System.out.println(wb.getPrintArea(0));
            System.out.println(wb.getPrintArea(1));
            fis.close();
            FileOutputStream fileOut = new FileOutputStream(targetFile);
            wb.write(fileOut);
            fileOut.flush();
            fileOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

First Run Result:
    Sheet1!$A$1:$C$6  // this value is my setting
    Sheet2!$B$1:$C$5  // this value is my setting
    -------------------
    null                           // after remove print area , the value is this
    Sheet2!$A$1:$A$1  // after remove print area , the value is this

Second Run Result:
    Sheet1!$A$1:$C$6  // this value should 'null' but ...
    Sheet2!$A$1:$A$1  
    -------------------
    null
    Sheet2!$A$1:$A$1

                                                                                                                       Thanks a lot !
Comment 1 Javen O'Neal 2016-06-20 08:06:01 UTC
Thanks for the simple test case. I added this to our test suite in r

I have verified the behavior your observed using an HSSFWorkbook. This problem does not occur on XSSFWorkbooks or SXSSFWorkbooks.

If this bug is still relevant to you, you could try to dive through the code in HSSFWorkbook/InternalWorkbook to figure out what's going on. It is possible that the Excel 97-2003 format doesn't allow null print areas, instead using some default print area.

Here's what I tried:

HSSFWorkbook:
>  public void removePrintArea(int sheetIndex) {
>+     // Clear out the formula tokens (equivalent to making this reference null)
>+     NameRecord name = workbook.getSpecificBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex+1);
>+     name.setNameDefinition(new Ptg[] {});
>+     
>      getWorkbook().removeBuiltinRecord(NameRecord.BUILTIN_PRINT_AREA, sheetIndex+1);
>  }
got:
> ptgs must not be null
> java.lang.IllegalArgumentException: ptgs must not be null
> 	at org.apache.poi.ss.formula.FormulaRenderer.toFormulaString(FormulaRenderer.java:48)
> 	at org.apache.poi.hssf.model.HSSFFormulaParser.toFormulaString(HSSFFormulaParser.java:81)
> 	at org.apache.poi.hssf.usermodel.HSSFWorkbook.getPrintArea(HSSFWorkbook.java:1547)
> 	at org.apache.poi.ss.usermodel.BaseTestBugzillaIssues.test57929(BaseTestBugzillaIssues.java:1605)

HSSFWorkbook:
> ...
>+     name.setNameDefinition(HSSFFormulaParser.parse("", this, FormulaType.NAMEDRANGE, sheetIndex));
> ...
got:
> Parse error near char 0 '' in specified formula ''. Expected cell ref or constant literal
> org.apache.poi.ss.formula.FormulaParseException: Parse error near char 0 '' in specified formula ''. Expected cell ref or constant literal

HSSFWorkbook:
> ...
>+     name.setNameDefinition(HSSFFormulaParser.parse("'"+getSheetName(0)+"'!$A$1:$A$1", this, FormulaType.NAMEDRANGE, sheetIndex));
> ...
got:
> Sheet0 after write expected null, but was:<Sheet0!$A$1:$A$1>
> junit.framework.AssertionFailedError: Sheet0 after write expected null, but was:<Sheet0!$A$1:$A$1>
> 	at org.apache.poi.ss.usermodel.BaseTestBugzillaIssues.test57929(BaseTestBugzillaIssues.java:1605

HSSFWorkbook
> ...
>+     name.setNameDefinition(new Ptg[] { new Area3DPtg(-1, -1, -1, -1, false, false, false, false, sheetIndex) });
> ...
got:
> Sheet0 after write expected null, but was:<Sheet0!$XFD$65536:$XFD$65536>
> junit.framework.AssertionFailedError: Sheet0 after write expected null, but was:<Sheet0!$XFD$65536:$XFD$65536>

To fix this, someone needs to figure out how Excel stores records in the binary format to indicate no print area has been set, if it falls back to something like A1:lastCol/lastRow), or picks a print area based on the current page size and the width of the columns/height of the rows.