2009-03-22 11:12:11 -04:00
|
|
|
/* ====================================================================
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
package org.apache.poi.ss.usermodel;
|
|
|
|
|
2016-03-20 20:14:21 -04:00
|
|
|
import static org.apache.poi.ss.usermodel.FormulaError.forInt;
|
2015-09-07 16:19:50 -04:00
|
|
|
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.assertTrue;
|
|
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
2015-01-05 09:03:37 -05:00
|
|
|
import java.io.IOException;
|
2009-04-11 13:53:39 -04:00
|
|
|
import java.util.Calendar;
|
|
|
|
|
2016-07-17 05:42:13 -04:00
|
|
|
import org.apache.poi.common.usermodel.HyperlinkType;
|
2015-01-05 09:03:37 -05:00
|
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
2009-04-11 13:53:39 -04:00
|
|
|
import org.apache.poi.ss.ITestDataProvider;
|
2015-04-03 11:55:21 -04:00
|
|
|
import org.apache.poi.ss.SpreadsheetVersion;
|
2015-09-07 16:19:50 -04:00
|
|
|
import org.apache.poi.util.LocaleUtil;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2009-03-22 11:12:11 -04:00
|
|
|
/**
|
2015-01-05 09:03:37 -05:00
|
|
|
* Common superclass for testing implementations of
|
2009-03-27 07:50:52 -04:00
|
|
|
* {@link org.apache.poi.ss.usermodel.Cell}
|
2009-03-22 11:12:11 -04:00
|
|
|
*/
|
2015-09-07 16:19:50 -04:00
|
|
|
public abstract class BaseTestCell {
|
2009-03-22 11:12:11 -04:00
|
|
|
|
2015-11-29 23:50:18 -05:00
|
|
|
protected final ITestDataProvider _testDataProvider;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param testDataProvider an object that provides test data in HSSF / XSSF specific way
|
|
|
|
*/
|
|
|
|
protected BaseTestCell(ITestDataProvider testDataProvider) {
|
|
|
|
_testDataProvider = testDataProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSetValues() throws Exception {
|
|
|
|
Workbook book = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sheet = book.createSheet("test");
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
|
|
|
|
CreationHelper factory = book.getCreationHelper();
|
|
|
|
Cell cell = row.createCell(0);
|
|
|
|
|
|
|
|
cell.setCellValue(1.2);
|
|
|
|
assertEquals(1.2, cell.getNumericCellValue(), 0.0001);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.NUMERIC, cell.getCellTypeEnum());
|
2016-07-04 05:45:46 -04:00
|
|
|
assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING,
|
|
|
|
CellType.FORMULA, CellType.ERROR);
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
cell.setCellValue(false);
|
|
|
|
assertEquals(false, cell.getBooleanCellValue());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.BOOLEAN, cell.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
cell.setCellValue(true);
|
|
|
|
assertEquals(true, cell.getBooleanCellValue());
|
2016-07-04 05:45:46 -04:00
|
|
|
assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.STRING,
|
|
|
|
CellType.FORMULA, CellType.ERROR);
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
cell.setCellValue(factory.createRichTextString("Foo"));
|
|
|
|
assertEquals("Foo", cell.getRichStringCellValue().getString());
|
|
|
|
assertEquals("Foo", cell.getStringCellValue());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.STRING, cell.getCellTypeEnum());
|
2016-07-04 05:45:46 -04:00
|
|
|
assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN,
|
|
|
|
CellType.FORMULA, CellType.ERROR);
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
cell.setCellValue("345");
|
|
|
|
assertEquals("345", cell.getRichStringCellValue().getString());
|
|
|
|
assertEquals("345", cell.getStringCellValue());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.STRING, cell.getCellTypeEnum());
|
2016-07-04 05:45:46 -04:00
|
|
|
assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN,
|
|
|
|
CellType.FORMULA, CellType.ERROR);
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
Calendar c = LocaleUtil.getLocaleCalendar();
|
|
|
|
c.setTimeInMillis(123456789);
|
|
|
|
cell.setCellValue(c.getTime());
|
|
|
|
assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.NUMERIC, cell.getCellTypeEnum());
|
2016-07-04 05:45:46 -04:00
|
|
|
assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING,
|
|
|
|
CellType.FORMULA, CellType.ERROR);
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
cell.setCellValue(c);
|
|
|
|
assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.NUMERIC, cell.getCellTypeEnum());
|
2016-07-04 05:45:46 -04:00
|
|
|
assertProhibitedValueAccess(cell, CellType.BOOLEAN, CellType.STRING,
|
|
|
|
CellType.FORMULA, CellType.ERROR);
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
cell.setCellErrorValue(FormulaError.NA.getCode());
|
|
|
|
assertEquals(FormulaError.NA.getCode(), cell.getErrorCellValue());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.ERROR, cell.getCellTypeEnum());
|
2016-07-04 05:45:46 -04:00
|
|
|
assertProhibitedValueAccess(cell, CellType.NUMERIC, CellType.BOOLEAN,
|
|
|
|
CellType.FORMULA, CellType.STRING);
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
book.close();
|
|
|
|
}
|
|
|
|
|
2016-07-04 05:45:46 -04:00
|
|
|
private static void assertProhibitedValueAccess(Cell cell, CellType ... types) {
|
|
|
|
for(CellType type : types){
|
2015-11-29 23:50:18 -05:00
|
|
|
try {
|
|
|
|
switch (type) {
|
2016-07-04 05:45:46 -04:00
|
|
|
case NUMERIC:
|
2015-11-29 23:50:18 -05:00
|
|
|
cell.getNumericCellValue();
|
|
|
|
break;
|
2016-07-04 05:45:46 -04:00
|
|
|
case STRING:
|
2015-11-29 23:50:18 -05:00
|
|
|
cell.getStringCellValue();
|
|
|
|
break;
|
2016-07-04 05:45:46 -04:00
|
|
|
case BOOLEAN:
|
2015-11-29 23:50:18 -05:00
|
|
|
cell.getBooleanCellValue();
|
|
|
|
break;
|
2016-07-04 05:45:46 -04:00
|
|
|
case FORMULA:
|
2015-11-29 23:50:18 -05:00
|
|
|
cell.getCellFormula();
|
|
|
|
break;
|
2016-07-04 05:45:46 -04:00
|
|
|
case ERROR:
|
2015-11-29 23:50:18 -05:00
|
|
|
cell.getErrorCellValue();
|
|
|
|
break;
|
2016-07-04 05:45:46 -04:00
|
|
|
default:
|
|
|
|
fail("Should get exception when reading cell type (" + type + ").");
|
2015-11-29 23:50:18 -05:00
|
|
|
}
|
2016-07-04 05:45:46 -04:00
|
|
|
|
2015-11-29 23:50:18 -05:00
|
|
|
} catch (IllegalStateException e){
|
|
|
|
// expected during successful test
|
|
|
|
assertTrue(e.getMessage().startsWith("Cannot get a"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that Boolean (BoolErrRecord) are supported properly.
|
|
|
|
* @see testErr
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void testBool() throws IOException {
|
|
|
|
|
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet s = wb1.createSheet("testSheet1");
|
|
|
|
Row r;
|
|
|
|
Cell c;
|
|
|
|
// B1
|
|
|
|
r = s.createRow(0);
|
|
|
|
c=r.createCell(1);
|
2015-11-30 00:00:24 -05:00
|
|
|
assertEquals(0, c.getRowIndex());
|
|
|
|
assertEquals(1, c.getColumnIndex());
|
2015-11-29 23:50:18 -05:00
|
|
|
c.setCellValue(true);
|
2015-11-30 00:21:01 -05:00
|
|
|
assertEquals("B1 value", true, c.getBooleanCellValue());
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
// C1
|
|
|
|
c=r.createCell(2);
|
2015-11-30 00:00:24 -05:00
|
|
|
assertEquals(0, c.getRowIndex());
|
|
|
|
assertEquals(2, c.getColumnIndex());
|
2015-11-29 23:50:18 -05:00
|
|
|
c.setCellValue(false);
|
2015-11-30 00:21:01 -05:00
|
|
|
assertEquals("C1 value", false, c.getBooleanCellValue());
|
2015-11-29 23:50:18 -05:00
|
|
|
|
2015-11-30 00:21:01 -05:00
|
|
|
// Make sure values are saved and re-read correctly.
|
2015-11-29 23:50:18 -05:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
|
|
|
|
s = wb2.getSheet("testSheet1");
|
|
|
|
r = s.getRow(0);
|
2015-11-30 00:00:24 -05:00
|
|
|
assertEquals("Row 1 should have 2 cells", 2, r.getPhysicalNumberOfCells());
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
c = r.getCell(1);
|
2015-11-30 00:00:24 -05:00
|
|
|
assertEquals(0, c.getRowIndex());
|
|
|
|
assertEquals(1, c.getColumnIndex());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.BOOLEAN, c.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("B1 value", true, c.getBooleanCellValue());
|
|
|
|
|
|
|
|
c = r.getCell(2);
|
2015-11-30 00:00:24 -05:00
|
|
|
assertEquals(0, c.getRowIndex());
|
|
|
|
assertEquals(2, c.getColumnIndex());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.BOOLEAN, c.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("C1 value", false, c.getBooleanCellValue());
|
|
|
|
|
|
|
|
wb2.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that Error types (BoolErrRecord) are supported properly.
|
|
|
|
* @see testBool
|
|
|
|
*/
|
2015-09-20 08:02:40 -04:00
|
|
|
@Test
|
2015-11-29 23:50:18 -05:00
|
|
|
public void testErr() throws IOException {
|
|
|
|
|
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet s = wb1.createSheet("testSheet1");
|
|
|
|
Row r;
|
|
|
|
Cell c;
|
|
|
|
|
|
|
|
// B1
|
2015-11-30 00:00:24 -05:00
|
|
|
r = s.createRow(0);
|
2015-11-29 23:50:18 -05:00
|
|
|
c=r.createCell(1);
|
2015-11-30 00:00:24 -05:00
|
|
|
assertEquals(0, c.getRowIndex());
|
|
|
|
assertEquals(1, c.getColumnIndex());
|
2015-11-29 23:50:18 -05:00
|
|
|
c.setCellErrorValue(FormulaError.NULL.getCode());
|
2015-11-30 00:21:01 -05:00
|
|
|
assertEquals("B1 value == #NULL!", FormulaError.NULL.getCode(), c.getErrorCellValue());
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
// C1
|
|
|
|
c=r.createCell(2);
|
2015-11-30 00:00:24 -05:00
|
|
|
assertEquals(0, c.getRowIndex());
|
|
|
|
assertEquals(2, c.getColumnIndex());
|
2015-11-29 23:50:18 -05:00
|
|
|
c.setCellErrorValue(FormulaError.DIV0.getCode());
|
2015-11-30 00:21:01 -05:00
|
|
|
assertEquals("C1 value == #DIV/0!", FormulaError.DIV0.getCode(), c.getErrorCellValue());
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
|
|
|
|
s = wb2.getSheet("testSheet1");
|
|
|
|
|
|
|
|
r = s.getRow(0);
|
2015-11-30 00:00:24 -05:00
|
|
|
assertEquals("Row 1 should have 2 cells", 2, r.getPhysicalNumberOfCells());
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
c = r.getCell(1);
|
2015-11-30 00:21:01 -05:00
|
|
|
assertEquals(0, c.getRowIndex());
|
|
|
|
assertEquals(1, c.getColumnIndex());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.ERROR, c.getCellTypeEnum());
|
2015-11-30 00:21:01 -05:00
|
|
|
assertEquals("B1 value == #NULL!", FormulaError.NULL.getCode(), c.getErrorCellValue());
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
c = r.getCell(2);
|
2015-11-30 00:21:01 -05:00
|
|
|
assertEquals(0, c.getRowIndex());
|
|
|
|
assertEquals(2, c.getColumnIndex());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.ERROR, c.getCellTypeEnum());
|
2015-11-30 00:21:01 -05:00
|
|
|
assertEquals("C1 value == #DIV/0!", FormulaError.DIV0.getCode(), c.getErrorCellValue());
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
wb2.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* test that Cell Styles being applied to formulas remain intact
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void testFormulaStyle() throws Exception {
|
|
|
|
|
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet s = wb1.createSheet("testSheet1");
|
|
|
|
Row r = null;
|
|
|
|
Cell c = null;
|
|
|
|
CellStyle cs = wb1.createCellStyle();
|
|
|
|
Font f = wb1.createFont();
|
|
|
|
f.setFontHeightInPoints((short) 20);
|
|
|
|
f.setColor(IndexedColors.RED.getIndex());
|
|
|
|
f.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
|
f.setFontName("Arial Unicode MS");
|
|
|
|
cs.setFillBackgroundColor((short)3);
|
|
|
|
cs.setFont(f);
|
2016-04-05 16:28:46 -04:00
|
|
|
cs.setBorderTop(BorderStyle.THIN);
|
|
|
|
cs.setBorderRight(BorderStyle.THIN);
|
|
|
|
cs.setBorderLeft(BorderStyle.THIN);
|
|
|
|
cs.setBorderBottom(BorderStyle.THIN);
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
r = s.createRow(0);
|
|
|
|
c=r.createCell(0);
|
|
|
|
c.setCellStyle(cs);
|
|
|
|
c.setCellFormula("2*3");
|
|
|
|
|
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
s = wb2.getSheetAt(0);
|
|
|
|
r = s.getRow(0);
|
|
|
|
c = r.getCell(0);
|
|
|
|
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals("Formula Cell at 0,0", CellType.FORMULA, c.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
cs = c.getCellStyle();
|
|
|
|
|
|
|
|
assertNotNull("Formula Cell Style", cs);
|
2016-04-05 16:28:46 -04:00
|
|
|
assertEquals("Font Index Matches", f.getIndex(), cs.getFontIndex());
|
|
|
|
assertEquals("Top Border", BorderStyle.THIN, cs.getBorderTop());
|
|
|
|
assertEquals("Left Border", BorderStyle.THIN, cs.getBorderLeft());
|
|
|
|
assertEquals("Right Border", BorderStyle.THIN, cs.getBorderRight());
|
|
|
|
assertEquals("Bottom Border", BorderStyle.THIN, cs.getBorderBottom());
|
2015-11-29 23:50:18 -05:00
|
|
|
wb2.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**tests the toString() method of HSSFCell*/
|
|
|
|
@Test
|
|
|
|
public void testToString() throws Exception {
|
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Row r = wb1.createSheet("Sheet1").createRow(0);
|
|
|
|
CreationHelper factory = wb1.getCreationHelper();
|
|
|
|
|
2016-07-20 22:17:37 -04:00
|
|
|
r.createCell(0).setCellValue(false);
|
|
|
|
r.createCell(1).setCellValue(true);
|
|
|
|
r.createCell(2).setCellValue(1.5);
|
|
|
|
r.createCell(3).setCellValue(factory.createRichTextString("Astring"));
|
|
|
|
r.createCell(4).setCellErrorValue(FormulaError.DIV0.getCode());
|
|
|
|
r.createCell(5).setCellFormula("A1+B1");
|
|
|
|
r.createCell(6); // blank
|
2016-04-06 02:01:26 -04:00
|
|
|
|
|
|
|
// create date-formatted cell
|
|
|
|
Calendar c = LocaleUtil.getLocaleCalendar();
|
|
|
|
c.set(2010, 01, 02, 00, 00, 00);
|
2016-07-20 22:17:37 -04:00
|
|
|
r.createCell(7).setCellValue(c);
|
2016-04-06 02:01:26 -04:00
|
|
|
CellStyle dateStyle = wb1.createCellStyle();
|
|
|
|
short formatId = wb1.getCreationHelper().createDataFormat().getFormat("m/d/yy h:mm"); // any date format will do
|
|
|
|
dateStyle.setDataFormat(formatId);
|
2016-07-20 22:17:37 -04:00
|
|
|
r.getCell(7).setCellStyle(dateStyle);
|
|
|
|
|
|
|
|
assertEquals("Boolean", "FALSE", r.getCell(0).toString());
|
|
|
|
assertEquals("Boolean", "TRUE", r.getCell(1).toString());
|
|
|
|
assertEquals("Numeric", "1.5", r.getCell(2).toString());
|
|
|
|
assertEquals("String", "Astring", r.getCell(3).toString());
|
|
|
|
assertEquals("Error", "#DIV/0!", r.getCell(4).toString());
|
|
|
|
assertEquals("Formula", "A1+B1", r.getCell(5).toString());
|
|
|
|
assertEquals("Blank", "", r.getCell(6).toString());
|
2016-04-06 02:01:26 -04:00
|
|
|
// toString on a date-formatted cell displays dates as dd-MMM-yyyy, which has locale problems with the month
|
2016-07-20 22:17:37 -04:00
|
|
|
String dateCell1 = r.getCell(7).toString();
|
2016-04-06 02:01:26 -04:00
|
|
|
assertTrue("Date (Day)", dateCell1.startsWith("02-"));
|
|
|
|
assertTrue("Date (Year)", dateCell1.endsWith("-2010"));
|
|
|
|
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
//Write out the file, read it in, and then check cell values
|
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
|
|
|
|
r = wb2.getSheetAt(0).getRow(0);
|
2016-07-20 22:17:37 -04:00
|
|
|
assertEquals("Boolean", "FALSE", r.getCell(0).toString());
|
|
|
|
assertEquals("Boolean", "TRUE", r.getCell(1).toString());
|
|
|
|
assertEquals("Numeric", "1.5", r.getCell(2).toString());
|
|
|
|
assertEquals("String", "Astring", r.getCell(3).toString());
|
|
|
|
assertEquals("Error", "#DIV/0!", r.getCell(4).toString());
|
|
|
|
assertEquals("Formula", "A1+B1", r.getCell(5).toString());
|
|
|
|
assertEquals("Blank", "", r.getCell(6).toString());
|
|
|
|
String dateCell2 = r.getCell(7).toString();
|
2016-04-06 02:01:26 -04:00
|
|
|
assertEquals("Date", dateCell1, dateCell2);
|
2015-11-29 23:50:18 -05:00
|
|
|
wb2.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that setting cached formula result keeps the cell type
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void testSetFormulaValue() throws Exception {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
Sheet s = wb.createSheet();
|
|
|
|
Row r = s.createRow(0);
|
|
|
|
|
|
|
|
Cell c1 = r.createCell(0);
|
|
|
|
c1.setCellFormula("NA()");
|
|
|
|
assertEquals(0.0, c1.getNumericCellValue(), 0.0);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.NUMERIC, c1.getCachedFormulaResultTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
c1.setCellValue(10);
|
|
|
|
assertEquals(10.0, c1.getNumericCellValue(), 0.0);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.FORMULA, c1.getCellTypeEnum());
|
|
|
|
assertEquals(CellType.NUMERIC, c1.getCachedFormulaResultTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
Cell c2 = r.createCell(1);
|
|
|
|
c2.setCellFormula("NA()");
|
|
|
|
assertEquals(0.0, c2.getNumericCellValue(), 0.0);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.NUMERIC, c2.getCachedFormulaResultTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
c2.setCellValue("I changed!");
|
|
|
|
assertEquals("I changed!", c2.getStringCellValue());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.FORMULA, c2.getCellTypeEnum());
|
|
|
|
assertEquals(CellType.STRING, c2.getCachedFormulaResultTypeEnum());
|
2009-04-20 12:50:05 -04:00
|
|
|
|
|
|
|
//calglin Cell.setCellFormula(null) for a non-formula cell
|
|
|
|
Cell c3 = r.createCell(2);
|
|
|
|
c3.setCellFormula(null);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.BLANK, c3.getCellTypeEnum());
|
2015-09-20 08:02:40 -04:00
|
|
|
wb.close();
|
2009-04-20 12:50:05 -04:00
|
|
|
|
|
|
|
}
|
2015-12-22 17:35:59 -05:00
|
|
|
|
2016-03-20 20:14:21 -04:00
|
|
|
private Cell createACell(Workbook wb) {
|
2015-12-22 17:35:59 -05:00
|
|
|
return wb.createSheet("Sheet1").createRow(0).createCell(0);
|
2015-11-29 23:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* bug 58452: Copy cell formulas containing unregistered function names
|
|
|
|
* Make sure that formulas with unknown/unregistered UDFs can be written to and read back from a file.
|
|
|
|
*
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void testFormulaWithUnknownUDF() throws IOException {
|
|
|
|
final Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
final FormulaEvaluator evaluator1 = wb1.getCreationHelper().createFormulaEvaluator();
|
|
|
|
try {
|
|
|
|
final Cell cell1 = wb1.createSheet().createRow(0).createCell(0);
|
|
|
|
final String formula = "myFunc(\"arg\")";
|
|
|
|
cell1.setCellFormula(formula);
|
|
|
|
confirmFormulaWithUnknownUDF(formula, cell1, evaluator1);
|
|
|
|
|
|
|
|
final Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
final FormulaEvaluator evaluator2 = wb2.getCreationHelper().createFormulaEvaluator();
|
|
|
|
try {
|
|
|
|
final Cell cell2 = wb2.getSheetAt(0).getRow(0).getCell(0);
|
|
|
|
confirmFormulaWithUnknownUDF(formula, cell2, evaluator2);
|
|
|
|
} finally {
|
|
|
|
wb2.close();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
wb1.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void confirmFormulaWithUnknownUDF(String expectedFormula, Cell cell, FormulaEvaluator evaluator) {
|
|
|
|
assertEquals(expectedFormula, cell.getCellFormula());
|
|
|
|
try {
|
|
|
|
evaluator.evaluate(cell);
|
|
|
|
fail("Expected NotImplementedFunctionException/NotImplementedException");
|
|
|
|
} catch (final org.apache.poi.ss.formula.eval.NotImplementedException e) {
|
|
|
|
// expected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-03-20 20:14:21 -04:00
|
|
|
public void testChangeTypeStringToBool() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
|
|
|
|
Cell cell = createACell(wb);
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
cell.setCellValue("TRUE");
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.STRING, cell.getCellTypeEnum());
|
2016-03-20 20:14:21 -04:00
|
|
|
// test conversion of cell from text to boolean
|
2016-07-04 05:45:46 -04:00
|
|
|
cell.setCellType(CellType.BOOLEAN);
|
2015-11-29 23:50:18 -05:00
|
|
|
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.BOOLEAN, cell.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals(true, cell.getBooleanCellValue());
|
2016-07-04 05:45:46 -04:00
|
|
|
cell.setCellType(CellType.STRING);
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("TRUE", cell.getRichStringCellValue().getString());
|
|
|
|
|
|
|
|
// 'false' text to bool and back
|
|
|
|
cell.setCellValue("FALSE");
|
2016-07-04 05:45:46 -04:00
|
|
|
cell.setCellType(CellType.BOOLEAN);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.BOOLEAN, cell.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals(false, cell.getBooleanCellValue());
|
2016-07-04 05:45:46 -04:00
|
|
|
cell.setCellType(CellType.STRING);
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("FALSE", cell.getRichStringCellValue().getString());
|
2016-03-20 20:14:21 -04:00
|
|
|
|
|
|
|
wb.close();
|
2015-11-29 23:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-03-20 20:14:21 -04:00
|
|
|
public void testChangeTypeBoolToString() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
|
|
|
|
Cell cell = createACell(wb);
|
2015-11-29 23:50:18 -05:00
|
|
|
|
|
|
|
cell.setCellValue(true);
|
2016-03-20 20:14:21 -04:00
|
|
|
// test conversion of cell from boolean to text
|
2016-07-04 05:45:46 -04:00
|
|
|
cell.setCellType(CellType.STRING);
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("TRUE", cell.getRichStringCellValue().getString());
|
2016-03-20 20:14:21 -04:00
|
|
|
|
|
|
|
wb.close();
|
2015-11-29 23:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-03-20 20:14:21 -04:00
|
|
|
public void testChangeTypeErrorToNumber() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
|
|
|
|
Cell cell = createACell(wb);
|
|
|
|
cell.setCellErrorValue(FormulaError.NAME.getCode());
|
2015-11-29 23:50:18 -05:00
|
|
|
try {
|
|
|
|
cell.setCellValue(2.5);
|
|
|
|
} catch (ClassCastException e) {
|
2016-03-20 20:14:21 -04:00
|
|
|
fail("Identified bug 46479b");
|
2015-11-29 23:50:18 -05:00
|
|
|
}
|
|
|
|
assertEquals(2.5, cell.getNumericCellValue(), 0.0);
|
2016-03-20 20:14:21 -04:00
|
|
|
|
|
|
|
wb.close();
|
2015-11-29 23:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2016-03-20 20:14:21 -04:00
|
|
|
public void testChangeTypeErrorToBoolean() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
2015-11-29 23:50:18 -05:00
|
|
|
|
2016-03-20 20:14:21 -04:00
|
|
|
Cell cell = createACell(wb);
|
|
|
|
cell.setCellErrorValue(FormulaError.NAME.getCode());
|
|
|
|
cell.setCellValue(true);
|
|
|
|
// Identify bug 46479c
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals(true, cell.getBooleanCellValue());
|
2016-03-20 20:14:21 -04:00
|
|
|
|
|
|
|
wb.close();
|
2015-11-29 23:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for a bug observed around svn r886733 when using
|
|
|
|
* {@link FormulaEvaluator#evaluateInCell(Cell)} with a
|
|
|
|
* string result type.
|
|
|
|
*/
|
|
|
|
@Test
|
2016-03-20 20:14:21 -04:00
|
|
|
public void testConvertStringFormulaCell() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
|
|
|
|
Cell cellA1 = createACell(wb);
|
2015-11-29 23:50:18 -05:00
|
|
|
cellA1.setCellFormula("\"abc\"");
|
|
|
|
|
|
|
|
// default cached formula result is numeric zero
|
|
|
|
assertEquals(0.0, cellA1.getNumericCellValue(), 0.0);
|
|
|
|
|
|
|
|
FormulaEvaluator fe = cellA1.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
|
|
|
|
|
2016-07-04 08:21:42 -04:00
|
|
|
fe.evaluateFormulaCellEnum(cellA1);
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("abc", cellA1.getStringCellValue());
|
|
|
|
|
|
|
|
fe.evaluateInCell(cellA1);
|
2016-03-20 20:14:21 -04:00
|
|
|
assertFalse("Identified bug with writing back formula result of type string", cellA1.getStringCellValue().equals(""));
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("abc", cellA1.getStringCellValue());
|
2016-03-20 20:14:21 -04:00
|
|
|
|
|
|
|
wb.close();
|
2015-11-29 23:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* similar to {@link #testConvertStringFormulaCell()} but checks at a
|
|
|
|
* lower level that {#link {@link Cell#setCellType(int)} works properly
|
|
|
|
*/
|
|
|
|
@Test
|
2016-03-20 20:14:21 -04:00
|
|
|
public void testSetTypeStringOnFormulaCell() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
|
|
|
|
Cell cellA1 = createACell(wb);
|
2015-11-29 23:50:18 -05:00
|
|
|
FormulaEvaluator fe = cellA1.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
|
|
|
|
|
|
|
|
cellA1.setCellFormula("\"DEF\"");
|
|
|
|
fe.clearAllCachedResultValues();
|
2016-07-04 08:21:42 -04:00
|
|
|
fe.evaluateFormulaCellEnum(cellA1);
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("DEF", cellA1.getStringCellValue());
|
2016-07-04 05:45:46 -04:00
|
|
|
cellA1.setCellType(CellType.STRING);
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("DEF", cellA1.getStringCellValue());
|
|
|
|
|
|
|
|
cellA1.setCellFormula("25.061");
|
|
|
|
fe.clearAllCachedResultValues();
|
2016-07-04 08:21:42 -04:00
|
|
|
fe.evaluateFormulaCellEnum(cellA1);
|
2015-11-29 23:50:18 -05:00
|
|
|
confirmCannotReadString(cellA1);
|
|
|
|
assertEquals(25.061, cellA1.getNumericCellValue(), 0.0);
|
2016-07-04 05:45:46 -04:00
|
|
|
cellA1.setCellType(CellType.STRING);
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("25.061", cellA1.getStringCellValue());
|
|
|
|
|
|
|
|
cellA1.setCellFormula("TRUE");
|
|
|
|
fe.clearAllCachedResultValues();
|
2016-07-04 08:21:42 -04:00
|
|
|
fe.evaluateFormulaCellEnum(cellA1);
|
2015-11-29 23:50:18 -05:00
|
|
|
confirmCannotReadString(cellA1);
|
|
|
|
assertEquals(true, cellA1.getBooleanCellValue());
|
2016-07-04 05:45:46 -04:00
|
|
|
cellA1.setCellType(CellType.STRING);
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("TRUE", cellA1.getStringCellValue());
|
|
|
|
|
|
|
|
cellA1.setCellFormula("#NAME?");
|
|
|
|
fe.clearAllCachedResultValues();
|
2016-07-04 08:21:42 -04:00
|
|
|
fe.evaluateFormulaCellEnum(cellA1);
|
2015-11-29 23:50:18 -05:00
|
|
|
confirmCannotReadString(cellA1);
|
2016-03-20 20:14:21 -04:00
|
|
|
assertEquals(FormulaError.NAME, forInt(cellA1.getErrorCellValue()));
|
2016-07-04 05:45:46 -04:00
|
|
|
cellA1.setCellType(CellType.STRING);
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("#NAME?", cellA1.getStringCellValue());
|
2016-03-20 20:14:21 -04:00
|
|
|
|
|
|
|
wb.close();
|
2015-11-29 23:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void confirmCannotReadString(Cell cell) {
|
2016-07-04 05:45:46 -04:00
|
|
|
assertProhibitedValueAccess(cell, CellType.STRING);
|
2015-11-29 23:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for bug in convertCellValueToBoolean to make sure that formula results get converted
|
|
|
|
*/
|
|
|
|
@Test
|
2016-03-20 20:14:21 -04:00
|
|
|
public void testChangeTypeFormulaToBoolean() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
|
|
|
|
Cell cell = createACell(wb);
|
2015-11-29 23:50:18 -05:00
|
|
|
cell.setCellFormula("1=1");
|
|
|
|
cell.setCellValue(true);
|
2016-07-04 05:45:46 -04:00
|
|
|
cell.setCellType(CellType.BOOLEAN);
|
2016-03-20 20:14:21 -04:00
|
|
|
assertTrue("Identified bug 46479d", cell.getBooleanCellValue());
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals(true, cell.getBooleanCellValue());
|
2016-03-20 20:14:21 -04:00
|
|
|
|
|
|
|
wb.close();
|
2015-11-29 23:50:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bug 40296: HSSFCell.setCellFormula throws
|
|
|
|
* ClassCastException if cell is created using HSSFRow.createCell(short column, int type)
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void test40296() throws Exception {
|
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet workSheet = wb1.createSheet("Sheet1");
|
|
|
|
Cell cell;
|
|
|
|
Row row = workSheet.createRow(0);
|
|
|
|
|
2016-07-04 05:45:46 -04:00
|
|
|
cell = row.createCell(0, CellType.NUMERIC);
|
2015-11-29 23:50:18 -05:00
|
|
|
cell.setCellValue(1.0);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.NUMERIC, cell.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals(1.0, cell.getNumericCellValue(), 0.0);
|
|
|
|
|
2016-07-04 05:45:46 -04:00
|
|
|
cell = row.createCell(1, CellType.NUMERIC);
|
2015-11-29 23:50:18 -05:00
|
|
|
cell.setCellValue(2.0);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.NUMERIC, cell.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals(2.0, cell.getNumericCellValue(), 0.0);
|
|
|
|
|
2016-07-04 05:45:46 -04:00
|
|
|
cell = row.createCell(2, CellType.FORMULA);
|
2015-11-29 23:50:18 -05:00
|
|
|
cell.setCellFormula("SUM(A1:B1)");
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.FORMULA, cell.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("SUM(A1:B1)", cell.getCellFormula());
|
|
|
|
|
|
|
|
//serialize and check again
|
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
row = wb2.getSheetAt(0).getRow(0);
|
|
|
|
cell = row.getCell(0);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.NUMERIC, cell.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals(1.0, cell.getNumericCellValue(), 0.0);
|
|
|
|
|
|
|
|
cell = row.getCell(1);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.NUMERIC, cell.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals(2.0, cell.getNumericCellValue(), 0.0);
|
|
|
|
|
|
|
|
cell = row.getCell(2);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.FORMULA, cell.getCellTypeEnum());
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals("SUM(A1:B1)", cell.getCellFormula());
|
|
|
|
wb2.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSetStringInFormulaCell_bug44606() throws Exception {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
|
|
|
|
cell.setCellFormula("B1&C1");
|
|
|
|
cell.setCellValue(wb.getCreationHelper().createRichTextString("hello"));
|
|
|
|
wb.close();
|
|
|
|
}
|
2009-04-17 14:02:01 -04:00
|
|
|
|
|
|
|
/**
|
2016-07-04 05:45:46 -04:00
|
|
|
* Make sure that cell.setCellType(CellType.BLANK) preserves the cell style
|
2009-04-17 14:02:01 -04:00
|
|
|
*/
|
2015-11-29 23:50:18 -05:00
|
|
|
@Test
|
|
|
|
public void testSetBlank_bug47028() throws Exception {
|
2009-04-17 14:02:01 -04:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
CellStyle style = wb.createCellStyle();
|
|
|
|
Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
|
|
|
|
cell.setCellStyle(style);
|
|
|
|
int i1 = cell.getCellStyle().getIndex();
|
2016-07-04 05:45:46 -04:00
|
|
|
cell.setCellType(CellType.BLANK);
|
2009-04-17 14:02:01 -04:00
|
|
|
int i2 = cell.getCellStyle().getIndex();
|
|
|
|
assertEquals(i1, i2);
|
2015-09-20 08:02:40 -04:00
|
|
|
wb.close();
|
2009-04-17 14:02:01 -04:00
|
|
|
}
|
2010-09-04 08:33:09 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Excel's implementation of floating number arithmetic does not fully adhere to IEEE 754:
|
|
|
|
*
|
|
|
|
* From http://support.microsoft.com/kb/78113:
|
|
|
|
*
|
|
|
|
* <ul>
|
|
|
|
* <li> Positive/Negative Infinities:
|
|
|
|
* Infinities occur when you divide by 0. Excel does not support infinities, rather,
|
|
|
|
* it gives a #DIV/0! error in these cases.
|
|
|
|
* </li>
|
|
|
|
* <li>
|
|
|
|
* Not-a-Number (NaN):
|
|
|
|
* NaN is used to represent invalid operations (such as infinity/infinity,
|
|
|
|
* infinity-infinity, or the square root of -1). NaNs allow a program to
|
|
|
|
* continue past an invalid operation. Excel instead immediately generates
|
|
|
|
* an error such as #NUM! or #DIV/0!.
|
|
|
|
* </li>
|
|
|
|
* </ul>
|
|
|
|
*/
|
2015-11-29 23:50:18 -05:00
|
|
|
@Test
|
|
|
|
public void testNanAndInfinity() throws Exception {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet workSheet = wb1.createSheet("Sheet1");
|
2010-09-04 08:33:09 -04:00
|
|
|
Row row = workSheet.createRow(0);
|
|
|
|
|
|
|
|
Cell cell0 = row.createCell(0);
|
|
|
|
cell0.setCellValue(Double.NaN);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals("Double.NaN should change cell type to CellType#ERROR", CellType.ERROR, cell0.getCellTypeEnum());
|
2016-03-20 20:14:21 -04:00
|
|
|
assertEquals("Double.NaN should change cell value to #NUM!", FormulaError.NUM, forInt(cell0.getErrorCellValue()));
|
2010-09-04 08:33:09 -04:00
|
|
|
|
|
|
|
Cell cell1 = row.createCell(1);
|
|
|
|
cell1.setCellValue(Double.POSITIVE_INFINITY);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals("Double.POSITIVE_INFINITY should change cell type to CellType#ERROR", CellType.ERROR, cell1.getCellTypeEnum());
|
2016-03-20 20:14:21 -04:00
|
|
|
assertEquals("Double.POSITIVE_INFINITY should change cell value to #DIV/0!", FormulaError.DIV0, forInt(cell1.getErrorCellValue()));
|
2010-09-04 08:33:09 -04:00
|
|
|
|
|
|
|
Cell cell2 = row.createCell(2);
|
|
|
|
cell2.setCellValue(Double.NEGATIVE_INFINITY);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals("Double.NEGATIVE_INFINITY should change cell type to CellType#ERROR", CellType.ERROR, cell2.getCellTypeEnum());
|
2016-03-20 20:14:21 -04:00
|
|
|
assertEquals("Double.NEGATIVE_INFINITY should change cell value to #DIV/0!", FormulaError.DIV0, forInt(cell2.getErrorCellValue()));
|
2010-09-04 08:33:09 -04:00
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
row = wb2.getSheetAt(0).getRow(0);
|
2010-09-04 08:33:09 -04:00
|
|
|
|
|
|
|
cell0 = row.getCell(0);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.ERROR, cell0.getCellTypeEnum());
|
2016-03-20 20:14:21 -04:00
|
|
|
assertEquals(FormulaError.NUM, forInt(cell0.getErrorCellValue()));
|
2010-09-04 08:33:09 -04:00
|
|
|
|
|
|
|
cell1 = row.getCell(1);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.ERROR, cell1.getCellTypeEnum());
|
2016-03-20 20:14:21 -04:00
|
|
|
assertEquals(FormulaError.DIV0, forInt(cell1.getErrorCellValue()));
|
2010-09-04 08:33:09 -04:00
|
|
|
|
|
|
|
cell2 = row.getCell(2);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.ERROR, cell2.getCellTypeEnum());
|
2016-03-20 20:14:21 -04:00
|
|
|
assertEquals(FormulaError.DIV0, forInt(cell2.getErrorCellValue()));
|
2015-09-20 08:02:40 -04:00
|
|
|
wb2.close();
|
2010-09-04 08:33:09 -04:00
|
|
|
}
|
2011-06-24 09:06:04 -04:00
|
|
|
|
2015-11-29 23:50:18 -05:00
|
|
|
@Test
|
|
|
|
public void testDefaultStyleProperties() throws Exception {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
2011-06-24 09:06:04 -04:00
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Cell cell = wb1.createSheet("Sheet1").createRow(0).createCell(0);
|
2011-06-24 09:06:04 -04:00
|
|
|
CellStyle style = cell.getCellStyle();
|
|
|
|
|
|
|
|
assertTrue(style.getLocked());
|
|
|
|
assertFalse(style.getHidden());
|
|
|
|
assertEquals(0, style.getIndention());
|
|
|
|
assertEquals(0, style.getFontIndex());
|
|
|
|
assertEquals(0, style.getAlignment());
|
|
|
|
assertEquals(0, style.getDataFormat());
|
|
|
|
assertEquals(false, style.getWrapText());
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
CellStyle style2 = wb1.createCellStyle();
|
2011-06-24 09:06:04 -04:00
|
|
|
assertTrue(style2.getLocked());
|
|
|
|
assertFalse(style2.getHidden());
|
|
|
|
style2.setLocked(false);
|
|
|
|
style2.setHidden(true);
|
|
|
|
assertFalse(style2.getLocked());
|
|
|
|
assertTrue(style2.getHidden());
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
cell = wb2.getSheetAt(0).getRow(0).getCell(0);
|
2011-06-24 09:06:04 -04:00
|
|
|
style = cell.getCellStyle();
|
|
|
|
assertFalse(style2.getLocked());
|
|
|
|
assertTrue(style2.getHidden());
|
|
|
|
|
|
|
|
style2.setLocked(true);
|
|
|
|
style2.setHidden(false);
|
|
|
|
assertTrue(style2.getLocked());
|
|
|
|
assertFalse(style2.getHidden());
|
2015-09-20 08:02:40 -04:00
|
|
|
wb2.close();
|
2011-06-24 09:06:04 -04:00
|
|
|
}
|
|
|
|
|
2015-11-29 23:50:18 -05:00
|
|
|
@Test
|
|
|
|
public void testBug55658SetNumericValue() throws Exception {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sh = wb1.createSheet();
|
2015-01-05 09:03:37 -05:00
|
|
|
Row row = sh.createRow(0);
|
|
|
|
Cell cell = row.createCell(0);
|
|
|
|
cell.setCellValue(Integer.valueOf(23));
|
|
|
|
|
|
|
|
cell.setCellValue("some");
|
|
|
|
|
|
|
|
cell = row.createCell(1);
|
|
|
|
cell.setCellValue(Integer.valueOf(23));
|
|
|
|
|
|
|
|
cell.setCellValue("24");
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
2015-01-05 09:03:37 -05:00
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
assertEquals("some", wb2.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
|
|
|
|
assertEquals("24", wb2.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
|
|
|
|
wb2.close();
|
2015-01-05 09:03:37 -05:00
|
|
|
}
|
|
|
|
|
2015-11-29 23:50:18 -05:00
|
|
|
@Test
|
|
|
|
public void testRemoveHyperlink() throws Exception {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sh = wb1.createSheet("test");
|
2015-01-05 09:03:37 -05:00
|
|
|
Row row = sh.createRow(0);
|
2015-09-20 08:02:40 -04:00
|
|
|
CreationHelper helper = wb1.getCreationHelper();
|
2015-01-05 09:03:37 -05:00
|
|
|
|
|
|
|
Cell cell1 = row.createCell(1);
|
2016-07-17 05:42:13 -04:00
|
|
|
Hyperlink link1 = helper.createHyperlink(HyperlinkType.URL);
|
2015-01-05 09:03:37 -05:00
|
|
|
cell1.setHyperlink(link1);
|
|
|
|
assertNotNull(cell1.getHyperlink());
|
|
|
|
cell1.removeHyperlink();
|
|
|
|
assertNull(cell1.getHyperlink());
|
|
|
|
|
|
|
|
Cell cell2 = row.createCell(0);
|
2016-07-17 05:42:13 -04:00
|
|
|
Hyperlink link2 = helper.createHyperlink(HyperlinkType.URL);
|
2015-01-05 09:03:37 -05:00
|
|
|
cell2.setHyperlink(link2);
|
|
|
|
assertNotNull(cell2.getHyperlink());
|
|
|
|
cell2.setHyperlink(null);
|
|
|
|
assertNull(cell2.getHyperlink());
|
|
|
|
|
|
|
|
Cell cell3 = row.createCell(2);
|
2016-07-17 05:42:13 -04:00
|
|
|
Hyperlink link3 = helper.createHyperlink(HyperlinkType.URL);
|
2015-01-05 09:03:37 -05:00
|
|
|
link3.setAddress("http://poi.apache.org/");
|
|
|
|
cell3.setHyperlink(link3);
|
|
|
|
assertNotNull(cell3.getHyperlink());
|
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
wb1.close();
|
|
|
|
assertNotNull(wb2);
|
2015-01-05 09:03:37 -05:00
|
|
|
|
2015-09-20 08:02:40 -04:00
|
|
|
cell1 = wb2.getSheet("test").getRow(0).getCell(1);
|
2015-01-05 09:03:37 -05:00
|
|
|
assertNull(cell1.getHyperlink());
|
2015-09-20 08:02:40 -04:00
|
|
|
cell2 = wb2.getSheet("test").getRow(0).getCell(0);
|
2015-01-05 09:03:37 -05:00
|
|
|
assertNull(cell2.getHyperlink());
|
2015-09-20 08:02:40 -04:00
|
|
|
cell3 = wb2.getSheet("test").getRow(0).getCell(2);
|
2015-01-05 09:03:37 -05:00
|
|
|
assertNotNull(cell3.getHyperlink());
|
2015-09-20 08:02:40 -04:00
|
|
|
wb2.close();
|
2015-01-05 09:03:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cell with the formula that returns error must return error code(There was
|
|
|
|
* an problem that cell could not return error value form formula cell).
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2015-11-29 23:50:18 -05:00
|
|
|
@Test
|
|
|
|
public void testGetErrorCellValueFromFormulaCell() throws IOException {
|
2015-01-05 09:03:37 -05:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
try {
|
|
|
|
Sheet sheet = wb.createSheet();
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
Cell cell = row.createCell(0);
|
|
|
|
cell.setCellFormula("SQRT(-1)");
|
2016-07-04 08:21:42 -04:00
|
|
|
wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCellEnum(cell);
|
2015-01-05 09:03:37 -05:00
|
|
|
assertEquals(36, cell.getErrorCellValue());
|
|
|
|
} finally {
|
|
|
|
wb.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-29 23:50:18 -05:00
|
|
|
@Test
|
|
|
|
public void testSetRemoveStyle() throws Exception {
|
2015-01-05 09:03:37 -05:00
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sheet = wb.createSheet();
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
Cell cell = row.createCell(0);
|
|
|
|
|
|
|
|
// different default style indexes for HSSF and XSSF/SXSSF
|
|
|
|
CellStyle defaultStyle = wb.getCellStyleAt(wb instanceof HSSFWorkbook ? (short)15 : (short)0);
|
|
|
|
|
|
|
|
// Starts out with the default style
|
|
|
|
assertEquals(defaultStyle, cell.getCellStyle());
|
|
|
|
|
|
|
|
// Create some styles, no change
|
|
|
|
CellStyle style1 = wb.createCellStyle();
|
|
|
|
CellStyle style2 = wb.createCellStyle();
|
|
|
|
style1.setDataFormat((short)2);
|
|
|
|
style2.setDataFormat((short)3);
|
|
|
|
|
|
|
|
assertEquals(defaultStyle, cell.getCellStyle());
|
|
|
|
|
|
|
|
// Apply one, changes
|
|
|
|
cell.setCellStyle(style1);
|
|
|
|
assertEquals(style1, cell.getCellStyle());
|
|
|
|
|
|
|
|
// Apply the other, changes
|
|
|
|
cell.setCellStyle(style2);
|
|
|
|
assertEquals(style2, cell.getCellStyle());
|
|
|
|
|
|
|
|
// Remove, goes back to default
|
|
|
|
cell.setCellStyle(null);
|
|
|
|
assertEquals(defaultStyle, cell.getCellStyle());
|
|
|
|
|
|
|
|
// Add back, returns
|
|
|
|
cell.setCellStyle(style2);
|
|
|
|
assertEquals(style2, cell.getCellStyle());
|
|
|
|
|
|
|
|
wb.close();
|
|
|
|
}
|
2015-04-03 11:55:21 -04:00
|
|
|
|
2015-11-29 23:50:18 -05:00
|
|
|
@Test
|
|
|
|
public void test57008() throws IOException {
|
2015-09-20 08:02:40 -04:00
|
|
|
Workbook wb1 = _testDataProvider.createWorkbook();
|
2015-11-29 23:50:18 -05:00
|
|
|
Sheet sheet = wb1.createSheet();
|
|
|
|
|
|
|
|
Row row0 = sheet.createRow(0);
|
|
|
|
Cell cell0 = row0.createCell(0);
|
|
|
|
cell0.setCellValue("row 0, cell 0 _x0046_ without changes");
|
|
|
|
|
|
|
|
Cell cell1 = row0.createCell(1);
|
|
|
|
cell1.setCellValue("row 0, cell 1 _x005fx0046_ with changes");
|
|
|
|
|
|
|
|
Cell cell2 = row0.createCell(2);
|
|
|
|
cell2.setCellValue("hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*");
|
|
|
|
|
|
|
|
checkUnicodeValues(wb1);
|
|
|
|
|
|
|
|
Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
|
|
|
|
checkUnicodeValues(wb2);
|
|
|
|
wb2.close();
|
|
|
|
wb1.close();
|
|
|
|
}
|
2015-04-03 11:55:21 -04:00
|
|
|
|
2015-10-28 13:42:56 -04:00
|
|
|
/**
|
|
|
|
* Setting a cell value of a null RichTextString should set
|
|
|
|
* the cell to Blank, test case for 58558
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void testSetCellValueNullRichTextString() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sheet = wb.createSheet();
|
|
|
|
Cell cell = sheet.createRow(0).createCell(0);
|
|
|
|
|
|
|
|
RichTextString nullStr = null;
|
|
|
|
cell.setCellValue(nullStr);
|
|
|
|
assertEquals("", cell.getStringCellValue());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.BLANK, cell.getCellTypeEnum());
|
2015-10-28 13:42:56 -04:00
|
|
|
|
|
|
|
cell = sheet.createRow(0).createCell(1);
|
|
|
|
cell.setCellValue(1.2d);
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.NUMERIC, cell.getCellTypeEnum());
|
2015-10-28 13:42:56 -04:00
|
|
|
cell.setCellValue(nullStr);
|
|
|
|
assertEquals("", cell.getStringCellValue());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.BLANK, cell.getCellTypeEnum());
|
2015-10-28 13:42:56 -04:00
|
|
|
|
|
|
|
cell = sheet.createRow(0).createCell(1);
|
|
|
|
cell.setCellValue(wb.getCreationHelper().createRichTextString("Test"));
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.STRING, cell.getCellTypeEnum());
|
2015-10-28 13:42:56 -04:00
|
|
|
cell.setCellValue(nullStr);
|
|
|
|
assertEquals("", cell.getStringCellValue());
|
2016-07-04 07:54:20 -04:00
|
|
|
assertEquals(CellType.BLANK, cell.getCellTypeEnum());
|
2015-10-28 13:42:56 -04:00
|
|
|
|
|
|
|
wb.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkUnicodeValues(Workbook wb) {
|
2015-11-29 23:50:18 -05:00
|
|
|
assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 0 _x0046_ without changes" : "row 0, cell 0 F without changes"),
|
|
|
|
wb.getSheetAt(0).getRow(0).getCell(0).toString());
|
|
|
|
assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 1 _x005fx0046_ with changes" : "row 0, cell 1 _x005fx0046_ with changes"),
|
|
|
|
wb.getSheetAt(0).getRow(0).getCell(1).toString());
|
|
|
|
assertEquals((wb instanceof HSSFWorkbook ? "hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*" : "hghA**\u0100*\u0101*\u0190*\u0200*\u0300*\u0427*"),
|
|
|
|
wb.getSheetAt(0).getRow(0).getCell(2).toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum length of cell contents (text) is 32,767 characters.
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void testMaxTextLength() throws IOException{
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
2015-04-03 11:55:21 -04:00
|
|
|
Sheet sheet = wb.createSheet();
|
2015-11-29 23:50:18 -05:00
|
|
|
Cell cell = sheet.createRow(0).createCell(0);
|
|
|
|
|
|
|
|
int maxlen = wb instanceof HSSFWorkbook ?
|
|
|
|
SpreadsheetVersion.EXCEL97.getMaxTextLength()
|
|
|
|
: SpreadsheetVersion.EXCEL2007.getMaxTextLength();
|
|
|
|
assertEquals(32767, maxlen);
|
|
|
|
|
|
|
|
StringBuffer b = new StringBuffer() ;
|
|
|
|
|
|
|
|
// 32767 is okay
|
|
|
|
for( int i = 0 ; i < maxlen ; i++ )
|
|
|
|
{
|
|
|
|
b.append( "X" ) ;
|
|
|
|
}
|
|
|
|
cell.setCellValue(b.toString());
|
|
|
|
|
|
|
|
b.append("X");
|
|
|
|
// 32768 produces an invalid XLS file
|
|
|
|
try {
|
|
|
|
cell.setCellValue(b.toString());
|
|
|
|
fail("Expected exception");
|
|
|
|
} catch (IllegalArgumentException e){
|
|
|
|
assertEquals("The maximum length of cell contents (text) is 32,767 characters", e.getMessage());
|
|
|
|
}
|
|
|
|
wb.close();
|
|
|
|
}
|
2015-12-04 01:36:27 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that the setAsActiveCell and getActiveCell function pairs work together
|
|
|
|
*/
|
|
|
|
@Test
|
|
|
|
public void setAsActiveCell() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sheet = wb.createSheet();
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
Cell A1 = row.createCell(0);
|
|
|
|
Cell B1 = row.createCell(1);
|
|
|
|
|
|
|
|
A1.setAsActiveCell();
|
|
|
|
assertEquals(A1.getAddress(), sheet.getActiveCell());
|
|
|
|
|
|
|
|
B1.setAsActiveCell();
|
|
|
|
assertEquals(B1.getAddress(), sheet.getActiveCell());
|
2015-12-22 17:35:59 -05:00
|
|
|
|
|
|
|
wb.close();
|
2015-12-04 01:36:27 -05:00
|
|
|
}
|
2016-06-17 04:54:08 -04:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void getCellComment() throws IOException {
|
|
|
|
Workbook wb = _testDataProvider.createWorkbook();
|
|
|
|
Sheet sheet = wb.createSheet();
|
|
|
|
CreationHelper factory = wb.getCreationHelper();
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
Cell cell = row.createCell(1);
|
|
|
|
|
|
|
|
// cell does not have a comment
|
|
|
|
assertNull(cell.getCellComment());
|
|
|
|
|
|
|
|
// add a cell comment
|
|
|
|
ClientAnchor anchor = factory.createClientAnchor();
|
|
|
|
anchor.setCol1(cell.getColumnIndex());
|
|
|
|
anchor.setCol2(cell.getColumnIndex()+1);
|
|
|
|
anchor.setRow1(row.getRowNum());
|
|
|
|
anchor.setRow2(row.getRowNum()+3);
|
|
|
|
|
|
|
|
Drawing drawing = sheet.createDrawingPatriarch();
|
|
|
|
Comment comment = drawing.createCellComment(anchor);
|
|
|
|
RichTextString str = factory.createRichTextString("Hello, World!");
|
|
|
|
comment.setString(str);
|
|
|
|
comment.setAuthor("Apache POI");
|
|
|
|
cell.setCellComment(comment);
|
|
|
|
// ideally assertSame, but XSSFCell creates a new XSSFCellComment wrapping the same bean for every call to getCellComment.
|
|
|
|
assertEquals(comment, cell.getCellComment());
|
|
|
|
|
|
|
|
wb.close();
|
|
|
|
}
|
2009-04-11 13:53:39 -04:00
|
|
|
}
|