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.

TestXSSFBugs.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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.xssf.usermodel;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.util.List;
  19. import org.apache.poi.POIXMLDocumentPart;
  20. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  21. import org.apache.poi.openxml4j.opc.OPCPackage;
  22. import org.apache.poi.openxml4j.opc.PackagePart;
  23. import org.apache.poi.openxml4j.opc.PackagingURIHelper;
  24. import org.apache.poi.ss.usermodel.*;
  25. import org.apache.poi.ss.formula.eval.NotImplementedException;
  26. import org.apache.poi.xssf.XSSFITestDataProvider;
  27. import org.apache.poi.xssf.XSSFTestDataSamples;
  28. import org.apache.poi.xssf.model.CalculationChain;
  29. import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
  30. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
  31. public final class TestXSSFBugs extends BaseTestBugzillaIssues {
  32. public TestXSSFBugs() {
  33. super(XSSFITestDataProvider.instance);
  34. }
  35. /**
  36. * test writing a file with large number of unique strings,
  37. * open resulting file in Excel to check results!
  38. */
  39. public void test15375_2() {
  40. baseTest15375(1000);
  41. }
  42. /**
  43. * Named ranges had the right reference, but
  44. * the wrong sheet name
  45. */
  46. public void test45430() {
  47. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("45430.xlsx");
  48. assertFalse(wb.isMacroEnabled());
  49. assertEquals(3, wb.getNumberOfNames());
  50. assertEquals(0, wb.getNameAt(0).getCTName().getLocalSheetId());
  51. assertFalse(wb.getNameAt(0).getCTName().isSetLocalSheetId());
  52. assertEquals("SheetA!$A$1", wb.getNameAt(0).getRefersToFormula());
  53. assertEquals("SheetA", wb.getNameAt(0).getSheetName());
  54. assertEquals(0, wb.getNameAt(1).getCTName().getLocalSheetId());
  55. assertFalse(wb.getNameAt(1).getCTName().isSetLocalSheetId());
  56. assertEquals("SheetB!$A$1", wb.getNameAt(1).getRefersToFormula());
  57. assertEquals("SheetB", wb.getNameAt(1).getSheetName());
  58. assertEquals(0, wb.getNameAt(2).getCTName().getLocalSheetId());
  59. assertFalse(wb.getNameAt(2).getCTName().isSetLocalSheetId());
  60. assertEquals("SheetC!$A$1", wb.getNameAt(2).getRefersToFormula());
  61. assertEquals("SheetC", wb.getNameAt(2).getSheetName());
  62. // Save and re-load, still there
  63. XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  64. assertEquals(3, nwb.getNumberOfNames());
  65. assertEquals("SheetA!$A$1", nwb.getNameAt(0).getRefersToFormula());
  66. }
  67. /**
  68. * We should carry vba macros over after save
  69. */
  70. public void test45431() throws Exception {
  71. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("45431.xlsm");
  72. OPCPackage pkg = wb.getPackage();
  73. assertTrue(wb.isMacroEnabled());
  74. // Check the various macro related bits can be found
  75. PackagePart vba = pkg.getPart(
  76. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  77. );
  78. assertNotNull(vba);
  79. // And the drawing bit
  80. PackagePart drw = pkg.getPart(
  81. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  82. );
  83. assertNotNull(drw);
  84. // Save and re-open, both still there
  85. XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  86. OPCPackage nPkg = nwb.getPackage();
  87. assertTrue(nwb.isMacroEnabled());
  88. vba = nPkg.getPart(
  89. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  90. );
  91. assertNotNull(vba);
  92. drw = nPkg.getPart(
  93. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  94. );
  95. assertNotNull(drw);
  96. // And again, just to be sure
  97. nwb = XSSFTestDataSamples.writeOutAndReadBack(nwb);
  98. nPkg = nwb.getPackage();
  99. assertTrue(nwb.isMacroEnabled());
  100. vba = nPkg.getPart(
  101. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  102. );
  103. assertNotNull(vba);
  104. drw = nPkg.getPart(
  105. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  106. );
  107. assertNotNull(drw);
  108. }
  109. public void test47504() {
  110. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47504.xlsx");
  111. assertEquals(1, wb.getNumberOfSheets());
  112. XSSFSheet sh = wb.getSheetAt(0);
  113. XSSFDrawing drawing = sh.createDrawingPatriarch();
  114. List<POIXMLDocumentPart> rels = drawing.getRelations();
  115. assertEquals(1, rels.size());
  116. assertEquals("Sheet1!A1", rels.get(0).getPackageRelationship().getTargetURI().getFragment());
  117. // And again, just to be sure
  118. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  119. assertEquals(1, wb.getNumberOfSheets());
  120. sh = wb.getSheetAt(0);
  121. drawing = sh.createDrawingPatriarch();
  122. rels = drawing.getRelations();
  123. assertEquals(1, rels.size());
  124. assertEquals("Sheet1!A1", rels.get(0).getPackageRelationship().getTargetURI().getFragment());
  125. }
  126. /**
  127. * Excel will sometimes write a button with a textbox
  128. * containing &gt;br&lt; (not closed!).
  129. * Clearly Excel shouldn't do this, but test that we can
  130. * read the file despite the naughtyness
  131. */
  132. public void test49020() throws Exception {
  133. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("BrNotClosed.xlsx");
  134. }
  135. /**
  136. * ensure that CTPhoneticPr is loaded by the ooxml test suite so that it is included in poi-ooxml-schemas
  137. */
  138. public void test49325() throws Exception {
  139. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49325.xlsx");
  140. CTWorksheet sh = wb.getSheetAt(0).getCTWorksheet();
  141. assertNotNull(sh.getPhoneticPr());
  142. }
  143. /**
  144. * Names which are defined with a Sheet
  145. * should return that sheet index properly
  146. */
  147. public void test48923() throws Exception {
  148. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48923.xlsx");
  149. assertEquals(4, wb.getNumberOfNames());
  150. Name b1 = wb.getName("NameB1");
  151. Name b2 = wb.getName("NameB2");
  152. Name sheet2 = wb.getName("NameSheet2");
  153. Name test = wb.getName("Test");
  154. assertNotNull(b1);
  155. assertEquals("NameB1", b1.getNameName());
  156. assertEquals("Sheet1", b1.getSheetName());
  157. assertEquals(-1, b1.getSheetIndex());
  158. assertNotNull(b2);
  159. assertEquals("NameB2", b2.getNameName());
  160. assertEquals("Sheet1", b2.getSheetName());
  161. assertEquals(-1, b2.getSheetIndex());
  162. assertNotNull(sheet2);
  163. assertEquals("NameSheet2", sheet2.getNameName());
  164. assertEquals("Sheet2", sheet2.getSheetName());
  165. assertEquals(-1, sheet2.getSheetIndex());
  166. assertNotNull(test);
  167. assertEquals("Test", test.getNameName());
  168. assertEquals("Sheet1", test.getSheetName());
  169. assertEquals(-1, test.getSheetIndex());
  170. }
  171. /**
  172. * Problem with evaluation formulas due to
  173. * NameXPtgs.
  174. * Blows up on:
  175. * IF(B6= (ROUNDUP(B6,0) + ROUNDDOWN(B6,0))/2, MROUND(B6,2),ROUND(B6,0))
  176. *
  177. * TODO: delete this test case when MROUND and VAR are implemented
  178. */
  179. public void test48539() throws Exception {
  180. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48539.xlsx");
  181. assertEquals(3, wb.getNumberOfSheets());
  182. // Try each cell individually
  183. XSSFFormulaEvaluator eval = new XSSFFormulaEvaluator(wb);
  184. for(int i=0; i<wb.getNumberOfSheets(); i++) {
  185. Sheet s = wb.getSheetAt(i);
  186. for(Row r : s) {
  187. for(Cell c : r) {
  188. if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
  189. CellValue cv = eval.evaluate(c);
  190. if(cv.getCellType() == Cell.CELL_TYPE_NUMERIC) {
  191. // assert that the calculated value agrees with
  192. // the cached formula result calculated by Excel
  193. double cachedFormulaResult = c.getNumericCellValue();
  194. double evaluatedFormulaResult = cv.getNumberValue();
  195. assertEquals(c.getCellFormula(), cachedFormulaResult, evaluatedFormulaResult, 1E-7);
  196. }
  197. }
  198. }
  199. }
  200. }
  201. // Now all of them
  202. XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  203. }
  204. /**
  205. * Foreground colours should be found even if
  206. * a theme is used
  207. */
  208. public void test48779() throws Exception {
  209. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48779.xlsx");
  210. XSSFCell cell = wb.getSheetAt(0).getRow(0).getCell(0);
  211. XSSFCellStyle cs = cell.getCellStyle();
  212. assertNotNull(cs);
  213. assertEquals(1, cs.getIndex());
  214. // Look at the low level xml elements
  215. assertEquals(2, cs.getCoreXf().getFillId());
  216. assertEquals(0, cs.getCoreXf().getXfId());
  217. assertEquals(true, cs.getCoreXf().getApplyFill());
  218. XSSFCellFill fg = wb.getStylesSource().getFillAt(2);
  219. assertEquals(0, fg.getFillForegroundColor().getIndexed());
  220. assertEquals(0.0, fg.getFillForegroundColor().getTint());
  221. assertEquals("FFFF0000", fg.getFillForegroundColor().getARGBHex());
  222. assertEquals(64, fg.getFillBackgroundColor().getIndexed());
  223. // Now look higher up
  224. assertNotNull(cs.getFillForegroundXSSFColor());
  225. assertEquals(0, cs.getFillForegroundColor());
  226. assertEquals("FFFF0000", cs.getFillForegroundXSSFColor().getARGBHex());
  227. assertEquals("FFFF0000", cs.getFillForegroundColorColor().getARGBHex());
  228. assertNotNull(cs.getFillBackgroundColor());
  229. assertEquals(64, cs.getFillBackgroundColor());
  230. assertEquals(null, cs.getFillBackgroundXSSFColor().getARGBHex());
  231. assertEquals(null, cs.getFillBackgroundColorColor().getARGBHex());
  232. }
  233. /**
  234. * With HSSF, if you create a font, don't change it, and
  235. * create a 2nd, you really do get two fonts that you
  236. * can alter as and when you want.
  237. * With XSSF, that wasn't the case, but this verfies
  238. * that it now is again
  239. */
  240. public void test48718() throws Exception {
  241. // Verify the HSSF behaviour
  242. // Then ensure the same for XSSF
  243. Workbook[] wbs = new Workbook[] {
  244. new HSSFWorkbook(),
  245. new XSSFWorkbook()
  246. };
  247. int[] initialFonts = new int[] { 4, 1 };
  248. for(int i=0; i<wbs.length; i++) {
  249. Workbook wb = wbs[i];
  250. int startingFonts = initialFonts[i];
  251. assertEquals(startingFonts, wb.getNumberOfFonts());
  252. // Get a font, and slightly change it
  253. Font a = wb.createFont();
  254. assertEquals(startingFonts+1, wb.getNumberOfFonts());
  255. a.setFontHeightInPoints((short)23);
  256. assertEquals(startingFonts+1, wb.getNumberOfFonts());
  257. // Get two more, unchanged
  258. Font b = wb.createFont();
  259. assertEquals(startingFonts+2, wb.getNumberOfFonts());
  260. Font c = wb.createFont();
  261. assertEquals(startingFonts+3, wb.getNumberOfFonts());
  262. }
  263. }
  264. /**
  265. * Ensure General and @ format are working properly
  266. * for integers
  267. */
  268. public void test47490() throws Exception {
  269. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("GeneralFormatTests.xlsx");
  270. Sheet s = wb.getSheetAt(1);
  271. Row r;
  272. DataFormatter df = new DataFormatter();
  273. r = s.getRow(1);
  274. assertEquals(1.0, r.getCell(2).getNumericCellValue());
  275. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  276. assertEquals("1", df.formatCellValue(r.getCell(2)));
  277. assertEquals("1", df.formatRawCellContents(1.0, -1, "@"));
  278. assertEquals("1", df.formatRawCellContents(1.0, -1, "General"));
  279. r = s.getRow(2);
  280. assertEquals(12.0, r.getCell(2).getNumericCellValue());
  281. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  282. assertEquals("12", df.formatCellValue(r.getCell(2)));
  283. assertEquals("12", df.formatRawCellContents(12.0, -1, "@"));
  284. assertEquals("12", df.formatRawCellContents(12.0, -1, "General"));
  285. r = s.getRow(3);
  286. assertEquals(123.0, r.getCell(2).getNumericCellValue());
  287. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  288. assertEquals("123", df.formatCellValue(r.getCell(2)));
  289. assertEquals("123", df.formatRawCellContents(123.0, -1, "@"));
  290. assertEquals("123", df.formatRawCellContents(123.0, -1, "General"));
  291. }
  292. /**
  293. * Ensures that XSSF and HSSF agree with each other,
  294. * and with the docs on when fetching the wrong
  295. * kind of value from a Formula cell
  296. */
  297. public void test47815() {
  298. Workbook[] wbs = new Workbook[] {
  299. new HSSFWorkbook(),
  300. new XSSFWorkbook()
  301. };
  302. for(Workbook wb : wbs) {
  303. Sheet s = wb.createSheet();
  304. Row r = s.createRow(0);
  305. // Setup
  306. Cell cn = r.createCell(0, Cell.CELL_TYPE_NUMERIC);
  307. cn.setCellValue(1.2);
  308. Cell cs = r.createCell(1, Cell.CELL_TYPE_STRING);
  309. cs.setCellValue("Testing");
  310. Cell cfn = r.createCell(2, Cell.CELL_TYPE_FORMULA);
  311. cfn.setCellFormula("A1");
  312. Cell cfs = r.createCell(3, Cell.CELL_TYPE_FORMULA);
  313. cfs.setCellFormula("B1");
  314. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  315. assertEquals(Cell.CELL_TYPE_NUMERIC, fe.evaluate(cfn).getCellType());
  316. assertEquals(Cell.CELL_TYPE_STRING, fe.evaluate(cfs).getCellType());
  317. fe.evaluateFormulaCell(cfn);
  318. fe.evaluateFormulaCell(cfs);
  319. // Now test
  320. assertEquals(Cell.CELL_TYPE_NUMERIC, cn.getCellType());
  321. assertEquals(Cell.CELL_TYPE_STRING, cs.getCellType());
  322. assertEquals(Cell.CELL_TYPE_FORMULA, cfn.getCellType());
  323. assertEquals(Cell.CELL_TYPE_NUMERIC, cfn.getCachedFormulaResultType());
  324. assertEquals(Cell.CELL_TYPE_FORMULA, cfs.getCellType());
  325. assertEquals(Cell.CELL_TYPE_STRING, cfs.getCachedFormulaResultType());
  326. // Different ways of retrieving
  327. assertEquals(1.2, cn.getNumericCellValue());
  328. try {
  329. cn.getRichStringCellValue();
  330. fail();
  331. } catch(IllegalStateException e) {}
  332. assertEquals("Testing", cs.getStringCellValue());
  333. try {
  334. cs.getNumericCellValue();
  335. fail();
  336. } catch(IllegalStateException e) {}
  337. assertEquals(1.2, cfn.getNumericCellValue());
  338. try {
  339. cfn.getRichStringCellValue();
  340. fail();
  341. } catch(IllegalStateException e) {}
  342. assertEquals("Testing", cfs.getStringCellValue());
  343. try {
  344. cfs.getNumericCellValue();
  345. fail();
  346. } catch(IllegalStateException e) {}
  347. }
  348. }
  349. /**
  350. * A problem file from a non-standard source (a scientific instrument that saves its
  351. * output as an .xlsx file) that have two issues:
  352. * 1. The Content Type part name is lower-case: [content_types].xml
  353. * 2. The file appears to use backslashes as path separators
  354. *
  355. * The OPC spec tolerates both of these peculiarities, so does POI
  356. */
  357. public void test49609() throws Exception {
  358. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49609.xlsx");
  359. assertEquals("FAM", wb.getSheetName(0));
  360. assertEquals("Cycle", wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
  361. }
  362. public void test49783() throws Exception {
  363. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49783.xlsx");
  364. Sheet sheet = wb.getSheetAt(0);
  365. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  366. Cell cell;
  367. cell = sheet.getRow(0).getCell(0);
  368. assertEquals("#REF!*#REF!", cell.getCellFormula());
  369. assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType());
  370. assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
  371. Name nm1 = wb.getName("sale_1");
  372. assertNotNull("name sale_1 should be present", nm1);
  373. assertEquals("Sheet1!#REF!", nm1.getRefersToFormula());
  374. Name nm2 = wb.getName("sale_2");
  375. assertNotNull("name sale_2 should be present", nm2);
  376. assertEquals("Sheet1!#REF!", nm2.getRefersToFormula());
  377. cell = sheet.getRow(1).getCell(0);
  378. assertEquals("sale_1*sale_2", cell.getCellFormula());
  379. assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType());
  380. assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
  381. }
  382. /**
  383. * Creating a rich string of "hello world" and applying
  384. * a font to characters 1-5 means we have two strings,
  385. * "hello" and " world". As such, we need to apply
  386. * preserve spaces to the 2nd bit, lest we end up
  387. * with something like "helloworld" !
  388. */
  389. public void test49941() throws Exception {
  390. XSSFWorkbook wb = new XSSFWorkbook();
  391. XSSFSheet s = wb.createSheet();
  392. XSSFRow r = s.createRow(0);
  393. XSSFCell c = r.createCell(0);
  394. // First without fonts
  395. c.setCellValue(
  396. new XSSFRichTextString(" with spaces ")
  397. );
  398. assertEquals(" with spaces ", c.getRichStringCellValue().toString());
  399. assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  400. assertEquals(true, c.getRichStringCellValue().getCTRst().isSetT());
  401. // Should have the preserve set
  402. assertEquals(
  403. 1,
  404. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength()
  405. );
  406. assertEquals(
  407. "preserve",
  408. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().item(0).getNodeValue()
  409. );
  410. // Save and check
  411. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  412. s = wb.getSheetAt(0);
  413. r = s.getRow(0);
  414. c = r.getCell(0);
  415. assertEquals(" with spaces ", c.getRichStringCellValue().toString());
  416. assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  417. assertEquals(true, c.getRichStringCellValue().getCTRst().isSetT());
  418. // Change the string
  419. c.setCellValue(
  420. new XSSFRichTextString("hello world")
  421. );
  422. assertEquals("hello world", c.getRichStringCellValue().toString());
  423. // Won't have preserve
  424. assertEquals(
  425. 0,
  426. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength()
  427. );
  428. // Apply a font
  429. XSSFFont f = wb.createFont();
  430. f.setBold(true);
  431. c.getRichStringCellValue().applyFont(0, 5, f);
  432. assertEquals("hello world", c.getRichStringCellValue().toString());
  433. // Does need preserving on the 2nd part
  434. assertEquals(2, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  435. assertEquals(
  436. 0,
  437. c.getRichStringCellValue().getCTRst().getRArray(0).xgetT().getDomNode().getAttributes().getLength()
  438. );
  439. assertEquals(
  440. 1,
  441. c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().getLength()
  442. );
  443. assertEquals(
  444. "preserve",
  445. c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().item(0).getNodeValue()
  446. );
  447. // Save and check
  448. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  449. s = wb.getSheetAt(0);
  450. r = s.getRow(0);
  451. c = r.getCell(0);
  452. assertEquals("hello world", c.getRichStringCellValue().toString());
  453. }
  454. /**
  455. * Repeatedly writing the same file which has styles
  456. * TODO Currently failing
  457. */
  458. public void DISABLEDtest49940() throws Exception {
  459. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("styles.xlsx");
  460. assertEquals(3, wb.getNumberOfSheets());
  461. assertEquals(10, wb.getStylesSource().getNumCellStyles());
  462. ByteArrayOutputStream b1 = new ByteArrayOutputStream();
  463. ByteArrayOutputStream b2 = new ByteArrayOutputStream();
  464. ByteArrayOutputStream b3 = new ByteArrayOutputStream();
  465. wb.write(b1);
  466. wb.write(b2);
  467. wb.write(b3);
  468. for(byte[] data : new byte[][] {
  469. b1.toByteArray(), b2.toByteArray(), b3.toByteArray()
  470. }) {
  471. ByteArrayInputStream bais = new ByteArrayInputStream(data);
  472. wb = new XSSFWorkbook(bais);
  473. assertEquals(3, wb.getNumberOfSheets());
  474. assertEquals(10, wb.getStylesSource().getNumCellStyles());
  475. }
  476. }
  477. /**
  478. * Various ways of removing a cell formula should all zap
  479. * the calcChain entry.
  480. */
  481. public void test49966() throws Exception {
  482. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("shared_formulas.xlsx");
  483. XSSFSheet sheet = wb.getSheetAt(0);
  484. // CalcChain has lots of entries
  485. CalculationChain cc = wb.getCalculationChain();
  486. assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
  487. assertEquals("A3", cc.getCTCalcChain().getCArray(1).getR());
  488. assertEquals("A4", cc.getCTCalcChain().getCArray(2).getR());
  489. assertEquals("A5", cc.getCTCalcChain().getCArray(3).getR());
  490. assertEquals("A6", cc.getCTCalcChain().getCArray(4).getR());
  491. assertEquals("A7", cc.getCTCalcChain().getCArray(5).getR());
  492. assertEquals("A8", cc.getCTCalcChain().getCArray(6).getR());
  493. assertEquals(40, cc.getCTCalcChain().sizeOfCArray());
  494. // Try various ways of changing the formulas
  495. // If it stays a formula, chain entry should remain
  496. // Otherwise should go
  497. sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay
  498. sheet.getRow(2).getCell(0).setCellFormula(null); // go
  499. sheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_FORMULA); // stay
  500. sheet.getRow(4).getCell(0).setCellType(Cell.CELL_TYPE_STRING); // go
  501. sheet.getRow(5).removeCell(
  502. sheet.getRow(5).getCell(0) // go
  503. );
  504. sheet.getRow(6).getCell(0).setCellType(Cell.CELL_TYPE_BLANK); // go
  505. sheet.getRow(7).getCell(0).setCellValue((String)null); // go
  506. // Save and check
  507. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  508. assertEquals(35, cc.getCTCalcChain().sizeOfCArray());
  509. cc = wb.getCalculationChain();
  510. assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
  511. assertEquals("A4", cc.getCTCalcChain().getCArray(1).getR());
  512. assertEquals("A9", cc.getCTCalcChain().getCArray(2).getR());
  513. }
  514. public void test49156() throws Exception {
  515. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49156.xlsx");
  516. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  517. Sheet sheet = wb.getSheetAt(0);
  518. for(Row row : sheet){
  519. for(Cell cell : row){
  520. if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
  521. formulaEvaluator.evaluateInCell(cell); // caused NPE on some cells
  522. }
  523. }
  524. }
  525. }
  526. }