Bug 55723: Make HSSFSheet.setAutoFilter() behave equal to XSSF, also make XSSF work when setAutoFilter is called multiple times

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1554688 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2014-01-01 22:15:44 +00:00
parent 5e77641784
commit d714d0da65
5 changed files with 95 additions and 9 deletions

View File

@ -2056,8 +2056,6 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
}
public HSSFAutoFilter setAutoFilter(CellRangeAddress range) {
InternalWorkbook workbook = _workbook.getWorkbook();
int sheetIndex = _workbook.getSheetIndex(this);
@ -2067,8 +2065,15 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
name = workbook.createBuiltInName(NameRecord.BUILTIN_FILTER_DB, sheetIndex + 1);
}
int firstRow = range.getFirstRow();
// if row was not given when constructing the range...
if(firstRow == -1) {
firstRow = 0;
}
// The built-in name must consist of a single Area3d Ptg.
Area3DPtg ptg = new Area3DPtg(range.getFirstRow(), range.getLastRow(),
Area3DPtg ptg = new Area3DPtg(firstRow, range.getLastRow(),
range.getFirstColumn(), range.getLastColumn(),
false, false, false, false, sheetIndex);
name.setNameDefinition(new Ptg[]{ptg});
@ -2084,7 +2089,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
HSSFPatriarch p = createDrawingPatriarch();
for (int col = range.getFirstColumn(); col <= range.getLastColumn(); col++) {
p.createComboBox(new HSSFClientAnchor(0, 0, 0, 0,
(short) col, range.getFirstRow(), (short) (col + 1), range.getFirstRow() + 1));
(short) col, firstRow, (short) (col + 1), firstRow + 1));
}
return new HSSFAutoFilter(this);

View File

@ -3194,12 +3194,13 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
XSSFName name = wb.getBuiltInName(XSSFName.BUILTIN_FILTER_DB, sheetIndex);
if (name == null) {
name = wb.createBuiltInName(XSSFName.BUILTIN_FILTER_DB, sheetIndex);
name.getCTName().setHidden(true);
CellReference r1 = new CellReference(getSheetName(), range.getFirstRow(), range.getFirstColumn(), true, true);
CellReference r2 = new CellReference(null, range.getLastRow(), range.getLastColumn(), true, true);
String fmla = r1.formatAsString() + ":" + r2.formatAsString();
name.setRefersToFormula(fmla);
}
name.getCTName().setHidden(true);
CellReference r1 = new CellReference(getSheetName(), range.getFirstRow(), range.getFirstColumn(), true, true);
CellReference r2 = new CellReference(null, range.getLastRow(), range.getLastColumn(), true, true);
String fmla = r1.formatAsString() + ":" + r2.formatAsString();
name.setRefersToFormula(fmla);
return new XSSFAutoFilter(this);
}

View File

@ -21,6 +21,7 @@ import java.util.List;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.PasswordRecord;
import org.apache.poi.ss.usermodel.AutoFilter;
import org.apache.poi.ss.usermodel.BaseTestSheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
@ -1228,4 +1229,30 @@ public final class TestXSSFSheet extends BaseTestSheet {
assertEquals("Sheet should contain 8 tables", 8, tables.size());
assertNotNull("Sheet should contain a comments table", sheet.getCommentsTable(false));
}
public void testBug55723b(){
XSSFWorkbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet();
// stored with a special name
assertNull(wb.getBuiltInName(XSSFName.BUILTIN_FILTER_DB, 0));
CellRangeAddress range = CellRangeAddress.valueOf("A:B");
AutoFilter filter = sheet.setAutoFilter(range);
assertNotNull(filter);
// stored with a special name
XSSFName name = wb.getBuiltInName(XSSFName.BUILTIN_FILTER_DB, 0);
assertNotNull(name);
assertEquals("Sheet0!$A:$B", name.getRefersToFormula());
range = CellRangeAddress.valueOf("B:C");
filter = sheet.setAutoFilter(range);
assertNotNull(filter);
// stored with a special name
name = wb.getBuiltInName(XSSFName.BUILTIN_FILTER_DB, 0);
assertNotNull(name);
assertEquals("Sheet0!$B:$C", name.getRefersToFormula());
}
}

View File

@ -35,6 +35,7 @@ import org.apache.poi.hssf.record.aggregates.WorksheetProtectionBlock;
import org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector;
import org.apache.poi.ss.formula.ptg.Area3DPtg;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.usermodel.AutoFilter;
import org.apache.poi.ss.usermodel.BaseTestSheet;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
@ -1066,4 +1067,20 @@ public final class TestHSSFSheet extends BaseTestSheet {
sheet.dumpDrawingRecords(true);*/
assertNull(sheet.getDrawingEscherAggregate());
}
public void testBug55723b() {
HSSFWorkbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
// stored with a special name
assertNull(wb.getWorkbook().getSpecificBuiltinRecord(NameRecord.BUILTIN_FILTER_DB, 1));
CellRangeAddress range = CellRangeAddress.valueOf("A:B");
AutoFilter filter = sheet.setAutoFilter(range);
assertNotNull(filter);
// stored with a special name
NameRecord record = wb.getWorkbook().getSpecificBuiltinRecord(NameRecord.BUILTIN_FILTER_DB, 1);
assertNotNull(record);
}
}

View File

@ -21,6 +21,7 @@ import java.util.Iterator;
import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.PaneInformation;
import org.apache.poi.ss.ITestDataProvider;
import org.apache.poi.ss.SpreadsheetVersion;
@ -800,4 +801,39 @@ public abstract class BaseTestSheet extends TestCase {
Sheet sheet = wb.createSheet();
sheet.showInPane(2, 3);
}
public void testBug55723(){
Workbook wb = _testDataProvider.createWorkbook();
Sheet sheet = wb.createSheet();
CellRangeAddress range = CellRangeAddress.valueOf("A:B");
AutoFilter filter = sheet.setAutoFilter(range);
assertNotNull(filter);
// there seems to be currently no generic way to check the setting...
range = CellRangeAddress.valueOf("B:C");
filter = sheet.setAutoFilter(range);
assertNotNull(filter);
// there seems to be currently no generic way to check the setting...
}
public void testBug55723_Rows() {
HSSFWorkbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
CellRangeAddress range = CellRangeAddress.valueOf("A4:B55000");
AutoFilter filter = sheet.setAutoFilter(range);
assertNotNull(filter);
}
public void testBug55723d_RowsOver65k() {
HSSFWorkbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
CellRangeAddress range = CellRangeAddress.valueOf("A4:B75000");
AutoFilter filter = sheet.setAutoFilter(range);
assertNotNull(filter);
}
}