You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseTestCell.java 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.usermodel;
  16. import static org.apache.poi.ss.usermodel.FormulaError.forInt;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertFalse;
  19. import static org.junit.Assert.assertNotNull;
  20. import static org.junit.Assert.assertNull;
  21. import static org.junit.Assert.assertTrue;
  22. import static org.junit.Assert.fail;
  23. import java.io.IOException;
  24. import java.util.Calendar;
  25. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  26. import org.apache.poi.ss.ITestDataProvider;
  27. import org.apache.poi.ss.SpreadsheetVersion;
  28. import org.apache.poi.util.LocaleUtil;
  29. import org.junit.Test;
  30. /**
  31. * Common superclass for testing implementations of
  32. * {@link org.apache.poi.ss.usermodel.Cell}
  33. */
  34. public abstract class BaseTestCell {
  35. protected final ITestDataProvider _testDataProvider;
  36. /**
  37. * @param testDataProvider an object that provides test data in HSSF / XSSF specific way
  38. */
  39. protected BaseTestCell(ITestDataProvider testDataProvider) {
  40. _testDataProvider = testDataProvider;
  41. }
  42. @Test
  43. public void testSetValues() throws Exception {
  44. Workbook book = _testDataProvider.createWorkbook();
  45. Sheet sheet = book.createSheet("test");
  46. Row row = sheet.createRow(0);
  47. CreationHelper factory = book.getCreationHelper();
  48. Cell cell = row.createCell(0);
  49. cell.setCellValue(1.2);
  50. assertEquals(1.2, cell.getNumericCellValue(), 0.0001);
  51. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  52. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING,
  53. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  54. cell.setCellValue(false);
  55. assertEquals(false, cell.getBooleanCellValue());
  56. assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType());
  57. cell.setCellValue(true);
  58. assertEquals(true, cell.getBooleanCellValue());
  59. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_STRING,
  60. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  61. cell.setCellValue(factory.createRichTextString("Foo"));
  62. assertEquals("Foo", cell.getRichStringCellValue().getString());
  63. assertEquals("Foo", cell.getStringCellValue());
  64. assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
  65. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN,
  66. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  67. cell.setCellValue("345");
  68. assertEquals("345", cell.getRichStringCellValue().getString());
  69. assertEquals("345", cell.getStringCellValue());
  70. assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
  71. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN,
  72. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  73. Calendar c = LocaleUtil.getLocaleCalendar();
  74. c.setTimeInMillis(123456789);
  75. cell.setCellValue(c.getTime());
  76. assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime());
  77. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  78. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING,
  79. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  80. cell.setCellValue(c);
  81. assertEquals(c.getTime().getTime(), cell.getDateCellValue().getTime());
  82. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  83. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_BOOLEAN, Cell.CELL_TYPE_STRING,
  84. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_ERROR);
  85. cell.setCellErrorValue(FormulaError.NA.getCode());
  86. assertEquals(FormulaError.NA.getCode(), cell.getErrorCellValue());
  87. assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType());
  88. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_NUMERIC, Cell.CELL_TYPE_BOOLEAN,
  89. Cell.CELL_TYPE_FORMULA, Cell.CELL_TYPE_STRING);
  90. book.close();
  91. }
  92. private static void assertProhibitedValueAccess(Cell cell, int ... types){
  93. for(int type : types){
  94. try {
  95. switch (type) {
  96. case Cell.CELL_TYPE_NUMERIC:
  97. cell.getNumericCellValue();
  98. break;
  99. case Cell.CELL_TYPE_STRING:
  100. cell.getStringCellValue();
  101. break;
  102. case Cell.CELL_TYPE_BOOLEAN:
  103. cell.getBooleanCellValue();
  104. break;
  105. case Cell.CELL_TYPE_FORMULA:
  106. cell.getCellFormula();
  107. break;
  108. case Cell.CELL_TYPE_ERROR:
  109. cell.getErrorCellValue();
  110. break;
  111. }
  112. fail("Should get exception when reading cell type (" + type + ").");
  113. } catch (IllegalStateException e){
  114. // expected during successful test
  115. assertTrue(e.getMessage().startsWith("Cannot get a"));
  116. }
  117. }
  118. }
  119. /**
  120. * test that Boolean (BoolErrRecord) are supported properly.
  121. * @see testErr
  122. */
  123. @Test
  124. public void testBool() throws IOException {
  125. Workbook wb1 = _testDataProvider.createWorkbook();
  126. Sheet s = wb1.createSheet("testSheet1");
  127. Row r;
  128. Cell c;
  129. // B1
  130. r = s.createRow(0);
  131. c=r.createCell(1);
  132. assertEquals(0, c.getRowIndex());
  133. assertEquals(1, c.getColumnIndex());
  134. c.setCellValue(true);
  135. assertEquals("B1 value", true, c.getBooleanCellValue());
  136. // C1
  137. c=r.createCell(2);
  138. assertEquals(0, c.getRowIndex());
  139. assertEquals(2, c.getColumnIndex());
  140. c.setCellValue(false);
  141. assertEquals("C1 value", false, c.getBooleanCellValue());
  142. // Make sure values are saved and re-read correctly.
  143. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  144. wb1.close();
  145. s = wb2.getSheet("testSheet1");
  146. r = s.getRow(0);
  147. assertEquals("Row 1 should have 2 cells", 2, r.getPhysicalNumberOfCells());
  148. c = r.getCell(1);
  149. assertEquals(0, c.getRowIndex());
  150. assertEquals(1, c.getColumnIndex());
  151. assertEquals(Cell.CELL_TYPE_BOOLEAN, c.getCellType());
  152. assertEquals("B1 value", true, c.getBooleanCellValue());
  153. c = r.getCell(2);
  154. assertEquals(0, c.getRowIndex());
  155. assertEquals(2, c.getColumnIndex());
  156. assertEquals(Cell.CELL_TYPE_BOOLEAN, c.getCellType());
  157. assertEquals("C1 value", false, c.getBooleanCellValue());
  158. wb2.close();
  159. }
  160. /**
  161. * test that Error types (BoolErrRecord) are supported properly.
  162. * @see testBool
  163. */
  164. @Test
  165. public void testErr() throws IOException {
  166. Workbook wb1 = _testDataProvider.createWorkbook();
  167. Sheet s = wb1.createSheet("testSheet1");
  168. Row r;
  169. Cell c;
  170. // B1
  171. r = s.createRow(0);
  172. c=r.createCell(1);
  173. assertEquals(0, c.getRowIndex());
  174. assertEquals(1, c.getColumnIndex());
  175. c.setCellErrorValue(FormulaError.NULL.getCode());
  176. assertEquals("B1 value == #NULL!", FormulaError.NULL.getCode(), c.getErrorCellValue());
  177. // C1
  178. c=r.createCell(2);
  179. assertEquals(0, c.getRowIndex());
  180. assertEquals(2, c.getColumnIndex());
  181. c.setCellErrorValue(FormulaError.DIV0.getCode());
  182. assertEquals("C1 value == #DIV/0!", FormulaError.DIV0.getCode(), c.getErrorCellValue());
  183. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  184. wb1.close();
  185. s = wb2.getSheet("testSheet1");
  186. r = s.getRow(0);
  187. assertEquals("Row 1 should have 2 cells", 2, r.getPhysicalNumberOfCells());
  188. c = r.getCell(1);
  189. assertEquals(0, c.getRowIndex());
  190. assertEquals(1, c.getColumnIndex());
  191. assertEquals(Cell.CELL_TYPE_ERROR, c.getCellType());
  192. assertEquals("B1 value == #NULL!", FormulaError.NULL.getCode(), c.getErrorCellValue());
  193. c = r.getCell(2);
  194. assertEquals(0, c.getRowIndex());
  195. assertEquals(2, c.getColumnIndex());
  196. assertEquals(Cell.CELL_TYPE_ERROR, c.getCellType());
  197. assertEquals("C1 value == #DIV/0!", FormulaError.DIV0.getCode(), c.getErrorCellValue());
  198. wb2.close();
  199. }
  200. /**
  201. * test that Cell Styles being applied to formulas remain intact
  202. */
  203. @Test
  204. public void testFormulaStyle() throws Exception {
  205. Workbook wb1 = _testDataProvider.createWorkbook();
  206. Sheet s = wb1.createSheet("testSheet1");
  207. Row r = null;
  208. Cell c = null;
  209. CellStyle cs = wb1.createCellStyle();
  210. Font f = wb1.createFont();
  211. f.setFontHeightInPoints((short) 20);
  212. f.setColor(IndexedColors.RED.getIndex());
  213. f.setBoldweight(Font.BOLDWEIGHT_BOLD);
  214. f.setFontName("Arial Unicode MS");
  215. cs.setFillBackgroundColor((short)3);
  216. cs.setFont(f);
  217. cs.setBorderTop(BorderStyle.THIN);
  218. cs.setBorderRight(BorderStyle.THIN);
  219. cs.setBorderLeft(BorderStyle.THIN);
  220. cs.setBorderBottom(BorderStyle.THIN);
  221. r = s.createRow(0);
  222. c=r.createCell(0);
  223. c.setCellStyle(cs);
  224. c.setCellFormula("2*3");
  225. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  226. wb1.close();
  227. s = wb2.getSheetAt(0);
  228. r = s.getRow(0);
  229. c = r.getCell(0);
  230. assertEquals("Formula Cell at 0,0", Cell.CELL_TYPE_FORMULA, c.getCellType());
  231. cs = c.getCellStyle();
  232. assertNotNull("Formula Cell Style", cs);
  233. assertEquals("Font Index Matches", f.getIndex(), cs.getFontIndex());
  234. assertEquals("Top Border", BorderStyle.THIN, cs.getBorderTop());
  235. assertEquals("Left Border", BorderStyle.THIN, cs.getBorderLeft());
  236. assertEquals("Right Border", BorderStyle.THIN, cs.getBorderRight());
  237. assertEquals("Bottom Border", BorderStyle.THIN, cs.getBorderBottom());
  238. wb2.close();
  239. }
  240. /**tests the toString() method of HSSFCell*/
  241. @Test
  242. public void testToString() throws Exception {
  243. Workbook wb1 = _testDataProvider.createWorkbook();
  244. Row r = wb1.createSheet("Sheet1").createRow(0);
  245. CreationHelper factory = wb1.getCreationHelper();
  246. r.createCell(0).setCellValue(true);
  247. r.createCell(1).setCellValue(1.5);
  248. r.createCell(2).setCellValue(factory.createRichTextString("Astring"));
  249. r.createCell(3).setCellErrorValue(FormulaError.DIV0.getCode());
  250. r.createCell(4).setCellFormula("A1+B1");
  251. assertEquals("Boolean", "TRUE", r.getCell(0).toString());
  252. assertEquals("Numeric", "1.5", r.getCell(1).toString());
  253. assertEquals("String", "Astring", r.getCell(2).toString());
  254. assertEquals("Error", "#DIV/0!", r.getCell(3).toString());
  255. assertEquals("Formula", "A1+B1", r.getCell(4).toString());
  256. //Write out the file, read it in, and then check cell values
  257. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  258. wb1.close();
  259. r = wb2.getSheetAt(0).getRow(0);
  260. assertEquals("Boolean", "TRUE", r.getCell(0).toString());
  261. assertEquals("Numeric", "1.5", r.getCell(1).toString());
  262. assertEquals("String", "Astring", r.getCell(2).toString());
  263. assertEquals("Error", "#DIV/0!", r.getCell(3).toString());
  264. assertEquals("Formula", "A1+B1", r.getCell(4).toString());
  265. wb2.close();
  266. }
  267. /**
  268. * Test that setting cached formula result keeps the cell type
  269. */
  270. @Test
  271. public void testSetFormulaValue() throws Exception {
  272. Workbook wb = _testDataProvider.createWorkbook();
  273. Sheet s = wb.createSheet();
  274. Row r = s.createRow(0);
  275. Cell c1 = r.createCell(0);
  276. c1.setCellFormula("NA()");
  277. assertEquals(0.0, c1.getNumericCellValue(), 0.0);
  278. assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType());
  279. c1.setCellValue(10);
  280. assertEquals(10.0, c1.getNumericCellValue(), 0.0);
  281. assertEquals(Cell.CELL_TYPE_FORMULA, c1.getCellType());
  282. assertEquals(Cell.CELL_TYPE_NUMERIC, c1.getCachedFormulaResultType());
  283. Cell c2 = r.createCell(1);
  284. c2.setCellFormula("NA()");
  285. assertEquals(0.0, c2.getNumericCellValue(), 0.0);
  286. assertEquals(Cell.CELL_TYPE_NUMERIC, c2.getCachedFormulaResultType());
  287. c2.setCellValue("I changed!");
  288. assertEquals("I changed!", c2.getStringCellValue());
  289. assertEquals(Cell.CELL_TYPE_FORMULA, c2.getCellType());
  290. assertEquals(Cell.CELL_TYPE_STRING, c2.getCachedFormulaResultType());
  291. //calglin Cell.setCellFormula(null) for a non-formula cell
  292. Cell c3 = r.createCell(2);
  293. c3.setCellFormula(null);
  294. assertEquals(Cell.CELL_TYPE_BLANK, c3.getCellType());
  295. wb.close();
  296. }
  297. private Cell createACell(Workbook wb) {
  298. return wb.createSheet("Sheet1").createRow(0).createCell(0);
  299. }
  300. /**
  301. * bug 58452: Copy cell formulas containing unregistered function names
  302. * Make sure that formulas with unknown/unregistered UDFs can be written to and read back from a file.
  303. *
  304. * @throws IOException
  305. */
  306. @Test
  307. public void testFormulaWithUnknownUDF() throws IOException {
  308. final Workbook wb1 = _testDataProvider.createWorkbook();
  309. final FormulaEvaluator evaluator1 = wb1.getCreationHelper().createFormulaEvaluator();
  310. try {
  311. final Cell cell1 = wb1.createSheet().createRow(0).createCell(0);
  312. final String formula = "myFunc(\"arg\")";
  313. cell1.setCellFormula(formula);
  314. confirmFormulaWithUnknownUDF(formula, cell1, evaluator1);
  315. final Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  316. final FormulaEvaluator evaluator2 = wb2.getCreationHelper().createFormulaEvaluator();
  317. try {
  318. final Cell cell2 = wb2.getSheetAt(0).getRow(0).getCell(0);
  319. confirmFormulaWithUnknownUDF(formula, cell2, evaluator2);
  320. } finally {
  321. wb2.close();
  322. }
  323. } finally {
  324. wb1.close();
  325. }
  326. }
  327. private static void confirmFormulaWithUnknownUDF(String expectedFormula, Cell cell, FormulaEvaluator evaluator) {
  328. assertEquals(expectedFormula, cell.getCellFormula());
  329. try {
  330. evaluator.evaluate(cell);
  331. fail("Expected NotImplementedFunctionException/NotImplementedException");
  332. } catch (final org.apache.poi.ss.formula.eval.NotImplementedException e) {
  333. // expected
  334. }
  335. }
  336. @Test
  337. public void testChangeTypeStringToBool() throws IOException {
  338. Workbook wb = _testDataProvider.createWorkbook();
  339. Cell cell = createACell(wb);
  340. cell.setCellValue("TRUE");
  341. assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
  342. // test conversion of cell from text to boolean
  343. cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
  344. assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType());
  345. assertEquals(true, cell.getBooleanCellValue());
  346. cell.setCellType(Cell.CELL_TYPE_STRING);
  347. assertEquals("TRUE", cell.getRichStringCellValue().getString());
  348. // 'false' text to bool and back
  349. cell.setCellValue("FALSE");
  350. cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
  351. assertEquals(Cell.CELL_TYPE_BOOLEAN, cell.getCellType());
  352. assertEquals(false, cell.getBooleanCellValue());
  353. cell.setCellType(Cell.CELL_TYPE_STRING);
  354. assertEquals("FALSE", cell.getRichStringCellValue().getString());
  355. wb.close();
  356. }
  357. @Test
  358. public void testChangeTypeBoolToString() throws IOException {
  359. Workbook wb = _testDataProvider.createWorkbook();
  360. Cell cell = createACell(wb);
  361. cell.setCellValue(true);
  362. // test conversion of cell from boolean to text
  363. cell.setCellType(Cell.CELL_TYPE_STRING);
  364. assertEquals("TRUE", cell.getRichStringCellValue().getString());
  365. wb.close();
  366. }
  367. @Test
  368. public void testChangeTypeErrorToNumber() throws IOException {
  369. Workbook wb = _testDataProvider.createWorkbook();
  370. Cell cell = createACell(wb);
  371. cell.setCellErrorValue(FormulaError.NAME.getCode());
  372. try {
  373. cell.setCellValue(2.5);
  374. } catch (ClassCastException e) {
  375. fail("Identified bug 46479b");
  376. }
  377. assertEquals(2.5, cell.getNumericCellValue(), 0.0);
  378. wb.close();
  379. }
  380. @Test
  381. public void testChangeTypeErrorToBoolean() throws IOException {
  382. Workbook wb = _testDataProvider.createWorkbook();
  383. Cell cell = createACell(wb);
  384. cell.setCellErrorValue(FormulaError.NAME.getCode());
  385. cell.setCellValue(true);
  386. // Identify bug 46479c
  387. assertEquals(true, cell.getBooleanCellValue());
  388. wb.close();
  389. }
  390. /**
  391. * Test for a bug observed around svn r886733 when using
  392. * {@link FormulaEvaluator#evaluateInCell(Cell)} with a
  393. * string result type.
  394. */
  395. @Test
  396. public void testConvertStringFormulaCell() throws IOException {
  397. Workbook wb = _testDataProvider.createWorkbook();
  398. Cell cellA1 = createACell(wb);
  399. cellA1.setCellFormula("\"abc\"");
  400. // default cached formula result is numeric zero
  401. assertEquals(0.0, cellA1.getNumericCellValue(), 0.0);
  402. FormulaEvaluator fe = cellA1.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
  403. fe.evaluateFormulaCell(cellA1);
  404. assertEquals("abc", cellA1.getStringCellValue());
  405. fe.evaluateInCell(cellA1);
  406. assertFalse("Identified bug with writing back formula result of type string", cellA1.getStringCellValue().equals(""));
  407. assertEquals("abc", cellA1.getStringCellValue());
  408. wb.close();
  409. }
  410. /**
  411. * similar to {@link #testConvertStringFormulaCell()} but checks at a
  412. * lower level that {#link {@link Cell#setCellType(int)} works properly
  413. */
  414. @Test
  415. public void testSetTypeStringOnFormulaCell() throws IOException {
  416. Workbook wb = _testDataProvider.createWorkbook();
  417. Cell cellA1 = createACell(wb);
  418. FormulaEvaluator fe = cellA1.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
  419. cellA1.setCellFormula("\"DEF\"");
  420. fe.clearAllCachedResultValues();
  421. fe.evaluateFormulaCell(cellA1);
  422. assertEquals("DEF", cellA1.getStringCellValue());
  423. cellA1.setCellType(Cell.CELL_TYPE_STRING);
  424. assertEquals("DEF", cellA1.getStringCellValue());
  425. cellA1.setCellFormula("25.061");
  426. fe.clearAllCachedResultValues();
  427. fe.evaluateFormulaCell(cellA1);
  428. confirmCannotReadString(cellA1);
  429. assertEquals(25.061, cellA1.getNumericCellValue(), 0.0);
  430. cellA1.setCellType(Cell.CELL_TYPE_STRING);
  431. assertEquals("25.061", cellA1.getStringCellValue());
  432. cellA1.setCellFormula("TRUE");
  433. fe.clearAllCachedResultValues();
  434. fe.evaluateFormulaCell(cellA1);
  435. confirmCannotReadString(cellA1);
  436. assertEquals(true, cellA1.getBooleanCellValue());
  437. cellA1.setCellType(Cell.CELL_TYPE_STRING);
  438. assertEquals("TRUE", cellA1.getStringCellValue());
  439. cellA1.setCellFormula("#NAME?");
  440. fe.clearAllCachedResultValues();
  441. fe.evaluateFormulaCell(cellA1);
  442. confirmCannotReadString(cellA1);
  443. assertEquals(FormulaError.NAME, forInt(cellA1.getErrorCellValue()));
  444. cellA1.setCellType(Cell.CELL_TYPE_STRING);
  445. assertEquals("#NAME?", cellA1.getStringCellValue());
  446. wb.close();
  447. }
  448. private static void confirmCannotReadString(Cell cell) {
  449. assertProhibitedValueAccess(cell, Cell.CELL_TYPE_STRING);
  450. }
  451. /**
  452. * Test for bug in convertCellValueToBoolean to make sure that formula results get converted
  453. */
  454. @Test
  455. public void testChangeTypeFormulaToBoolean() throws IOException {
  456. Workbook wb = _testDataProvider.createWorkbook();
  457. Cell cell = createACell(wb);
  458. cell.setCellFormula("1=1");
  459. cell.setCellValue(true);
  460. cell.setCellType(Cell.CELL_TYPE_BOOLEAN);
  461. assertTrue("Identified bug 46479d", cell.getBooleanCellValue());
  462. assertEquals(true, cell.getBooleanCellValue());
  463. wb.close();
  464. }
  465. /**
  466. * Bug 40296: HSSFCell.setCellFormula throws
  467. * ClassCastException if cell is created using HSSFRow.createCell(short column, int type)
  468. */
  469. @Test
  470. public void test40296() throws Exception {
  471. Workbook wb1 = _testDataProvider.createWorkbook();
  472. Sheet workSheet = wb1.createSheet("Sheet1");
  473. Cell cell;
  474. Row row = workSheet.createRow(0);
  475. cell = row.createCell(0, Cell.CELL_TYPE_NUMERIC);
  476. cell.setCellValue(1.0);
  477. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  478. assertEquals(1.0, cell.getNumericCellValue(), 0.0);
  479. cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC);
  480. cell.setCellValue(2.0);
  481. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  482. assertEquals(2.0, cell.getNumericCellValue(), 0.0);
  483. cell = row.createCell(2, Cell.CELL_TYPE_FORMULA);
  484. cell.setCellFormula("SUM(A1:B1)");
  485. assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
  486. assertEquals("SUM(A1:B1)", cell.getCellFormula());
  487. //serialize and check again
  488. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  489. wb1.close();
  490. row = wb2.getSheetAt(0).getRow(0);
  491. cell = row.getCell(0);
  492. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  493. assertEquals(1.0, cell.getNumericCellValue(), 0.0);
  494. cell = row.getCell(1);
  495. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  496. assertEquals(2.0, cell.getNumericCellValue(), 0.0);
  497. cell = row.getCell(2);
  498. assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
  499. assertEquals("SUM(A1:B1)", cell.getCellFormula());
  500. wb2.close();
  501. }
  502. @Test
  503. public void testSetStringInFormulaCell_bug44606() throws Exception {
  504. Workbook wb = _testDataProvider.createWorkbook();
  505. Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
  506. cell.setCellFormula("B1&C1");
  507. cell.setCellValue(wb.getCreationHelper().createRichTextString("hello"));
  508. wb.close();
  509. }
  510. /**
  511. * Make sure that cell.setCellType(Cell.CELL_TYPE_BLANK) preserves the cell style
  512. */
  513. @Test
  514. public void testSetBlank_bug47028() throws Exception {
  515. Workbook wb = _testDataProvider.createWorkbook();
  516. CellStyle style = wb.createCellStyle();
  517. Cell cell = wb.createSheet("Sheet1").createRow(0).createCell(0);
  518. cell.setCellStyle(style);
  519. int i1 = cell.getCellStyle().getIndex();
  520. cell.setCellType(Cell.CELL_TYPE_BLANK);
  521. int i2 = cell.getCellStyle().getIndex();
  522. assertEquals(i1, i2);
  523. wb.close();
  524. }
  525. /**
  526. * Excel's implementation of floating number arithmetic does not fully adhere to IEEE 754:
  527. *
  528. * From http://support.microsoft.com/kb/78113:
  529. *
  530. * <ul>
  531. * <li> Positive/Negative Infinities:
  532. * Infinities occur when you divide by 0. Excel does not support infinities, rather,
  533. * it gives a #DIV/0! error in these cases.
  534. * </li>
  535. * <li>
  536. * Not-a-Number (NaN):
  537. * NaN is used to represent invalid operations (such as infinity/infinity,
  538. * infinity-infinity, or the square root of -1). NaNs allow a program to
  539. * continue past an invalid operation. Excel instead immediately generates
  540. * an error such as #NUM! or #DIV/0!.
  541. * </li>
  542. * </ul>
  543. */
  544. @Test
  545. public void testNanAndInfinity() throws Exception {
  546. Workbook wb1 = _testDataProvider.createWorkbook();
  547. Sheet workSheet = wb1.createSheet("Sheet1");
  548. Row row = workSheet.createRow(0);
  549. Cell cell0 = row.createCell(0);
  550. cell0.setCellValue(Double.NaN);
  551. assertEquals("Double.NaN should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell0.getCellType());
  552. assertEquals("Double.NaN should change cell value to #NUM!", FormulaError.NUM, forInt(cell0.getErrorCellValue()));
  553. Cell cell1 = row.createCell(1);
  554. cell1.setCellValue(Double.POSITIVE_INFINITY);
  555. assertEquals("Double.POSITIVE_INFINITY should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell1.getCellType());
  556. assertEquals("Double.POSITIVE_INFINITY should change cell value to #DIV/0!", FormulaError.DIV0, forInt(cell1.getErrorCellValue()));
  557. Cell cell2 = row.createCell(2);
  558. cell2.setCellValue(Double.NEGATIVE_INFINITY);
  559. assertEquals("Double.NEGATIVE_INFINITY should change cell type to CELL_TYPE_ERROR", Cell.CELL_TYPE_ERROR, cell2.getCellType());
  560. assertEquals("Double.NEGATIVE_INFINITY should change cell value to #DIV/0!", FormulaError.DIV0, forInt(cell2.getErrorCellValue()));
  561. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  562. wb1.close();
  563. row = wb2.getSheetAt(0).getRow(0);
  564. cell0 = row.getCell(0);
  565. assertEquals(Cell.CELL_TYPE_ERROR, cell0.getCellType());
  566. assertEquals(FormulaError.NUM, forInt(cell0.getErrorCellValue()));
  567. cell1 = row.getCell(1);
  568. assertEquals(Cell.CELL_TYPE_ERROR, cell1.getCellType());
  569. assertEquals(FormulaError.DIV0, forInt(cell1.getErrorCellValue()));
  570. cell2 = row.getCell(2);
  571. assertEquals(Cell.CELL_TYPE_ERROR, cell2.getCellType());
  572. assertEquals(FormulaError.DIV0, forInt(cell2.getErrorCellValue()));
  573. wb2.close();
  574. }
  575. @Test
  576. public void testDefaultStyleProperties() throws Exception {
  577. Workbook wb1 = _testDataProvider.createWorkbook();
  578. Cell cell = wb1.createSheet("Sheet1").createRow(0).createCell(0);
  579. CellStyle style = cell.getCellStyle();
  580. assertTrue(style.getLocked());
  581. assertFalse(style.getHidden());
  582. assertEquals(0, style.getIndention());
  583. assertEquals(0, style.getFontIndex());
  584. assertEquals(0, style.getAlignment());
  585. assertEquals(0, style.getDataFormat());
  586. assertEquals(false, style.getWrapText());
  587. CellStyle style2 = wb1.createCellStyle();
  588. assertTrue(style2.getLocked());
  589. assertFalse(style2.getHidden());
  590. style2.setLocked(false);
  591. style2.setHidden(true);
  592. assertFalse(style2.getLocked());
  593. assertTrue(style2.getHidden());
  594. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  595. wb1.close();
  596. cell = wb2.getSheetAt(0).getRow(0).getCell(0);
  597. style = cell.getCellStyle();
  598. assertFalse(style2.getLocked());
  599. assertTrue(style2.getHidden());
  600. style2.setLocked(true);
  601. style2.setHidden(false);
  602. assertTrue(style2.getLocked());
  603. assertFalse(style2.getHidden());
  604. wb2.close();
  605. }
  606. @Test
  607. public void testBug55658SetNumericValue() throws Exception {
  608. Workbook wb1 = _testDataProvider.createWorkbook();
  609. Sheet sh = wb1.createSheet();
  610. Row row = sh.createRow(0);
  611. Cell cell = row.createCell(0);
  612. cell.setCellValue(Integer.valueOf(23));
  613. cell.setCellValue("some");
  614. cell = row.createCell(1);
  615. cell.setCellValue(Integer.valueOf(23));
  616. cell.setCellValue("24");
  617. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  618. wb1.close();
  619. assertEquals("some", wb2.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
  620. assertEquals("24", wb2.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
  621. wb2.close();
  622. }
  623. @Test
  624. public void testRemoveHyperlink() throws Exception {
  625. Workbook wb1 = _testDataProvider.createWorkbook();
  626. Sheet sh = wb1.createSheet("test");
  627. Row row = sh.createRow(0);
  628. CreationHelper helper = wb1.getCreationHelper();
  629. Cell cell1 = row.createCell(1);
  630. Hyperlink link1 = helper.createHyperlink(Hyperlink.LINK_URL);
  631. cell1.setHyperlink(link1);
  632. assertNotNull(cell1.getHyperlink());
  633. cell1.removeHyperlink();
  634. assertNull(cell1.getHyperlink());
  635. Cell cell2 = row.createCell(0);
  636. Hyperlink link2 = helper.createHyperlink(Hyperlink.LINK_URL);
  637. cell2.setHyperlink(link2);
  638. assertNotNull(cell2.getHyperlink());
  639. cell2.setHyperlink(null);
  640. assertNull(cell2.getHyperlink());
  641. Cell cell3 = row.createCell(2);
  642. Hyperlink link3 = helper.createHyperlink(Hyperlink.LINK_URL);
  643. link3.setAddress("http://poi.apache.org/");
  644. cell3.setHyperlink(link3);
  645. assertNotNull(cell3.getHyperlink());
  646. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  647. wb1.close();
  648. assertNotNull(wb2);
  649. cell1 = wb2.getSheet("test").getRow(0).getCell(1);
  650. assertNull(cell1.getHyperlink());
  651. cell2 = wb2.getSheet("test").getRow(0).getCell(0);
  652. assertNull(cell2.getHyperlink());
  653. cell3 = wb2.getSheet("test").getRow(0).getCell(2);
  654. assertNotNull(cell3.getHyperlink());
  655. wb2.close();
  656. }
  657. /**
  658. * Cell with the formula that returns error must return error code(There was
  659. * an problem that cell could not return error value form formula cell).
  660. * @throws IOException
  661. */
  662. @Test
  663. public void testGetErrorCellValueFromFormulaCell() throws IOException {
  664. Workbook wb = _testDataProvider.createWorkbook();
  665. try {
  666. Sheet sheet = wb.createSheet();
  667. Row row = sheet.createRow(0);
  668. Cell cell = row.createCell(0);
  669. cell.setCellFormula("SQRT(-1)");
  670. wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(cell);
  671. assertEquals(36, cell.getErrorCellValue());
  672. } finally {
  673. wb.close();
  674. }
  675. }
  676. @Test
  677. public void testSetRemoveStyle() throws Exception {
  678. Workbook wb = _testDataProvider.createWorkbook();
  679. Sheet sheet = wb.createSheet();
  680. Row row = sheet.createRow(0);
  681. Cell cell = row.createCell(0);
  682. // different default style indexes for HSSF and XSSF/SXSSF
  683. CellStyle defaultStyle = wb.getCellStyleAt(wb instanceof HSSFWorkbook ? (short)15 : (short)0);
  684. // Starts out with the default style
  685. assertEquals(defaultStyle, cell.getCellStyle());
  686. // Create some styles, no change
  687. CellStyle style1 = wb.createCellStyle();
  688. CellStyle style2 = wb.createCellStyle();
  689. style1.setDataFormat((short)2);
  690. style2.setDataFormat((short)3);
  691. assertEquals(defaultStyle, cell.getCellStyle());
  692. // Apply one, changes
  693. cell.setCellStyle(style1);
  694. assertEquals(style1, cell.getCellStyle());
  695. // Apply the other, changes
  696. cell.setCellStyle(style2);
  697. assertEquals(style2, cell.getCellStyle());
  698. // Remove, goes back to default
  699. cell.setCellStyle(null);
  700. assertEquals(defaultStyle, cell.getCellStyle());
  701. // Add back, returns
  702. cell.setCellStyle(style2);
  703. assertEquals(style2, cell.getCellStyle());
  704. wb.close();
  705. }
  706. @Test
  707. public void test57008() throws IOException {
  708. Workbook wb1 = _testDataProvider.createWorkbook();
  709. Sheet sheet = wb1.createSheet();
  710. Row row0 = sheet.createRow(0);
  711. Cell cell0 = row0.createCell(0);
  712. cell0.setCellValue("row 0, cell 0 _x0046_ without changes");
  713. Cell cell1 = row0.createCell(1);
  714. cell1.setCellValue("row 0, cell 1 _x005fx0046_ with changes");
  715. Cell cell2 = row0.createCell(2);
  716. cell2.setCellValue("hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*");
  717. checkUnicodeValues(wb1);
  718. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  719. checkUnicodeValues(wb2);
  720. wb2.close();
  721. wb1.close();
  722. }
  723. /**
  724. * Setting a cell value of a null RichTextString should set
  725. * the cell to Blank, test case for 58558
  726. */
  727. @Test
  728. public void testSetCellValueNullRichTextString() throws IOException {
  729. Workbook wb = _testDataProvider.createWorkbook();
  730. Sheet sheet = wb.createSheet();
  731. Cell cell = sheet.createRow(0).createCell(0);
  732. RichTextString nullStr = null;
  733. cell.setCellValue(nullStr);
  734. assertEquals("", cell.getStringCellValue());
  735. assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
  736. cell = sheet.createRow(0).createCell(1);
  737. cell.setCellValue(1.2d);
  738. assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
  739. cell.setCellValue(nullStr);
  740. assertEquals("", cell.getStringCellValue());
  741. assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
  742. cell = sheet.createRow(0).createCell(1);
  743. cell.setCellValue(wb.getCreationHelper().createRichTextString("Test"));
  744. assertEquals(Cell.CELL_TYPE_STRING, cell.getCellType());
  745. cell.setCellValue(nullStr);
  746. assertEquals("", cell.getStringCellValue());
  747. assertEquals(Cell.CELL_TYPE_BLANK, cell.getCellType());
  748. wb.close();
  749. }
  750. private void checkUnicodeValues(Workbook wb) {
  751. assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 0 _x0046_ without changes" : "row 0, cell 0 F without changes"),
  752. wb.getSheetAt(0).getRow(0).getCell(0).toString());
  753. assertEquals((wb instanceof HSSFWorkbook ? "row 0, cell 1 _x005fx0046_ with changes" : "row 0, cell 1 _x005fx0046_ with changes"),
  754. wb.getSheetAt(0).getRow(0).getCell(1).toString());
  755. assertEquals((wb instanceof HSSFWorkbook ? "hgh_x0041_**_x0100_*_x0101_*_x0190_*_x0200_*_x0300_*_x0427_*" : "hghA**\u0100*\u0101*\u0190*\u0200*\u0300*\u0427*"),
  756. wb.getSheetAt(0).getRow(0).getCell(2).toString());
  757. }
  758. /**
  759. * The maximum length of cell contents (text) is 32,767 characters.
  760. * @throws IOException
  761. */
  762. @Test
  763. public void testMaxTextLength() throws IOException{
  764. Workbook wb = _testDataProvider.createWorkbook();
  765. Sheet sheet = wb.createSheet();
  766. Cell cell = sheet.createRow(0).createCell(0);
  767. int maxlen = wb instanceof HSSFWorkbook ?
  768. SpreadsheetVersion.EXCEL97.getMaxTextLength()
  769. : SpreadsheetVersion.EXCEL2007.getMaxTextLength();
  770. assertEquals(32767, maxlen);
  771. StringBuffer b = new StringBuffer() ;
  772. // 32767 is okay
  773. for( int i = 0 ; i < maxlen ; i++ )
  774. {
  775. b.append( "X" ) ;
  776. }
  777. cell.setCellValue(b.toString());
  778. b.append("X");
  779. // 32768 produces an invalid XLS file
  780. try {
  781. cell.setCellValue(b.toString());
  782. fail("Expected exception");
  783. } catch (IllegalArgumentException e){
  784. assertEquals("The maximum length of cell contents (text) is 32,767 characters", e.getMessage());
  785. }
  786. wb.close();
  787. }
  788. /**
  789. * Tests that the setAsActiveCell and getActiveCell function pairs work together
  790. */
  791. @Test
  792. public void setAsActiveCell() throws IOException {
  793. Workbook wb = _testDataProvider.createWorkbook();
  794. Sheet sheet = wb.createSheet();
  795. Row row = sheet.createRow(0);
  796. Cell A1 = row.createCell(0);
  797. Cell B1 = row.createCell(1);
  798. A1.setAsActiveCell();
  799. assertEquals(A1.getAddress(), sheet.getActiveCell());
  800. B1.setAsActiveCell();
  801. assertEquals(B1.getAddress(), sheet.getActiveCell());
  802. wb.close();
  803. }
  804. }