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 49KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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.BaseTestBugzillaIssues;
  25. import org.apache.poi.ss.usermodel.Cell;
  26. import org.apache.poi.ss.usermodel.CellStyle;
  27. import org.apache.poi.ss.usermodel.CellValue;
  28. import org.apache.poi.ss.usermodel.ClientAnchor;
  29. import org.apache.poi.ss.usermodel.Comment;
  30. import org.apache.poi.ss.usermodel.CreationHelper;
  31. import org.apache.poi.ss.usermodel.DataFormatter;
  32. import org.apache.poi.ss.usermodel.Drawing;
  33. import org.apache.poi.ss.usermodel.Font;
  34. import org.apache.poi.ss.usermodel.FormulaError;
  35. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  36. import org.apache.poi.ss.usermodel.IndexedColors;
  37. import org.apache.poi.ss.usermodel.Name;
  38. import org.apache.poi.ss.usermodel.Row;
  39. import org.apache.poi.ss.usermodel.Sheet;
  40. import org.apache.poi.ss.usermodel.Workbook;
  41. import org.apache.poi.ss.util.AreaReference;
  42. import org.apache.poi.xssf.XSSFITestDataProvider;
  43. import org.apache.poi.xssf.XSSFTestDataSamples;
  44. import org.apache.poi.xssf.model.CalculationChain;
  45. import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
  46. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols;
  47. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
  48. public final class TestXSSFBugs extends BaseTestBugzillaIssues {
  49. public TestXSSFBugs() {
  50. super(XSSFITestDataProvider.instance);
  51. }
  52. /**
  53. * test writing a file with large number of unique strings,
  54. * open resulting file in Excel to check results!
  55. */
  56. public void test15375_2() {
  57. baseTest15375(1000);
  58. }
  59. /**
  60. * Named ranges had the right reference, but
  61. * the wrong sheet name
  62. */
  63. public void test45430() {
  64. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("45430.xlsx");
  65. assertFalse(wb.isMacroEnabled());
  66. assertEquals(3, wb.getNumberOfNames());
  67. assertEquals(0, wb.getNameAt(0).getCTName().getLocalSheetId());
  68. assertFalse(wb.getNameAt(0).getCTName().isSetLocalSheetId());
  69. assertEquals("SheetA!$A$1", wb.getNameAt(0).getRefersToFormula());
  70. assertEquals("SheetA", wb.getNameAt(0).getSheetName());
  71. assertEquals(0, wb.getNameAt(1).getCTName().getLocalSheetId());
  72. assertFalse(wb.getNameAt(1).getCTName().isSetLocalSheetId());
  73. assertEquals("SheetB!$A$1", wb.getNameAt(1).getRefersToFormula());
  74. assertEquals("SheetB", wb.getNameAt(1).getSheetName());
  75. assertEquals(0, wb.getNameAt(2).getCTName().getLocalSheetId());
  76. assertFalse(wb.getNameAt(2).getCTName().isSetLocalSheetId());
  77. assertEquals("SheetC!$A$1", wb.getNameAt(2).getRefersToFormula());
  78. assertEquals("SheetC", wb.getNameAt(2).getSheetName());
  79. // Save and re-load, still there
  80. XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  81. assertEquals(3, nwb.getNumberOfNames());
  82. assertEquals("SheetA!$A$1", nwb.getNameAt(0).getRefersToFormula());
  83. }
  84. /**
  85. * We should carry vba macros over after save
  86. */
  87. public void test45431() throws Exception {
  88. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("45431.xlsm");
  89. OPCPackage pkg = wb.getPackage();
  90. assertTrue(wb.isMacroEnabled());
  91. // Check the various macro related bits can be found
  92. PackagePart vba = pkg.getPart(
  93. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  94. );
  95. assertNotNull(vba);
  96. // And the drawing bit
  97. PackagePart drw = pkg.getPart(
  98. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  99. );
  100. assertNotNull(drw);
  101. // Save and re-open, both still there
  102. XSSFWorkbook nwb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  103. OPCPackage nPkg = nwb.getPackage();
  104. assertTrue(nwb.isMacroEnabled());
  105. vba = nPkg.getPart(
  106. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  107. );
  108. assertNotNull(vba);
  109. drw = nPkg.getPart(
  110. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  111. );
  112. assertNotNull(drw);
  113. // And again, just to be sure
  114. nwb = XSSFTestDataSamples.writeOutAndReadBack(nwb);
  115. nPkg = nwb.getPackage();
  116. assertTrue(nwb.isMacroEnabled());
  117. vba = nPkg.getPart(
  118. PackagingURIHelper.createPartName("/xl/vbaProject.bin")
  119. );
  120. assertNotNull(vba);
  121. drw = nPkg.getPart(
  122. PackagingURIHelper.createPartName("/xl/drawings/vmlDrawing1.vml")
  123. );
  124. assertNotNull(drw);
  125. }
  126. public void test47504() {
  127. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("47504.xlsx");
  128. assertEquals(1, wb.getNumberOfSheets());
  129. XSSFSheet sh = wb.getSheetAt(0);
  130. XSSFDrawing drawing = sh.createDrawingPatriarch();
  131. List<POIXMLDocumentPart> rels = drawing.getRelations();
  132. assertEquals(1, rels.size());
  133. assertEquals("Sheet1!A1", rels.get(0).getPackageRelationship().getTargetURI().getFragment());
  134. // And again, just to be sure
  135. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  136. assertEquals(1, wb.getNumberOfSheets());
  137. sh = wb.getSheetAt(0);
  138. drawing = sh.createDrawingPatriarch();
  139. rels = drawing.getRelations();
  140. assertEquals(1, rels.size());
  141. assertEquals("Sheet1!A1", rels.get(0).getPackageRelationship().getTargetURI().getFragment());
  142. }
  143. /**
  144. * Excel will sometimes write a button with a textbox
  145. * containing &gt;br&lt; (not closed!).
  146. * Clearly Excel shouldn't do this, but test that we can
  147. * read the file despite the naughtyness
  148. */
  149. public void test49020() throws Exception {
  150. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("BrNotClosed.xlsx");
  151. }
  152. /**
  153. * ensure that CTPhoneticPr is loaded by the ooxml test suite so that it is included in poi-ooxml-schemas
  154. */
  155. public void test49325() throws Exception {
  156. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49325.xlsx");
  157. CTWorksheet sh = wb.getSheetAt(0).getCTWorksheet();
  158. assertNotNull(sh.getPhoneticPr());
  159. }
  160. /**
  161. * Names which are defined with a Sheet
  162. * should return that sheet index properly
  163. */
  164. public void test48923() throws Exception {
  165. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48923.xlsx");
  166. assertEquals(4, wb.getNumberOfNames());
  167. Name b1 = wb.getName("NameB1");
  168. Name b2 = wb.getName("NameB2");
  169. Name sheet2 = wb.getName("NameSheet2");
  170. Name test = wb.getName("Test");
  171. assertNotNull(b1);
  172. assertEquals("NameB1", b1.getNameName());
  173. assertEquals("Sheet1", b1.getSheetName());
  174. assertEquals(-1, b1.getSheetIndex());
  175. assertNotNull(b2);
  176. assertEquals("NameB2", b2.getNameName());
  177. assertEquals("Sheet1", b2.getSheetName());
  178. assertEquals(-1, b2.getSheetIndex());
  179. assertNotNull(sheet2);
  180. assertEquals("NameSheet2", sheet2.getNameName());
  181. assertEquals("Sheet2", sheet2.getSheetName());
  182. assertEquals(-1, sheet2.getSheetIndex());
  183. assertNotNull(test);
  184. assertEquals("Test", test.getNameName());
  185. assertEquals("Sheet1", test.getSheetName());
  186. assertEquals(-1, test.getSheetIndex());
  187. }
  188. /**
  189. * Problem with evaluation formulas due to
  190. * NameXPtgs.
  191. * Blows up on:
  192. * IF(B6= (ROUNDUP(B6,0) + ROUNDDOWN(B6,0))/2, MROUND(B6,2),ROUND(B6,0))
  193. *
  194. * TODO: delete this test case when MROUND and VAR are implemented
  195. */
  196. public void test48539() throws Exception {
  197. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48539.xlsx");
  198. assertEquals(3, wb.getNumberOfSheets());
  199. // Try each cell individually
  200. XSSFFormulaEvaluator eval = new XSSFFormulaEvaluator(wb);
  201. for(int i=0; i<wb.getNumberOfSheets(); i++) {
  202. Sheet s = wb.getSheetAt(i);
  203. for(Row r : s) {
  204. for(Cell c : r) {
  205. if(c.getCellType() == Cell.CELL_TYPE_FORMULA) {
  206. CellValue cv = eval.evaluate(c);
  207. if(cv.getCellType() == Cell.CELL_TYPE_NUMERIC) {
  208. // assert that the calculated value agrees with
  209. // the cached formula result calculated by Excel
  210. double cachedFormulaResult = c.getNumericCellValue();
  211. double evaluatedFormulaResult = cv.getNumberValue();
  212. assertEquals(c.getCellFormula(), cachedFormulaResult, evaluatedFormulaResult, 1E-7);
  213. }
  214. }
  215. }
  216. }
  217. }
  218. // Now all of them
  219. XSSFFormulaEvaluator.evaluateAllFormulaCells(wb);
  220. }
  221. /**
  222. * Foreground colours should be found even if
  223. * a theme is used
  224. */
  225. public void test48779() throws Exception {
  226. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48779.xlsx");
  227. XSSFCell cell = wb.getSheetAt(0).getRow(0).getCell(0);
  228. XSSFCellStyle cs = cell.getCellStyle();
  229. assertNotNull(cs);
  230. assertEquals(1, cs.getIndex());
  231. // Look at the low level xml elements
  232. assertEquals(2, cs.getCoreXf().getFillId());
  233. assertEquals(0, cs.getCoreXf().getXfId());
  234. assertEquals(true, cs.getCoreXf().getApplyFill());
  235. XSSFCellFill fg = wb.getStylesSource().getFillAt(2);
  236. assertEquals(0, fg.getFillForegroundColor().getIndexed());
  237. assertEquals(0.0, fg.getFillForegroundColor().getTint());
  238. assertEquals("FFFF0000", fg.getFillForegroundColor().getARGBHex());
  239. assertEquals(64, fg.getFillBackgroundColor().getIndexed());
  240. // Now look higher up
  241. assertNotNull(cs.getFillForegroundXSSFColor());
  242. assertEquals(0, cs.getFillForegroundColor());
  243. assertEquals("FFFF0000", cs.getFillForegroundXSSFColor().getARGBHex());
  244. assertEquals("FFFF0000", cs.getFillForegroundColorColor().getARGBHex());
  245. assertNotNull(cs.getFillBackgroundColor());
  246. assertEquals(64, cs.getFillBackgroundColor());
  247. assertEquals(null, cs.getFillBackgroundXSSFColor().getARGBHex());
  248. assertEquals(null, cs.getFillBackgroundColorColor().getARGBHex());
  249. }
  250. /**
  251. * With HSSF, if you create a font, don't change it, and
  252. * create a 2nd, you really do get two fonts that you
  253. * can alter as and when you want.
  254. * With XSSF, that wasn't the case, but this verfies
  255. * that it now is again
  256. */
  257. public void test48718() throws Exception {
  258. // Verify the HSSF behaviour
  259. // Then ensure the same for XSSF
  260. Workbook[] wbs = new Workbook[] {
  261. new HSSFWorkbook(),
  262. new XSSFWorkbook()
  263. };
  264. int[] initialFonts = new int[] { 4, 1 };
  265. for(int i=0; i<wbs.length; i++) {
  266. Workbook wb = wbs[i];
  267. int startingFonts = initialFonts[i];
  268. assertEquals(startingFonts, wb.getNumberOfFonts());
  269. // Get a font, and slightly change it
  270. Font a = wb.createFont();
  271. assertEquals(startingFonts+1, wb.getNumberOfFonts());
  272. a.setFontHeightInPoints((short)23);
  273. assertEquals(startingFonts+1, wb.getNumberOfFonts());
  274. // Get two more, unchanged
  275. Font b = wb.createFont();
  276. assertEquals(startingFonts+2, wb.getNumberOfFonts());
  277. Font c = wb.createFont();
  278. assertEquals(startingFonts+3, wb.getNumberOfFonts());
  279. }
  280. }
  281. /**
  282. * Ensure General and @ format are working properly
  283. * for integers
  284. */
  285. public void test47490() throws Exception {
  286. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("GeneralFormatTests.xlsx");
  287. Sheet s = wb.getSheetAt(1);
  288. Row r;
  289. DataFormatter df = new DataFormatter();
  290. r = s.getRow(1);
  291. assertEquals(1.0, r.getCell(2).getNumericCellValue());
  292. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  293. assertEquals("1", df.formatCellValue(r.getCell(2)));
  294. assertEquals("1", df.formatRawCellContents(1.0, -1, "@"));
  295. assertEquals("1", df.formatRawCellContents(1.0, -1, "General"));
  296. r = s.getRow(2);
  297. assertEquals(12.0, r.getCell(2).getNumericCellValue());
  298. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  299. assertEquals("12", df.formatCellValue(r.getCell(2)));
  300. assertEquals("12", df.formatRawCellContents(12.0, -1, "@"));
  301. assertEquals("12", df.formatRawCellContents(12.0, -1, "General"));
  302. r = s.getRow(3);
  303. assertEquals(123.0, r.getCell(2).getNumericCellValue());
  304. assertEquals("General", r.getCell(2).getCellStyle().getDataFormatString());
  305. assertEquals("123", df.formatCellValue(r.getCell(2)));
  306. assertEquals("123", df.formatRawCellContents(123.0, -1, "@"));
  307. assertEquals("123", df.formatRawCellContents(123.0, -1, "General"));
  308. }
  309. /**
  310. * Ensures that XSSF and HSSF agree with each other,
  311. * and with the docs on when fetching the wrong
  312. * kind of value from a Formula cell
  313. */
  314. public void test47815() {
  315. Workbook[] wbs = new Workbook[] {
  316. new HSSFWorkbook(),
  317. new XSSFWorkbook()
  318. };
  319. for(Workbook wb : wbs) {
  320. Sheet s = wb.createSheet();
  321. Row r = s.createRow(0);
  322. // Setup
  323. Cell cn = r.createCell(0, Cell.CELL_TYPE_NUMERIC);
  324. cn.setCellValue(1.2);
  325. Cell cs = r.createCell(1, Cell.CELL_TYPE_STRING);
  326. cs.setCellValue("Testing");
  327. Cell cfn = r.createCell(2, Cell.CELL_TYPE_FORMULA);
  328. cfn.setCellFormula("A1");
  329. Cell cfs = r.createCell(3, Cell.CELL_TYPE_FORMULA);
  330. cfs.setCellFormula("B1");
  331. FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
  332. assertEquals(Cell.CELL_TYPE_NUMERIC, fe.evaluate(cfn).getCellType());
  333. assertEquals(Cell.CELL_TYPE_STRING, fe.evaluate(cfs).getCellType());
  334. fe.evaluateFormulaCell(cfn);
  335. fe.evaluateFormulaCell(cfs);
  336. // Now test
  337. assertEquals(Cell.CELL_TYPE_NUMERIC, cn.getCellType());
  338. assertEquals(Cell.CELL_TYPE_STRING, cs.getCellType());
  339. assertEquals(Cell.CELL_TYPE_FORMULA, cfn.getCellType());
  340. assertEquals(Cell.CELL_TYPE_NUMERIC, cfn.getCachedFormulaResultType());
  341. assertEquals(Cell.CELL_TYPE_FORMULA, cfs.getCellType());
  342. assertEquals(Cell.CELL_TYPE_STRING, cfs.getCachedFormulaResultType());
  343. // Different ways of retrieving
  344. assertEquals(1.2, cn.getNumericCellValue());
  345. try {
  346. cn.getRichStringCellValue();
  347. fail();
  348. } catch(IllegalStateException e) {}
  349. assertEquals("Testing", cs.getStringCellValue());
  350. try {
  351. cs.getNumericCellValue();
  352. fail();
  353. } catch(IllegalStateException e) {}
  354. assertEquals(1.2, cfn.getNumericCellValue());
  355. try {
  356. cfn.getRichStringCellValue();
  357. fail();
  358. } catch(IllegalStateException e) {}
  359. assertEquals("Testing", cfs.getStringCellValue());
  360. try {
  361. cfs.getNumericCellValue();
  362. fail();
  363. } catch(IllegalStateException e) {}
  364. }
  365. }
  366. /**
  367. * A problem file from a non-standard source (a scientific instrument that saves its
  368. * output as an .xlsx file) that have two issues:
  369. * 1. The Content Type part name is lower-case: [content_types].xml
  370. * 2. The file appears to use backslashes as path separators
  371. *
  372. * The OPC spec tolerates both of these peculiarities, so does POI
  373. */
  374. public void test49609() throws Exception {
  375. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("49609.xlsx");
  376. assertEquals("FAM", wb.getSheetName(0));
  377. assertEquals("Cycle", wb.getSheetAt(0).getRow(0).getCell(1).getStringCellValue());
  378. }
  379. public void test49783() throws Exception {
  380. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49783.xlsx");
  381. Sheet sheet = wb.getSheetAt(0);
  382. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  383. Cell cell;
  384. cell = sheet.getRow(0).getCell(0);
  385. assertEquals("#REF!*#REF!", cell.getCellFormula());
  386. assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType());
  387. assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
  388. Name nm1 = wb.getName("sale_1");
  389. assertNotNull("name sale_1 should be present", nm1);
  390. assertEquals("Sheet1!#REF!", nm1.getRefersToFormula());
  391. Name nm2 = wb.getName("sale_2");
  392. assertNotNull("name sale_2 should be present", nm2);
  393. assertEquals("Sheet1!#REF!", nm2.getRefersToFormula());
  394. cell = sheet.getRow(1).getCell(0);
  395. assertEquals("sale_1*sale_2", cell.getCellFormula());
  396. assertEquals(Cell.CELL_TYPE_ERROR, evaluator.evaluateInCell(cell).getCellType());
  397. assertEquals("#REF!", FormulaError.forInt(cell.getErrorCellValue()).getString());
  398. }
  399. /**
  400. * Creating a rich string of "hello world" and applying
  401. * a font to characters 1-5 means we have two strings,
  402. * "hello" and " world". As such, we need to apply
  403. * preserve spaces to the 2nd bit, lest we end up
  404. * with something like "helloworld" !
  405. */
  406. public void test49941() throws Exception {
  407. XSSFWorkbook wb = new XSSFWorkbook();
  408. XSSFSheet s = wb.createSheet();
  409. XSSFRow r = s.createRow(0);
  410. XSSFCell c = r.createCell(0);
  411. // First without fonts
  412. c.setCellValue(
  413. new XSSFRichTextString(" with spaces ")
  414. );
  415. assertEquals(" with spaces ", c.getRichStringCellValue().toString());
  416. assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  417. assertEquals(true, c.getRichStringCellValue().getCTRst().isSetT());
  418. // Should have the preserve set
  419. assertEquals(
  420. 1,
  421. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength()
  422. );
  423. assertEquals(
  424. "preserve",
  425. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().item(0).getNodeValue()
  426. );
  427. // Save and check
  428. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  429. s = wb.getSheetAt(0);
  430. r = s.getRow(0);
  431. c = r.getCell(0);
  432. assertEquals(" with spaces ", c.getRichStringCellValue().toString());
  433. assertEquals(0, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  434. assertEquals(true, c.getRichStringCellValue().getCTRst().isSetT());
  435. // Change the string
  436. c.setCellValue(
  437. new XSSFRichTextString("hello world")
  438. );
  439. assertEquals("hello world", c.getRichStringCellValue().toString());
  440. // Won't have preserve
  441. assertEquals(
  442. 0,
  443. c.getRichStringCellValue().getCTRst().xgetT().getDomNode().getAttributes().getLength()
  444. );
  445. // Apply a font
  446. XSSFFont f = wb.createFont();
  447. f.setBold(true);
  448. c.getRichStringCellValue().applyFont(0, 5, f);
  449. assertEquals("hello world", c.getRichStringCellValue().toString());
  450. // Does need preserving on the 2nd part
  451. assertEquals(2, c.getRichStringCellValue().getCTRst().sizeOfRArray());
  452. assertEquals(
  453. 0,
  454. c.getRichStringCellValue().getCTRst().getRArray(0).xgetT().getDomNode().getAttributes().getLength()
  455. );
  456. assertEquals(
  457. 1,
  458. c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().getLength()
  459. );
  460. assertEquals(
  461. "preserve",
  462. c.getRichStringCellValue().getCTRst().getRArray(1).xgetT().getDomNode().getAttributes().item(0).getNodeValue()
  463. );
  464. // Save and check
  465. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  466. s = wb.getSheetAt(0);
  467. r = s.getRow(0);
  468. c = r.getCell(0);
  469. assertEquals("hello world", c.getRichStringCellValue().toString());
  470. }
  471. /**
  472. * Repeatedly writing the same file which has styles
  473. * TODO Currently failing
  474. */
  475. public void DISABLEDtest49940() throws Exception {
  476. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("styles.xlsx");
  477. assertEquals(3, wb.getNumberOfSheets());
  478. assertEquals(10, wb.getStylesSource().getNumCellStyles());
  479. ByteArrayOutputStream b1 = new ByteArrayOutputStream();
  480. ByteArrayOutputStream b2 = new ByteArrayOutputStream();
  481. ByteArrayOutputStream b3 = new ByteArrayOutputStream();
  482. wb.write(b1);
  483. wb.write(b2);
  484. wb.write(b3);
  485. for(byte[] data : new byte[][] {
  486. b1.toByteArray(), b2.toByteArray(), b3.toByteArray()
  487. }) {
  488. ByteArrayInputStream bais = new ByteArrayInputStream(data);
  489. wb = new XSSFWorkbook(bais);
  490. assertEquals(3, wb.getNumberOfSheets());
  491. assertEquals(10, wb.getStylesSource().getNumCellStyles());
  492. }
  493. }
  494. /**
  495. * Various ways of removing a cell formula should all zap
  496. * the calcChain entry.
  497. */
  498. public void test49966() throws Exception {
  499. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("shared_formulas.xlsx");
  500. XSSFSheet sheet = wb.getSheetAt(0);
  501. // CalcChain has lots of entries
  502. CalculationChain cc = wb.getCalculationChain();
  503. assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
  504. assertEquals("A3", cc.getCTCalcChain().getCArray(1).getR());
  505. assertEquals("A4", cc.getCTCalcChain().getCArray(2).getR());
  506. assertEquals("A5", cc.getCTCalcChain().getCArray(3).getR());
  507. assertEquals("A6", cc.getCTCalcChain().getCArray(4).getR());
  508. assertEquals("A7", cc.getCTCalcChain().getCArray(5).getR());
  509. assertEquals("A8", cc.getCTCalcChain().getCArray(6).getR());
  510. assertEquals(40, cc.getCTCalcChain().sizeOfCArray());
  511. // Try various ways of changing the formulas
  512. // If it stays a formula, chain entry should remain
  513. // Otherwise should go
  514. sheet.getRow(1).getCell(0).setCellFormula("A1"); // stay
  515. sheet.getRow(2).getCell(0).setCellFormula(null); // go
  516. sheet.getRow(3).getCell(0).setCellType(Cell.CELL_TYPE_FORMULA); // stay
  517. sheet.getRow(4).getCell(0).setCellType(Cell.CELL_TYPE_STRING); // go
  518. sheet.getRow(5).removeCell(
  519. sheet.getRow(5).getCell(0) // go
  520. );
  521. sheet.getRow(6).getCell(0).setCellType(Cell.CELL_TYPE_BLANK); // go
  522. sheet.getRow(7).getCell(0).setCellValue((String)null); // go
  523. // Save and check
  524. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  525. assertEquals(35, cc.getCTCalcChain().sizeOfCArray());
  526. cc = wb.getCalculationChain();
  527. assertEquals("A2", cc.getCTCalcChain().getCArray(0).getR());
  528. assertEquals("A4", cc.getCTCalcChain().getCArray(1).getR());
  529. assertEquals("A9", cc.getCTCalcChain().getCArray(2).getR());
  530. }
  531. public void test49156() throws Exception {
  532. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("49156.xlsx");
  533. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  534. Sheet sheet = wb.getSheetAt(0);
  535. for(Row row : sheet){
  536. for(Cell cell : row){
  537. if(cell.getCellType() == Cell.CELL_TYPE_FORMULA){
  538. formulaEvaluator.evaluateInCell(cell); // caused NPE on some cells
  539. }
  540. }
  541. }
  542. }
  543. /**
  544. * Newlines are valid characters in a formula
  545. */
  546. public void test50440And51875() throws Exception {
  547. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("NewlineInFormulas.xlsx");
  548. Sheet s = wb.getSheetAt(0);
  549. Cell c = s.getRow(0).getCell(0);
  550. assertEquals("SUM(\n1,2\n)", c.getCellFormula());
  551. assertEquals(3.0, c.getNumericCellValue());
  552. FormulaEvaluator formulaEvaluator = wb.getCreationHelper().createFormulaEvaluator();
  553. formulaEvaluator.evaluateFormulaCell(c);
  554. assertEquals("SUM(\n1,2\n)", c.getCellFormula());
  555. assertEquals(3.0, c.getNumericCellValue());
  556. // For 51875
  557. Cell b3 = s.getRow(2).getCell(1);
  558. formulaEvaluator.evaluateFormulaCell(b3);
  559. assertEquals("B1+B2", b3.getCellFormula()); // The newline is lost for shared formulas
  560. assertEquals(3.0, b3.getNumericCellValue());
  561. }
  562. /**
  563. * Moving a cell comment from one cell to another
  564. */
  565. public void test50795() throws Exception {
  566. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50795.xlsx");
  567. XSSFSheet sheet = wb.getSheetAt(0);
  568. XSSFRow row = sheet.getRow(0);
  569. XSSFCell cellWith = row.getCell(0);
  570. XSSFCell cellWithoutComment = row.getCell(1);
  571. assertNotNull(cellWith.getCellComment());
  572. assertNull(cellWithoutComment.getCellComment());
  573. String exp = "\u0410\u0432\u0442\u043e\u0440:\ncomment";
  574. XSSFComment comment = cellWith.getCellComment();
  575. assertEquals(exp, comment.getString().getString());
  576. // Check we can write it out and read it back as-is
  577. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  578. sheet = wb.getSheetAt(0);
  579. row = sheet.getRow(0);
  580. cellWith = row.getCell(0);
  581. cellWithoutComment = row.getCell(1);
  582. // Double check things are as expected
  583. assertNotNull(cellWith.getCellComment());
  584. assertNull(cellWithoutComment.getCellComment());
  585. comment = cellWith.getCellComment();
  586. assertEquals(exp, comment.getString().getString());
  587. // Move the comment
  588. cellWithoutComment.setCellComment(comment);
  589. // Write out and re-check
  590. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  591. sheet = wb.getSheetAt(0);
  592. row = sheet.getRow(0);
  593. // Ensure it swapped over
  594. cellWith = row.getCell(0);
  595. cellWithoutComment = row.getCell(1);
  596. assertNull(cellWith.getCellComment());
  597. assertNotNull(cellWithoutComment.getCellComment());
  598. comment = cellWithoutComment.getCellComment();
  599. assertEquals(exp, comment.getString().getString());
  600. }
  601. /**
  602. * When the cell background colour is set with one of the first
  603. * two columns of the theme colour palette, the colours are
  604. * shades of white or black.
  605. * For those cases, ensure we don't break on reading the colour
  606. */
  607. public void test50299() throws Exception {
  608. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("50299.xlsx");
  609. // Check all the colours
  610. for(int sn=0; sn<wb.getNumberOfSheets(); sn++) {
  611. Sheet s = wb.getSheetAt(sn);
  612. for(Row r : s) {
  613. for(Cell c : r) {
  614. CellStyle cs = c.getCellStyle();
  615. if(cs != null) {
  616. cs.getFillForegroundColor();
  617. }
  618. }
  619. }
  620. }
  621. // Check one bit in detail
  622. // Check that we get back foreground=0 for the theme colours,
  623. // and background=64 for the auto colouring
  624. Sheet s = wb.getSheetAt(0);
  625. assertEquals(0, s.getRow(0).getCell(8).getCellStyle().getFillForegroundColor());
  626. assertEquals(64, s.getRow(0).getCell(8).getCellStyle().getFillBackgroundColor());
  627. assertEquals(0, s.getRow(1).getCell(8).getCellStyle().getFillForegroundColor());
  628. assertEquals(64, s.getRow(1).getCell(8).getCellStyle().getFillBackgroundColor());
  629. }
  630. /**
  631. * Excel .xls style indexed colours in a .xlsx file
  632. */
  633. public void test50786() throws Exception {
  634. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50786-indexed_colours.xlsx");
  635. XSSFSheet s = wb.getSheetAt(0);
  636. XSSFRow r = s.getRow(2);
  637. // Check we have the right cell
  638. XSSFCell c = r.getCell(1);
  639. assertEquals("test\u00a0", c.getRichStringCellValue().getString());
  640. // It should be light green
  641. XSSFCellStyle cs = c.getCellStyle();
  642. assertEquals(42, cs.getFillForegroundColor());
  643. assertEquals(42, cs.getFillForegroundColorColor().getIndexed());
  644. assertNotNull(cs.getFillForegroundColorColor().getRgb());
  645. assertEquals("FFCCFFCC", cs.getFillForegroundColorColor().getARGBHex());
  646. }
  647. /**
  648. * If the border colours are set with themes, then we
  649. * should still be able to get colours
  650. */
  651. public void test50846() throws Exception {
  652. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50846-border_colours.xlsx");
  653. XSSFSheet sheet = wb.getSheetAt(0);
  654. XSSFRow row = sheet.getRow(0);
  655. // Border from a theme, brown
  656. XSSFCell cellT = row.getCell(0);
  657. XSSFCellStyle styleT = cellT.getCellStyle();
  658. XSSFColor colorT = styleT.getBottomBorderXSSFColor();
  659. assertEquals(5, colorT.getTheme());
  660. assertEquals("FFC0504D", colorT.getARGBHex());
  661. // Border from a style direct, red
  662. XSSFCell cellS = row.getCell(1);
  663. XSSFCellStyle styleS = cellS.getCellStyle();
  664. XSSFColor colorS = styleS.getBottomBorderXSSFColor();
  665. assertEquals(0, colorS.getTheme());
  666. assertEquals("FFFF0000", colorS.getARGBHex());
  667. }
  668. /**
  669. * Fonts where their colours come from the theme rather
  670. * then being set explicitly still should allow the
  671. * fetching of the RGB.
  672. */
  673. public void test50784() throws Exception {
  674. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50784-font_theme_colours.xlsx");
  675. XSSFSheet s = wb.getSheetAt(0);
  676. XSSFRow r = s.getRow(0);
  677. // Column 1 has a font with regular colours
  678. XSSFCell cr = r.getCell(1);
  679. XSSFFont fr = wb.getFontAt( cr.getCellStyle().getFontIndex() );
  680. XSSFColor colr = fr.getXSSFColor();
  681. // No theme, has colours
  682. assertEquals(0, colr.getTheme());
  683. assertNotNull( colr.getRgb() );
  684. // Column 0 has a font with colours from a theme
  685. XSSFCell ct = r.getCell(0);
  686. XSSFFont ft = wb.getFontAt( ct.getCellStyle().getFontIndex() );
  687. XSSFColor colt = ft.getXSSFColor();
  688. // Has a theme, which has the colours on it
  689. assertEquals(9, colt.getTheme());
  690. XSSFColor themeC = wb.getTheme().getThemeColor(colt.getTheme());
  691. assertNotNull( themeC.getRgb() );
  692. assertNotNull( colt.getRgb() );
  693. assertEquals( themeC.getARGBHex(), colt.getARGBHex() ); // The same colour
  694. }
  695. /**
  696. * New lines were being eaten when setting a font on
  697. * a rich text string
  698. */
  699. public void test48877() throws Exception {
  700. String text = "Use \n with word wrap on to create a new line.\n" +
  701. "This line finishes with two trailing spaces. ";
  702. XSSFWorkbook wb = new XSSFWorkbook();
  703. XSSFSheet sheet = wb.createSheet();
  704. Font font1 = wb.createFont();
  705. font1.setColor((short) 20);
  706. Font font2 = wb.createFont();
  707. font2.setColor(Font.COLOR_RED);
  708. Font font3 = wb.getFontAt((short)0);
  709. XSSFRow row = sheet.createRow(2);
  710. XSSFCell cell = row.createCell(2);
  711. XSSFRichTextString richTextString =
  712. wb.getCreationHelper().createRichTextString(text);
  713. // Check the text has the newline
  714. assertEquals(text, richTextString.getString());
  715. // Apply the font
  716. richTextString.applyFont(font3);
  717. richTextString.applyFont(0, 3, font1);
  718. cell.setCellValue(richTextString);
  719. // To enable newlines you need set a cell styles with wrap=true
  720. CellStyle cs = wb.createCellStyle();
  721. cs.setWrapText(true);
  722. cell.setCellStyle(cs);
  723. // Check the text has the
  724. assertEquals(text, cell.getStringCellValue());
  725. // Save the file and re-read it
  726. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  727. sheet = wb.getSheetAt(0);
  728. row = sheet.getRow(2);
  729. cell = row.getCell(2);
  730. assertEquals(text, cell.getStringCellValue());
  731. // Now add a 2nd, and check again
  732. int fontAt = text.indexOf("\n", 6);
  733. cell.getRichStringCellValue().applyFont(10, fontAt+1, font2);
  734. assertEquals(text, cell.getStringCellValue());
  735. assertEquals(4, cell.getRichStringCellValue().numFormattingRuns());
  736. assertEquals("Use", cell.getRichStringCellValue().getCTRst().getRList().get(0).getT());
  737. String r3 = cell.getRichStringCellValue().getCTRst().getRList().get(2).getT();
  738. assertEquals("line.\n", r3.substring(r3.length()-6));
  739. // Save and re-check
  740. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  741. sheet = wb.getSheetAt(0);
  742. row = sheet.getRow(2);
  743. cell = row.getCell(2);
  744. assertEquals(text, cell.getStringCellValue());
  745. // FileOutputStream out = new FileOutputStream("/tmp/test48877.xlsx");
  746. // wb.write(out);
  747. // out.close();
  748. }
  749. /**
  750. * Adding sheets when one has a table, then re-ordering
  751. */
  752. public void test50867() throws Exception {
  753. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50867_with_table.xlsx");
  754. assertEquals(3, wb.getNumberOfSheets());
  755. XSSFSheet s1 = wb.getSheetAt(0);
  756. XSSFSheet s2 = wb.getSheetAt(1);
  757. XSSFSheet s3 = wb.getSheetAt(2);
  758. assertEquals(1, s1.getTables().size());
  759. assertEquals(0, s2.getTables().size());
  760. assertEquals(0, s3.getTables().size());
  761. XSSFTable t = s1.getTables().get(0);
  762. assertEquals("Tabella1", t.getName());
  763. assertEquals("Tabella1", t.getDisplayName());
  764. assertEquals("A1:C3", t.getCTTable().getRef());
  765. // Add a sheet and re-order
  766. XSSFSheet s4 = wb.createSheet("NewSheet");
  767. wb.setSheetOrder(s4.getSheetName(), 0);
  768. // Check on tables
  769. assertEquals(1, s1.getTables().size());
  770. assertEquals(0, s2.getTables().size());
  771. assertEquals(0, s3.getTables().size());
  772. assertEquals(0, s4.getTables().size());
  773. // Refetch to get the new order
  774. s1 = wb.getSheetAt(0);
  775. s2 = wb.getSheetAt(1);
  776. s3 = wb.getSheetAt(2);
  777. s4 = wb.getSheetAt(3);
  778. assertEquals(0, s1.getTables().size());
  779. assertEquals(1, s2.getTables().size());
  780. assertEquals(0, s3.getTables().size());
  781. assertEquals(0, s4.getTables().size());
  782. // Save and re-load
  783. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  784. s1 = wb.getSheetAt(0);
  785. s2 = wb.getSheetAt(1);
  786. s3 = wb.getSheetAt(2);
  787. s4 = wb.getSheetAt(3);
  788. assertEquals(0, s1.getTables().size());
  789. assertEquals(1, s2.getTables().size());
  790. assertEquals(0, s3.getTables().size());
  791. assertEquals(0, s4.getTables().size());
  792. t = s2.getTables().get(0);
  793. assertEquals("Tabella1", t.getName());
  794. assertEquals("Tabella1", t.getDisplayName());
  795. assertEquals("A1:C3", t.getCTTable().getRef());
  796. // Add some more tables, and check
  797. t = s2.createTable();
  798. t.setName("New 2");
  799. t.setDisplayName("New 2");
  800. t = s3.createTable();
  801. t.setName("New 3");
  802. t.setDisplayName("New 3");
  803. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  804. s1 = wb.getSheetAt(0);
  805. s2 = wb.getSheetAt(1);
  806. s3 = wb.getSheetAt(2);
  807. s4 = wb.getSheetAt(3);
  808. assertEquals(0, s1.getTables().size());
  809. assertEquals(2, s2.getTables().size());
  810. assertEquals(1, s3.getTables().size());
  811. assertEquals(0, s4.getTables().size());
  812. t = s2.getTables().get(0);
  813. assertEquals("Tabella1", t.getName());
  814. assertEquals("Tabella1", t.getDisplayName());
  815. assertEquals("A1:C3", t.getCTTable().getRef());
  816. t = s2.getTables().get(1);
  817. assertEquals("New 2", t.getName());
  818. assertEquals("New 2", t.getDisplayName());
  819. t = s3.getTables().get(0);
  820. assertEquals("New 3", t.getName());
  821. assertEquals("New 3", t.getDisplayName());
  822. // Check the relationships
  823. assertEquals(0, s1.getRelations().size());
  824. assertEquals(3, s2.getRelations().size());
  825. assertEquals(1, s3.getRelations().size());
  826. assertEquals(0, s4.getRelations().size());
  827. assertEquals(
  828. XSSFRelation.PRINTER_SETTINGS.getContentType(),
  829. s2.getRelations().get(0).getPackagePart().getContentType()
  830. );
  831. assertEquals(
  832. XSSFRelation.TABLE.getContentType(),
  833. s2.getRelations().get(1).getPackagePart().getContentType()
  834. );
  835. assertEquals(
  836. XSSFRelation.TABLE.getContentType(),
  837. s2.getRelations().get(2).getPackagePart().getContentType()
  838. );
  839. assertEquals(
  840. XSSFRelation.TABLE.getContentType(),
  841. s3.getRelations().get(0).getPackagePart().getContentType()
  842. );
  843. assertEquals(
  844. "/xl/tables/table3.xml",
  845. s3.getRelations().get(0).getPackagePart().getPartName().toString()
  846. );
  847. }
  848. /**
  849. * Setting repeating rows and columns shouldn't break
  850. * any print settings that were there before
  851. */
  852. public void test49253() throws Exception {
  853. XSSFWorkbook wb1 = new XSSFWorkbook();
  854. XSSFWorkbook wb2 = new XSSFWorkbook();
  855. // No print settings before repeating
  856. XSSFSheet s1 = wb1.createSheet();
  857. assertEquals(false, s1.getCTWorksheet().isSetPageSetup());
  858. assertEquals(true, s1.getCTWorksheet().isSetPageMargins());
  859. wb1.setRepeatingRowsAndColumns(0, 2, 3, 1, 2);
  860. assertEquals(true, s1.getCTWorksheet().isSetPageSetup());
  861. assertEquals(true, s1.getCTWorksheet().isSetPageMargins());
  862. XSSFPrintSetup ps1 = s1.getPrintSetup();
  863. assertEquals(false, ps1.getValidSettings());
  864. assertEquals(false, ps1.getLandscape());
  865. // Had valid print settings before repeating
  866. XSSFSheet s2 = wb2.createSheet();
  867. XSSFPrintSetup ps2 = s2.getPrintSetup();
  868. assertEquals(true, s2.getCTWorksheet().isSetPageSetup());
  869. assertEquals(true, s2.getCTWorksheet().isSetPageMargins());
  870. ps2.setLandscape(false);
  871. assertEquals(true, ps2.getValidSettings());
  872. assertEquals(false, ps2.getLandscape());
  873. wb2.setRepeatingRowsAndColumns(0, 2, 3, 1, 2);
  874. ps2 = s2.getPrintSetup();
  875. assertEquals(true, s2.getCTWorksheet().isSetPageSetup());
  876. assertEquals(true, s2.getCTWorksheet().isSetPageMargins());
  877. assertEquals(true, ps2.getValidSettings());
  878. assertEquals(false, ps2.getLandscape());
  879. }
  880. /**
  881. * Default Column style
  882. */
  883. public void test51037() throws Exception {
  884. XSSFWorkbook wb = new XSSFWorkbook();
  885. XSSFSheet s = wb.createSheet();
  886. CellStyle defaultStyle = wb.getCellStyleAt((short)0);
  887. assertEquals(0, defaultStyle.getIndex());
  888. CellStyle blueStyle = wb.createCellStyle();
  889. blueStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
  890. blueStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  891. assertEquals(1, blueStyle.getIndex());
  892. CellStyle pinkStyle = wb.createCellStyle();
  893. pinkStyle.setFillForegroundColor(IndexedColors.PINK.getIndex());
  894. pinkStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  895. assertEquals(2, pinkStyle.getIndex());
  896. // Starts empty
  897. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  898. CTCols cols = s.getCTWorksheet().getColsArray(0);
  899. assertEquals(0, cols.sizeOfColArray());
  900. // Add some rows and columns
  901. XSSFRow r1 = s.createRow(0);
  902. XSSFRow r2 = s.createRow(1);
  903. r1.createCell(0);
  904. r1.createCell(2);
  905. r2.createCell(0);
  906. r2.createCell(3);
  907. // Check no style is there
  908. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  909. assertEquals(0, cols.sizeOfColArray());
  910. assertEquals(defaultStyle, s.getColumnStyle(0));
  911. assertEquals(defaultStyle, s.getColumnStyle(2));
  912. assertEquals(defaultStyle, s.getColumnStyle(3));
  913. // Apply the styles
  914. s.setDefaultColumnStyle(0, pinkStyle);
  915. s.setDefaultColumnStyle(3, blueStyle);
  916. // Check
  917. assertEquals(pinkStyle, s.getColumnStyle(0));
  918. assertEquals(defaultStyle, s.getColumnStyle(2));
  919. assertEquals(blueStyle, s.getColumnStyle(3));
  920. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  921. assertEquals(2, cols.sizeOfColArray());
  922. assertEquals(1, cols.getColArray(0).getMin());
  923. assertEquals(1, cols.getColArray(0).getMax());
  924. assertEquals(pinkStyle.getIndex(), cols.getColArray(0).getStyle());
  925. assertEquals(4, cols.getColArray(1).getMin());
  926. assertEquals(4, cols.getColArray(1).getMax());
  927. assertEquals(blueStyle.getIndex(), cols.getColArray(1).getStyle());
  928. // Save, re-load and re-check
  929. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  930. s = wb.getSheetAt(0);
  931. defaultStyle = wb.getCellStyleAt(defaultStyle.getIndex());
  932. blueStyle = wb.getCellStyleAt(blueStyle.getIndex());
  933. pinkStyle = wb.getCellStyleAt(pinkStyle.getIndex());
  934. assertEquals(pinkStyle, s.getColumnStyle(0));
  935. assertEquals(defaultStyle, s.getColumnStyle(2));
  936. assertEquals(blueStyle, s.getColumnStyle(3));
  937. }
  938. /**
  939. * Repeatedly writing a file.
  940. * Something with the SharedStringsTable currently breaks...
  941. */
  942. public void DISABLEDtest46662() throws Exception {
  943. // New file
  944. XSSFWorkbook wb = new XSSFWorkbook();
  945. XSSFTestDataSamples.writeOutAndReadBack(wb);
  946. XSSFTestDataSamples.writeOutAndReadBack(wb);
  947. XSSFTestDataSamples.writeOutAndReadBack(wb);
  948. // Simple file
  949. wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
  950. XSSFTestDataSamples.writeOutAndReadBack(wb);
  951. XSSFTestDataSamples.writeOutAndReadBack(wb);
  952. XSSFTestDataSamples.writeOutAndReadBack(wb);
  953. // Complex file
  954. // TODO
  955. }
  956. /**
  957. * Colours and styles when the list has gaps in it
  958. */
  959. public void test51222() throws Exception {
  960. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51222.xlsx");
  961. XSSFSheet s = wb.getSheetAt(0);
  962. XSSFCell cA4_EEECE1 = s.getRow(3).getCell(0);
  963. XSSFCell cA5_1F497D = s.getRow(4).getCell(0);
  964. // Check the text
  965. assertEquals("A4", cA4_EEECE1.getRichStringCellValue().getString());
  966. assertEquals("A5", cA5_1F497D.getRichStringCellValue().getString());
  967. // Check the styles assigned to them
  968. assertEquals(4, cA4_EEECE1.getCTCell().getS());
  969. assertEquals(5, cA5_1F497D.getCTCell().getS());
  970. // Check we look up the correct style
  971. assertEquals(4, cA4_EEECE1.getCellStyle().getIndex());
  972. assertEquals(5, cA5_1F497D.getCellStyle().getIndex());
  973. // Check the fills on them at the low level
  974. assertEquals(5, cA4_EEECE1.getCellStyle().getCoreXf().getFillId());
  975. assertEquals(6, cA5_1F497D.getCellStyle().getCoreXf().getFillId());
  976. // These should reference themes 2 and 3
  977. assertEquals(2, wb.getStylesSource().getFillAt(5).getCTFill().getPatternFill().getFgColor().getTheme());
  978. assertEquals(3, wb.getStylesSource().getFillAt(6).getCTFill().getPatternFill().getFgColor().getTheme());
  979. // Ensure we get the right colours for these themes
  980. // TODO fix
  981. // assertEquals("FFEEECE1", wb.getTheme().getThemeColor(2).getARGBHex());
  982. // assertEquals("FF1F497D", wb.getTheme().getThemeColor(3).getARGBHex());
  983. // Finally check the colours on the styles
  984. // TODO fix
  985. // assertEquals("FFEEECE1", cA4_EEECE1.getCellStyle().getFillForegroundXSSFColor().getARGBHex());
  986. // assertEquals("FF1F497D", cA5_1F497D.getCellStyle().getFillForegroundXSSFColor().getARGBHex());
  987. }
  988. public void test51470() throws Exception {
  989. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51470.xlsx");
  990. XSSFSheet sh0 = wb.getSheetAt(0);
  991. XSSFSheet sh1 = wb.cloneSheet(0);
  992. List<POIXMLDocumentPart> rels0 = sh0.getRelations();
  993. List<POIXMLDocumentPart> rels1 = sh1.getRelations();
  994. assertEquals(1, rels0.size());
  995. assertEquals(1, rels1.size());
  996. assertEquals(rels0.get(0).getPackageRelationship(), rels1.get(0).getPackageRelationship());
  997. }
  998. /**
  999. * Add comments to Sheet 1, when Sheet 2 already has
  1000. * comments (so /xl/comments1.xml is taken)
  1001. */
  1002. public void test51850() {
  1003. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51850.xlsx");
  1004. XSSFSheet sh1 = wb.getSheetAt(0);
  1005. XSSFSheet sh2 = wb.getSheetAt(1);
  1006. // Sheet 2 has comments
  1007. assertNotNull(sh2.getCommentsTable(false));
  1008. assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
  1009. // Sheet 1 doesn't (yet)
  1010. assertNull(sh1.getCommentsTable(false));
  1011. // Try to add comments to Sheet 1
  1012. CreationHelper factory = wb.getCreationHelper();
  1013. Drawing drawing = sh1.createDrawingPatriarch();
  1014. ClientAnchor anchor = factory.createClientAnchor();
  1015. anchor.setCol1(0);
  1016. anchor.setCol2(4);
  1017. anchor.setRow1(0);
  1018. anchor.setRow2(1);
  1019. Comment comment1 = drawing.createCellComment(anchor);
  1020. comment1.setString(
  1021. factory.createRichTextString("I like this cell. It's my favourite."));
  1022. comment1.setAuthor("Bob T. Fish");
  1023. Comment comment2 = drawing.createCellComment(anchor);
  1024. comment2.setString(
  1025. factory.createRichTextString("This is much less fun..."));
  1026. comment2.setAuthor("Bob T. Fish");
  1027. Cell c1 = sh1.getRow(0).createCell(4);
  1028. c1.setCellValue(2.3);
  1029. c1.setCellComment(comment1);
  1030. Cell c2 = sh1.getRow(0).createCell(5);
  1031. c2.setCellValue(2.1);
  1032. c2.setCellComment(comment2);
  1033. // Save and re-load
  1034. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  1035. sh1 = wb.getSheetAt(0);
  1036. sh2 = wb.getSheetAt(1);
  1037. // Check the comments
  1038. assertNotNull(sh2.getCommentsTable(false));
  1039. assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
  1040. assertNotNull(sh1.getCommentsTable(false));
  1041. assertEquals(2, sh1.getCommentsTable(false).getNumberOfComments());
  1042. }
  1043. /**
  1044. * Sheet names with a , in them
  1045. */
  1046. public void test51963() throws Exception {
  1047. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51963.xlsx");
  1048. XSSFSheet sheet = wb.getSheetAt(0);
  1049. assertEquals("Abc,1", sheet.getSheetName());
  1050. Name name = wb.getName("Intekon.ProdCodes");
  1051. assertEquals("'Abc,1'!$A$1:$A$2", name.getRefersToFormula());
  1052. AreaReference ref = new AreaReference(name.getRefersToFormula());
  1053. assertEquals(0, ref.getFirstCell().getRow());
  1054. assertEquals(0, ref.getFirstCell().getCol());
  1055. assertEquals(1, ref.getLastCell().getRow());
  1056. assertEquals(0, ref.getLastCell().getCol());
  1057. }
  1058. /**
  1059. * Sum across multiple workbooks
  1060. * eg =SUM($Sheet1.C1:$Sheet4.C1)
  1061. * DISABLED As we can't currently evaluate these
  1062. */
  1063. public void DISABLEDtest48703() throws Exception {
  1064. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("48703.xlsx");
  1065. XSSFSheet sheet = wb.getSheetAt(0);
  1066. // Contains two forms, one with a range and one a list
  1067. XSSFRow r1 = sheet.getRow(0);
  1068. XSSFRow r2 = sheet.getRow(1);
  1069. XSSFCell c1 = r1.getCell(1);
  1070. XSSFCell c2 = r2.getCell(1);
  1071. assertEquals(20.0, c1.getNumericCellValue());
  1072. assertEquals("SUM(Sheet1!C1,Sheet2!C1,Sheet3!C1,Sheet4!C1)", c1.getCellFormula());
  1073. assertEquals(20.0, c2.getNumericCellValue());
  1074. assertEquals("SUM(Sheet1:Sheet4!C1)", c2.getCellFormula());
  1075. // Try evaluating both
  1076. XSSFFormulaEvaluator eval = new XSSFFormulaEvaluator(wb);
  1077. eval.evaluateFormulaCell(c1);
  1078. eval.evaluateFormulaCell(c2);
  1079. assertEquals(20.0, c1.getNumericCellValue());
  1080. assertEquals(20.0, c2.getNumericCellValue());
  1081. }
  1082. }