package org.apache.poi.xssf.usermodel;
import static org.hamcrest.core.IsEqual.equalTo;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.functions.Function;
-import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellStyle;
-import org.apache.poi.ss.usermodel.CellValue;
-import org.apache.poi.ss.usermodel.ClientAnchor;
-import org.apache.poi.ss.usermodel.Comment;
-import org.apache.poi.ss.usermodel.CreationHelper;
-import org.apache.poi.ss.usermodel.DataFormatter;
-import org.apache.poi.ss.usermodel.Drawing;
-import org.apache.poi.ss.usermodel.Font;
-import org.apache.poi.ss.usermodel.FormulaError;
-import org.apache.poi.ss.usermodel.FormulaEvaluator;
-import org.apache.poi.ss.usermodel.IndexedColors;
-import org.apache.poi.ss.usermodel.Name;
-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.ss.usermodel.WorkbookFactory;
+import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
}
/**
- * Various ways of removing a cell formula should all zap
- * the calcChain entry.
+ * Various ways of removing a cell formula should all zap the calcChain
+ * entry.
*/
@Test
public void bug49966() throws Exception {
- XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("shared_formulas.xlsx");
- XSSFSheet sheet = wb.getSheetAt(0);
-
- // CalcChain has lots of entries
- CalculationChain cc = wb.getCalculationChain();
- assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
- assertEquals("A3", cc.getCTCalcChain().getCArray(1).getR());
- assertEquals("A4", cc.getCTCalcChain().getCArray(2).getR());
- assertEquals("A5", cc.getCTCalcChain().getCArray(3).getR());
- assertEquals("A6", cc.getCTCalcChain().getCArray(4).getR());
- assertEquals("A7", cc.getCTCalcChain().getCArray(5).getR());
- assertEquals("A8", cc.getCTCalcChain().getCArray(6).getR());
- assertEquals(40, cc.getCTCalcChain().sizeOfCArray());
-
- // Try various ways of changing the formulas
- // If it stays a formula, chain entry should remain
- // Otherwise should go
- sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay
- sheet.getRow(2).getCell(0).setCellFormula(null); // go
- sheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_FORMULA); // stay
- sheet.getRow(4).getCell(0).setCellType(Cell.CELL_TYPE_STRING); // go
- sheet.getRow(5).removeCell(
- sheet.getRow(5).getCell(0) // go
- );
- sheet.getRow(6).getCell(0).setCellType(Cell.CELL_TYPE_BLANK); // go
- sheet.getRow(7).getCell(0).setCellValue((String)null); // go
+ XSSFWorkbook wb = XSSFTestDataSamples
+ .openSampleWorkbook("shared_formulas.xlsx");
+ XSSFSheet sheet = wb.getSheetAt(0);
+
+ Workbook wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+
+ // CalcChain has lots of entries
+ CalculationChain cc = wb.getCalculationChain();
+ assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
+ assertEquals("A3", cc.getCTCalcChain().getCArray(1).getR());
+ assertEquals("A4", cc.getCTCalcChain().getCArray(2).getR());
+ assertEquals("A5", cc.getCTCalcChain().getCArray(3).getR());
+ assertEquals("A6", cc.getCTCalcChain().getCArray(4).getR());
+ assertEquals("A7", cc.getCTCalcChain().getCArray(5).getR());
+ assertEquals("A8", cc.getCTCalcChain().getCArray(6).getR());
+ assertEquals(40, cc.getCTCalcChain().sizeOfCArray());
+
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+
+ // Try various ways of changing the formulas
+ // If it stays a formula, chain entry should remain
+ // Otherwise should go
+ sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay
+ sheet.getRow(2).getCell(0).setCellFormula(null); // go
+ sheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_FORMULA); // stay
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet.getRow(4).getCell(0).setCellType(Cell.CELL_TYPE_STRING); // go
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+
+ validateCells(sheet);
+ sheet.getRow(5).removeCell(sheet.getRow(5).getCell(0)); // go
+ validateCells(sheet);
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+
+ sheet.getRow(6).getCell(0).setCellType(Cell.CELL_TYPE_BLANK); // go
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ sheet.getRow(7).getCell(0).setCellValue((String) null); // go
- // Save and check
- wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
- assertEquals(35, cc.getCTCalcChain().sizeOfCArray());
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
- cc = wb.getCalculationChain();
- assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
- assertEquals("A4", cc.getCTCalcChain().getCArray(1).getR());
- assertEquals("A9", cc.getCTCalcChain().getCArray(2).getR());
+ // Save and check
+ wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ assertEquals(35, cc.getCTCalcChain().sizeOfCArray());
+ cc = wb.getCalculationChain();
+ assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
+ assertEquals("A4", cc.getCTCalcChain().getCArray(1).getR());
+ assertEquals("A9", cc.getCTCalcChain().getCArray(2).getR());
+ }
+
+ @Test
+ public void bug49966Row() throws Exception {
+ XSSFWorkbook wb = XSSFTestDataSamples
+ .openSampleWorkbook("shared_formulas.xlsx");
+ XSSFSheet sheet = wb.getSheetAt(0);
+
+ validateCells(sheet);
+ sheet.getRow(5).removeCell(sheet.getRow(5).getCell(0)); // go
+ validateCells(sheet);
+ }
+
+ private void validateCells(XSSFSheet sheet) {
+ for(Row row : sheet) {
+ // trigger handling
+ ((XSSFRow)row).onDocumentWrite();
+ }
}
@Test
package org.apache.poi.xssf.usermodel;
-import org.apache.poi.ss.usermodel.*;
+import java.io.IOException;
+
+import org.apache.poi.ss.usermodel.BaseTestCell;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.DataFormatter;
+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.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.XSSFITestDataProvider;
+import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.model.SharedStringsTable;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
-
-import java.io.FileOutputStream;
-import java.io.IOException;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType;
/**
* @author Yegor Kozlov
}
}
}
+
+ public void test56170() throws IOException {
+ final Workbook wb = XSSFTestDataSamples.openSampleWorkbook("56170.xlsx");
+ final XSSFSheet sheet = (XSSFSheet) wb.getSheetAt(0);
+
+ Workbook wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ Cell cell;
+
+ // add some contents to table so that the table will need expansion
+ Row row = sheet.getRow(0);
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ cell = row.createCell(0);
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ cell.setCellValue("demo1");
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ cell = row.createCell(1);
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ cell.setCellValue("demo2");
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ cell = row.createCell(2);
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ cell.setCellValue("demo3");
+
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+
+ row = sheet.getRow(1);
+ cell = row.createCell(0);
+ cell.setCellValue("demo1");
+ cell = row.createCell(1);
+ cell.setCellValue("demo2");
+ cell = row.createCell(2);
+ cell.setCellValue("demo3");
+
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+
+ // expand table
+ XSSFTable table = sheet.getTables().get(0);
+ final CellReference startRef = table.getStartCellReference();
+ final CellReference endRef = table.getEndCellReference();
+ table.getCTTable().setRef(new CellRangeAddress(startRef.getRow(), 1, startRef.getCol(), endRef.getCol()).formatAsString());
+
+ wbRead = XSSFTestDataSamples.writeOutAndReadBack(wb);
+ assertNotNull(wbRead);
+
+ /*FileOutputStream stream = new FileOutputStream("c:\\temp\\output.xlsx");
+ workbook.write(stream);
+ stream.close();*/
+ }
+
+ public void test56170Reproduce() throws IOException {
+ final Workbook wb = new XSSFWorkbook();
+ final Sheet sheet = wb.createSheet();
+ Row row = sheet.createRow(0);
+
+ // by creating Cells out of order we trigger the handling in onDocumentWrite()
+ Cell cell1 = row.createCell(1);
+ Cell cell2 = row.createCell(0);
+
+ validateRow(row);
+
+ validateRow(row);
+ // once again with removing one cell
+ row.removeCell(cell1);
+
+ validateRow(row);
+
+ // once again with removing one cell
+ row.removeCell(cell1);
+
+ // now check again
+ validateRow(row);
+
+ // once again with removing one cell
+ row.removeCell(cell2);
+
+ // now check again
+ validateRow(row);
+ }
+
+ private void validateRow(Row row) {
+ // trigger bug with CArray handling
+ ((XSSFRow)row).onDocumentWrite();
+
+ for(Cell cell : row) {
+ cell.toString();
+ }
+ }
+
}