[bug-64536] add getDimension on XSSFSheet

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1899091 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-03-20 21:41:31 +00:00
parent d013349052
commit 56b4313997
3 changed files with 38 additions and 0 deletions

View File

@ -3962,6 +3962,20 @@ public class XSSFSheet extends POIXMLDocumentPart implements Sheet, OoxmlSheetEx
safeGetProtectionField().setSelectUnlockedCells(enabled); safeGetProtectionField().setSelectUnlockedCells(enabled);
} }
/**
* Reads the dimensions of the sheet data
* @return dimensions of the sheet data as a Cell Range (can be null)
* @since POI 5.2.3
*/
public CellRangeAddress getDimension() {
CTSheetDimension ctSheetDimension = worksheet.getDimension();
String ref = ctSheetDimension == null ? null : ctSheetDimension.getRef();
if (ref != null) {
return CellRangeAddress.valueOf(ref);
}
return null;
}
private CTSheetProtection safeGetProtectionField() { private CTSheetProtection safeGetProtectionField() {
if (!isSheetProtectionEnabled()) { if (!isSheetProtectionEnabled()) {
return worksheet.addNewSheetProtection(); return worksheet.addNewSheetProtection();

View File

@ -2253,4 +2253,28 @@ public final class TestXSSFSheet extends BaseTestXSheet {
assertTrue(row2.getZeroHeight()); assertTrue(row2.getZeroHeight());
} }
} }
@Test
void testBug64536() throws IOException {
try (
XSSFWorkbook xssfWorkbook = (XSSFWorkbook) _testDataProvider.openSampleWorkbook("1_NoIden.xlsx");
UnsynchronizedByteArrayOutputStream bos = new UnsynchronizedByteArrayOutputStream()
) {
XSSFSheet fileSheet = xssfWorkbook.getSheetAt(0);
assertEquals(CellRangeAddress.valueOf("B1:D9"), fileSheet.getDimension());
// Create Row and Cell, Then Set a Value
// 5, 2, 4 is just random number for test
Row fileRow = fileSheet.createRow(5);
Cell fileCell = fileRow.createCell(2, CellType.STRING);
fileCell.setCellValue("TEST VALUE");
xssfWorkbook.write(bos);
try (XSSFWorkbook xssfWorkbook2 = new XSSFWorkbook(bos.toInputStream())) {
XSSFSheet xssfSheet = xssfWorkbook2.getSheetAt(0);
assertEquals(CellRangeAddress.valueOf("B1:F9"), xssfSheet.getDimension());
}
}
}
} }

Binary file not shown.