diff --git a/src/java/org/apache/poi/ss/usermodel/Cell.java b/src/java/org/apache/poi/ss/usermodel/Cell.java index 6f960a30d7..21e701490e 100644 --- a/src/java/org/apache/poi/ss/usermodel/Cell.java +++ b/src/java/org/apache/poi/ss/usermodel/Cell.java @@ -89,6 +89,13 @@ public interface Cell { */ void setCellType(CellType cellType); + /** + * Removes formula and value from the cell, and sets its type to {@link CellType#BLANK}. + * Preserves comments and hyperlinks. + * While {@link #setCellType(CellType)} exists, is an alias for {@code setCellType(CellType.BLANK)}. + */ + void setBlank(); + /** * Return the cell type. * diff --git a/src/java/org/apache/poi/ss/usermodel/CellBase.java b/src/java/org/apache/poi/ss/usermodel/CellBase.java index 7e3929b236..9777739fcf 100644 --- a/src/java/org/apache/poi/ss/usermodel/CellBase.java +++ b/src/java/org/apache/poi/ss/usermodel/CellBase.java @@ -40,6 +40,14 @@ public abstract class CellBase implements Cell { setCellTypeImpl(cellType); } + /** + * {@inheritDoc} + */ + @Override + public void setBlank() { + setCellType(CellType.BLANK); + } + /** * Implementation-specific logic * @param cellType new cell type. Guaranteed non-null, not _NONE. diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 9eadae5b26..66061f4c64 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -944,7 +944,7 @@ public final class XSSFCell extends CellBase { * Blanks this cell. Blank cells have no formula or value but may have styling. * This method erases all the data previously associated with this cell. */ - private void setBlank(){ + private void setBlankPrivate(){ CTCell blank = CTCell.Factory.newInstance(); blank.setR(_cell.getR()); if(_cell.isSetS()) { @@ -1010,7 +1010,7 @@ public final class XSSFCell extends CellBase { } break; case BLANK: - setBlank(); + setBlankPrivate(); break; case BOOLEAN: String newVal = convertCellValueToBoolean() ? TRUE_AS_STRING : FALSE_AS_STRING; diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index 9afa79f956..94a003dcb1 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -24,6 +24,9 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -32,13 +35,11 @@ import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import java.util.TimeZone; -import java.util.function.Consumer; import org.apache.poi.common.usermodel.HyperlinkType; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.ITestDataProvider; import org.apache.poi.ss.SpreadsheetVersion; -import org.apache.poi.ss.formula.FormulaParseException; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.util.LocaleUtil; import org.junit.Test; @@ -1357,6 +1358,16 @@ public abstract class BaseTestCell { assertTrue(cell.getBooleanCellValue()); } + @Test + public final void setBlank_delegatesTo_setCellType_BLANK() { + Cell cell = mock(CellBase.class); + doCallRealMethod().when(cell).setBlank(); + + cell.setBlank(); + + verify(cell).setCellType(CellType.BLANK); + } + private Cell getInstance() { return _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0); }