* contain text, numbers, dates, and formulas. Cells can also be formatted.
* </p>
*/
+@SuppressWarnings("deprecation")
public class XSSFSheet extends POIXMLDocumentPart implements Sheet {
private static final POILogger logger = POILogFactory.getLogger(XSSFSheet.class);
/**
* Verify that candidate region does not intersect with an existing merged region in this sheet
*
- * @param candidateRegion
+ * @param candidateRegion the range of cells to verify
* @throws IllegalStateException if candidate region intersects an existing merged region in this sheet (or candidateRegion is already merged in this sheet)
*/
private void validateMergedRegions(CellRangeAddress candidateRegion) {
/**
* Get a Hyperlink in this sheet anchored at row, column
*
- * @param row
- * @param column
+ * @param row The row where the hyperlink is anchored
+ * @param column The column where the hyperlinkn is anchored
* @return hyperlink if there is a hyperlink anchored at row, column; otherwise returns null
*/
@Override
*
* @param startRowNum the first row number in this sheet to return
* @param endRowNum the last row number in this sheet to return
- * @param createRowIfMissing
- * @return All rows between startRow and endRow, inclusive
+ * @param createRowIfMissing If missing rows should be created.
+ * @return All rows between startRow and endRow, inclusive. If createRowIfMissing is false,
+ * only previously existing rows are returned, otherwise empty rows are added as necessary
* @throws IllegalArgumentException if startRowNum and endRowNum are not in ascending order
*/
private List<XSSFRow> getRows(int startRowNum, int endRowNum, boolean createRowIfMissing) {
* 'Collapsed' state is stored in a single column col info record
* immediately after the outline group
*
- * @param idx
+ * @param idx The column-index to check
* @return a boolean represented if the column is collapsed
*/
private boolean isColumnGroupCollapsed(int idx) {
rebuildRows();
}
- private final void rebuildRows() {
+ private void rebuildRows() {
//rebuild the _rows map
List<XSSFRow> rowList = new ArrayList<>(_rows.values());
_rows.clear();
// then do the actual moving and also adjust comments/rowHeight
// we need to sort it in a way so the shifting does not mess up the structures,
// i.e. when shifting down, start from down and go up, when shifting up, vice-versa
- SortedMap<XSSFComment, Integer> commentsToShift = new TreeMap<>(new Comparator<XSSFComment>() {
- @Override
- public int compare(XSSFComment o1, XSSFComment o2) {
- int row1 = o1.getRow();
- int row2 = o2.getRow();
-
- if (row1 == row2) {
- // ordering is not important when row is equal, but don't return zero to still
- // get multiple comments per row into the map
- return o1.hashCode() - o2.hashCode();
- }
+ SortedMap<XSSFComment, Integer> commentsToShift = new TreeMap<>((o1, o2) -> {
+ int row1 = o1.getRow();
+ int row2 = o2.getRow();
+
+ if (row1 == row2) {
+ // ordering is not important when row is equal, but don't return zero to still
+ // get multiple comments per row into the map
+ return o1.hashCode() - o2.hashCode();
+ }
- // when shifting down, sort higher row-values first
- if (n > 0) {
- return row1 < row2 ? 1 : -1;
- } else {
- // sort lower-row values first when shifting up
- return row1 > row2 ? 1 : -1;
- }
+ // when shifting down, sort higher row-values first
+ if (n > 0) {
+ return row1 < row2 ? 1 : -1;
+ } else {
+ // sort lower-row values first when shifting up
+ return row1 > row2 ? 1 : -1;
}
});
// then do the actual moving and also adjust comments/rowHeight
// we need to sort it in a way so the shifting does not mess up the structures,
// i.e. when shifting down, start from down and go up, when shifting up, vice-versa
- SortedMap<XSSFComment, Integer> commentsToShift = new TreeMap<>(new Comparator<XSSFComment>() {
- @Override
- public int compare(XSSFComment o1, XSSFComment o2) {
- int column1 = o1.getColumn();
- int column2 = o2.getColumn();
-
- if (column1 == column2) {
- // ordering is not important when row is equal, but don't return zero to still
- // get multiple comments per row into the map
- return o1.hashCode() - o2.hashCode();
- }
+ SortedMap<XSSFComment, Integer> commentsToShift = new TreeMap<>((o1, o2) -> {
+ int column1 = o1.getColumn();
+ int column2 = o2.getColumn();
+
+ if (column1 == column2) {
+ // ordering is not important when row is equal, but don't return zero to still
+ // get multiple comments per row into the map
+ return o1.hashCode() - o2.hashCode();
+ }
- // when shifting down, sort higher row-values first
- if (n > 0) {
- return column1 < column2 ? 1 : -1;
- } else {
- // sort lower-row values first when shifting up
- return column1 > column2 ? 1 : -1;
- }
+ // when shifting down, sort higher row-values first
+ if (n > 0) {
+ return column1 < column2 ? 1 : -1;
+ } else {
+ // sort lower-row values first when shifting up
+ return column1 > column2 ? 1 : -1;
}
});
* Creates a new Table, and associates it with this Sheet.
*
* @param tableArea
- * the area that the table should cover, should not be {@null}
+ * the area that the table should cover, should not be null
* @return the created table
* @since 4.0.0
*/
+ "defined source sheet " + sourceSheet.getSheetName() + ".");
}
- return createPivotTable(position, sourceSheet, new PivotTableReferenceConfigurator() {
- @Override
- public void configureReference(CTWorksheetSource wsSource) {
- final String[] firstCell = source.getFirstCell().getCellRefParts();
- final String firstRow = firstCell[1];
- final String firstCol = firstCell[2];
- final String[] lastCell = source.getLastCell().getCellRefParts();
- final String lastRow = lastCell[1];
- final String lastCol = lastCell[2];
- final String ref = firstCol+firstRow+':'+lastCol+lastRow; //or just source.formatAsString()
- wsSource.setRef(ref);
- }
+ return createPivotTable(position, sourceSheet, wsSource -> {
+ final String[] firstCell = source.getFirstCell().getCellRefParts();
+ final String firstRow = firstCell[1];
+ final String firstCol = firstCell[2];
+ final String[] lastCell = source.getLastCell().getCellRefParts();
+ final String lastRow = lastCell[1];
+ final String lastCol = lastCell[2];
+ final String ref = firstCol+firstRow+':'+lastCol+lastRow; //or just source.formatAsString()
+ wsSource.setRef(ref);
});
}
+ "defined source sheet " + sourceSheet.getSheetName() + ".");
}
- return createPivotTable(position, sourceSheet, new PivotTableReferenceConfigurator() {
- @Override
- public void configureReference(CTWorksheetSource wsSource) {
- wsSource.setName(source.getNameName());
- }
- });
+ return createPivotTable(position, sourceSheet, wsSource -> wsSource.setName(source.getNameName()));
}
/**
*/
@Beta
public XSSFPivotTable createPivotTable(final Table source, CellReference position) {
- return createPivotTable(position, getWorkbook().getSheet(source.getSheetName()), new PivotTableReferenceConfigurator() {
- @Override
- public void configureReference(CTWorksheetSource wsSource) {
- wsSource.setName(source.getName());
- }
- });
+ return createPivotTable(position, getWorkbook().getSheet(source.getSheetName()), wsSource -> wsSource.setName(source.getName()));
}
/**
/**
* when a cell with a 'master' shared formula is removed, the next cell in the range becomes the master
- * @param cell
+ * @param cell The cell that is removed
* @param evalWb BaseXSSFEvaluationWorkbook in use, if one exists
*/
protected void onDeleteFormula(XSSFCell cell, BaseXSSFEvaluationWorkbook evalWb){
import java.io.IOException;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
}
@Test
- public void testAddCleanColIntoColsExactOverlap() throws Exception {
+ public void testAddCleanColIntoColsExactOverlap() {
CTCols cols = createHiddenAndBestFitColsWithHelper(1, 1, 1, 1);
assertEquals(1, cols.sizeOfColArray());
assertMinMaxHiddenBestFit(cols, 0, 1, 1, true, true);
}
@Test
- public void testAddCleanColIntoColsOverlapsOverhangingBothSides() throws Exception {
+ public void testAddCleanColIntoColsOverlapsOverhangingBothSides() {
CTCols cols = createHiddenAndBestFitColsWithHelper(2, 2, 1, 3);
assertEquals(3, cols.sizeOfColArray());
assertMinMaxHiddenBestFit(cols, 0, 1, 1, false, true);
}
@Test
- public void testAddCleanColIntoColsOverlapsCompletelyNested() throws Exception {
+ public void testAddCleanColIntoColsOverlapsCompletelyNested() {
CTCols cols = createHiddenAndBestFitColsWithHelper(1, 3, 2, 2);
assertEquals(3, cols.sizeOfColArray());
assertMinMaxHiddenBestFit(cols, 0, 1, 1, true, false);
}
@Test
- public void testAddCleanColIntoColsNewOverlapsOverhangingLeftNotRightExactRight() throws Exception {
+ public void testAddCleanColIntoColsNewOverlapsOverhangingLeftNotRightExactRight() {
CTCols cols = createHiddenAndBestFitColsWithHelper(2, 3, 1, 3);
assertEquals(2, cols.sizeOfColArray());
assertMinMaxHiddenBestFit(cols, 0, 1, 1, false, true);
}
@Test
- public void testAddCleanColIntoColsNewOverlapsOverhangingRightNotLeftExactLeft() throws Exception {
+ public void testAddCleanColIntoColsNewOverlapsOverhangingRightNotLeftExactLeft() {
CTCols cols = createHiddenAndBestFitColsWithHelper(1, 2, 1, 3);
assertEquals(2, cols.sizeOfColArray());
assertMinMaxHiddenBestFit(cols, 0, 1, 2, true, true);
}
@Test
- public void testAddCleanColIntoColsNewOverlapsOverhangingLeftNotRight() throws Exception {
+ public void testAddCleanColIntoColsNewOverlapsOverhangingLeftNotRight() {
CTCols cols = createHiddenAndBestFitColsWithHelper(2, 3, 1, 2);
assertEquals(3, cols.sizeOfColArray());
assertMinMaxHiddenBestFit(cols, 0, 1, 1, false, true);
}
@Test
- public void testAddCleanColIntoColsNewOverlapsOverhangingRightNotLeft() throws Exception {
+ public void testAddCleanColIntoColsNewOverlapsOverhangingRightNotLeft() {
CTCols cols = createHiddenAndBestFitColsWithHelper(1, 2, 2, 3);
assertEquals(3, cols.sizeOfColArray());
assertMinMaxHiddenBestFit(cols, 0, 1, 1, true, false);
}
return count;
}
+
+ @SuppressWarnings("deprecation")
+ @Test
+ public void testColumnsCollapsed() {
+ Workbook wb = new XSSFWorkbook();
+ Sheet sheet = wb.createSheet("test");
+ Row row = sheet.createRow(0);
+ row.createCell(0);
+ row.createCell(1);
+ row.createCell(2);
+
+ sheet.setColumnWidth(0, 10);
+ sheet.setColumnWidth(1, 10);
+ sheet.setColumnWidth(2, 10);
+
+ sheet.groupColumn(0, 1);
+ sheet.setColumnGroupCollapsed(0, true);
+
+ CTCols ctCols = ((XSSFSheet) sheet).getCTWorksheet().getColsArray()[0];
+ assertEquals(3, ctCols.sizeOfColArray());
+ assertTrue(ctCols.getColArray(0).isSetCollapsed());
+ assertTrue(ctCols.getColArray(1).isSetCollapsed());
+ assertTrue(ctCols.getColArray(2).isSetCollapsed());
+
+ ColumnHelper helper = new ColumnHelper(CTWorksheet.Factory.newInstance());
+ helper.setColumnAttributes(ctCols.getColArray(1), ctCols.getColArray(2));
+
+ ctCols = ((XSSFSheet) sheet).getCTWorksheet().getColsArray()[0];
+ assertTrue(ctCols.getColArray(0).isSetCollapsed());
+ assertTrue(ctCols.getColArray(1).isSetCollapsed());
+ assertTrue(ctCols.getColArray(2).isSetCollapsed());
+ }
}