Bug 55661 - Support for option "shrink to fit"
Summary: Support for option "shrink to fit"
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.9-FINAL
Hardware: PC All
: P2 enhancement (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-10-17 11:16 UTC by Oliver Frank
Modified: 2013-11-07 22:39 UTC (History)
0 users



Attachments
Test file containing the option "shrink to fit" (24.50 KB, application/vnd.ms-excel)
2013-10-23 06:21 UTC, Oliver Frank
Details
The output of BiffViewer for a file without shrink to fit (156.78 KB, text/plain)
2013-10-25 08:08 UTC, Oliver Frank
Details
The output of BiffViewer for a file with shrink to fit (162.57 KB, text/plain)
2013-10-25 08:09 UTC, Oliver Frank
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Oliver Frank 2013-10-17 11:16:43 UTC
Hi,
excel provides the option "shrink to fit" for a cell, which autosizes the content of the cell, if it is too long.
Unfortunately it is not possible to set this option using POI.

Since the class "org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellAlignment" already supports this option, the necessessary enhancement should be quite simple.
I just need the following methods in XSSFCellStyle:

public void setShrinkToFit(boolean shrinkToFit) {
    getCellAlignment().setShrinkToFit(shrinkToFit);
}

public boolean getShrinkToFit() {
    CTCellAlignment align = _cellXf.getAlignment();
    return align != null && align.isSetShrinkToFit();
}

Regards
Oliver
Comment 1 Nick Burch 2013-10-22 22:12:50 UTC
Does Excel support this for .xls files, or is it .xlsx only?

(That would affect if we need to put it on the interface or not, and if we therefore need to get a test .xls file to identify the record there)
Comment 2 Oliver Frank 2013-10-23 06:21:49 UTC
Created attachment 30956 [details]
Test file containing the option "shrink to fit"

Excel supports this for .xls files too. I've added a test document which contains a cell with this option.
Comment 3 Nick Burch 2013-10-23 09:49:56 UTC
If you have some time, any chance you could create two very similar and simple .xls files? Firstly create one without shrink to fit, then open the same file + set shrink to fit + save-as. (Should help ensure they're very similar). Then, run org.apache.poi.hssf.dev.BiffViewer against the two, and diff the output. With any luck, that will flag up the record that gets changed, along with the option to do it!
Comment 4 lsj8080 2013-10-25 02:41:06 UTC
Hi POI developer,
excel provides the option "shrink to fit" for a cell, which autosizes the content of the cell, if it is too long in version 3.7. but I can't find the method 'setShrinkToFit' in class "org.apache.poi.hssf.usermodel.HSSFCellStyle" at version 3.9.
Comment 5 Oliver Frank 2013-10-25 08:08:00 UTC
I've created both test files (with and without shrink to fit) and diffed the output of BiffViewer. Unfortunately there are 275 differences between them.
But in the output for the file with shrink to fit a found the entry ".shrinktoft= true" in line 3475. I hope/guess this is the important difference between these files.
Comment 6 Oliver Frank 2013-10-25 08:08:41 UTC
Created attachment 30967 [details]
The output of BiffViewer for a file without shrink to fit
Comment 7 Oliver Frank 2013-10-25 08:09:29 UTC
Created attachment 30968 [details]
The output of BiffViewer for a file with shrink to fit
Comment 8 Nick Burch 2013-10-28 11:29:15 UTC
(In reply to Oliver Frank from comment #5)
>  for the file with shrink to fit a found the entry
> ".shrinktoft= true" in line 3475.

That looks very likely to be what we need! Which record does this live in? (Should be a few lines about that in the output)
Comment 9 Oliver Frank 2013-10-29 07:24:35 UTC
Here is the record, in which this line occurs:
Offset=0x00000C9A(3226) recno=114 sid=0x00E0 size=0x0014(20)
[EXTENDEDFORMAT]
 CELL_RECORD_TYPE
    .fontindex       = 0
    .formatindex     = 0
    .celloptions     = 1
          .islocked  = true
          .ishidden  = false
          .recordtype= 0
          .parentidx = 0
    .alignmentoptions= 20
          .alignment = 0
          .wraptext  = false
          .valignment= 2
          .justlast  = 0
          .rotation  = 0
    .indentionoptions= 1010
          .indent    = 0
          .shrinktoft= true
          .mergecells= false
          .readngordr= 0
          .formatflag= false
          .fontflag  = false
          .prntalgnmt= true
          .borderflag= false
          .paternflag= false
          .celloption= false
    .borderoptns     = 0
          .lftln     = 0
          .rgtln     = 0
          .topln     = 0
          .btmln     = 0
    .paleteoptns     = 0
          .leftborder= 0
          .rghtborder= 0
          .diag      = 0
    .paleteoptn2     = 2000000
          .topborder = 0
          .botmborder= 0
          .adtldiag  = 0
          .diaglnstyl= 0
          .fillpattrn= 0
    .fillpaloptn     = 20c0
          .foreground= 40
          .background= 41
[/EXTENDEDFORMAT]

Does this contain the information, that you need?
Comment 10 Nick Burch 2013-11-07 22:39:57 UTC
Thanks for your investigations, and test files

I've added support in r1539848, along with unit tests for it in r1539850.