When the specified column is larger than the value of getFirstColumn by the loop of Sheet.getColumnWidth, an exact columninfo object is not returned. public short getColumnWidth(short column) { short retval = 0; ColumnInfoRecord ci = null; int k = 0; if (columnSizes != null) { for (k = 0; k < columnSizes.size(); k++) { ci = ( ColumnInfoRecord ) columnSizes.get(k); - if ((ci.getFirstColumn() >= column) + if ((ci.getFirstColumn() <= column) && (column <= ci.getLastColumn())) { break; } ci = null; } } if (ci != null) { retval = ci.getColumnWidth(); } else { retval = defaultcolwidth.getColWidth(); } return retval; }
Created attachment 5602 [details] I found same bug at org.apache.poi.hssf.model.Sheet#setColumn.So I submitt as a patch.
Sorry, not Sheet#setColumn() but Sheet#setColumnWidth().
The bug Mr.Tanino reported is a simple bug,and I found same problem because of another reason. When user changed plural column's width collectively and saved,the info about the operation is exist only the first column's info.(I see the info by XML Element generated by MS OfficeXP.Maybe OLE2 file keep it with another sytle,but I don't know the spec deeply) So,user can't get correct width about rest columns by Sheet#getColumnWidth().
...Sorry,I didn't read org.apache.poi.hssf.record.ColumnInfoRecord. The Logic I said is already implemented. ...So,why a problem happen in my local env?hmm...
Very sorry,I tested POI without Mr.tanino's patch X(
Sorry, now I am confused. Which patch should i apply?
Created attachment 5606 [details] Testcase to find this problem
Because I am stupid,I am sorry to make you confused...X( The attatchment I submitted is a copy of that Mr.Tanino wrote. To fix org.apache.poi.hssf.model.Sheet#getColumnWidth(),please apply it. And I found same mistake in #setColumnWidth(),so in the patch I apply to fix it. BTW, Now I submitted a testcase testing #getColumnWidth() and #setColumnWidth(). And I found a new problem with the testcase... By the testcase,ColumnInfoRecord should be splited to 3 records,ColumnNo=5, and ColumnNo=6,and ColumnNo=7~10. But now there is no implementation like so in #setColumnWidth(). So the last asserEquals() block in the testcase will throw a Error.
Created attachment 5614 [details] PATCH to fix the case like,a ColumnInfoRecord should be splited to 3 records
Committed. Please cross check. unit tests passed.