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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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 test50440() 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. }
  557. /**
  558. * Moving a cell comment from one cell to another
  559. */
  560. public void test50795() throws Exception {
  561. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50795.xlsx");
  562. XSSFSheet sheet = wb.getSheetAt(0);
  563. XSSFRow row = sheet.getRow(0);
  564. XSSFCell cellWith = row.getCell(0);
  565. XSSFCell cellWithoutComment = row.getCell(1);
  566. assertNotNull(cellWith.getCellComment());
  567. assertNull(cellWithoutComment.getCellComment());
  568. String exp = "\u0410\u0432\u0442\u043e\u0440:\ncomment";
  569. XSSFComment comment = cellWith.getCellComment();
  570. assertEquals(exp, comment.getString().getString());
  571. // Check we can write it out and read it back as-is
  572. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  573. sheet = wb.getSheetAt(0);
  574. row = sheet.getRow(0);
  575. cellWith = row.getCell(0);
  576. cellWithoutComment = row.getCell(1);
  577. // Double check things are as expected
  578. assertNotNull(cellWith.getCellComment());
  579. assertNull(cellWithoutComment.getCellComment());
  580. comment = cellWith.getCellComment();
  581. assertEquals(exp, comment.getString().getString());
  582. // Move the comment
  583. cellWithoutComment.setCellComment(comment);
  584. // Write out and re-check
  585. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  586. sheet = wb.getSheetAt(0);
  587. row = sheet.getRow(0);
  588. // Ensure it swapped over
  589. cellWith = row.getCell(0);
  590. cellWithoutComment = row.getCell(1);
  591. assertNull(cellWith.getCellComment());
  592. assertNotNull(cellWithoutComment.getCellComment());
  593. comment = cellWithoutComment.getCellComment();
  594. assertEquals(exp, comment.getString().getString());
  595. }
  596. /**
  597. * When the cell background colour is set with one of the first
  598. * two columns of the theme colour palette, the colours are
  599. * shades of white or black.
  600. * For those cases, ensure we don't break on reading the colour
  601. */
  602. public void test50299() throws Exception {
  603. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("50299.xlsx");
  604. // Check all the colours
  605. for(int sn=0; sn<wb.getNumberOfSheets(); sn++) {
  606. Sheet s = wb.getSheetAt(sn);
  607. for(Row r : s) {
  608. for(Cell c : r) {
  609. CellStyle cs = c.getCellStyle();
  610. if(cs != null) {
  611. cs.getFillForegroundColor();
  612. }
  613. }
  614. }
  615. }
  616. // Check one bit in detail
  617. // Check that we get back foreground=0 for the theme colours,
  618. // and background=64 for the auto colouring
  619. Sheet s = wb.getSheetAt(0);
  620. assertEquals(0, s.getRow(0).getCell(8).getCellStyle().getFillForegroundColor());
  621. assertEquals(64, s.getRow(0).getCell(8).getCellStyle().getFillBackgroundColor());
  622. assertEquals(0, s.getRow(1).getCell(8).getCellStyle().getFillForegroundColor());
  623. assertEquals(64, s.getRow(1).getCell(8).getCellStyle().getFillBackgroundColor());
  624. }
  625. /**
  626. * Excel .xls style indexed colours in a .xlsx file
  627. */
  628. public void test50786() throws Exception {
  629. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50786-indexed_colours.xlsx");
  630. XSSFSheet s = wb.getSheetAt(0);
  631. XSSFRow r = s.getRow(2);
  632. // Check we have the right cell
  633. XSSFCell c = r.getCell(1);
  634. assertEquals("test\u00a0", c.getRichStringCellValue().getString());
  635. // It should be light green
  636. XSSFCellStyle cs = c.getCellStyle();
  637. assertEquals(42, cs.getFillForegroundColor());
  638. assertEquals(42, cs.getFillForegroundColorColor().getIndexed());
  639. assertNotNull(cs.getFillForegroundColorColor().getRgb());
  640. assertEquals("FFCCFFCC", cs.getFillForegroundColorColor().getARGBHex());
  641. }
  642. /**
  643. * If the border colours are set with themes, then we
  644. * should still be able to get colours
  645. */
  646. public void test50846() throws Exception {
  647. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50846-border_colours.xlsx");
  648. XSSFSheet sheet = wb.getSheetAt(0);
  649. XSSFRow row = sheet.getRow(0);
  650. // Border from a theme, brown
  651. XSSFCell cellT = row.getCell(0);
  652. XSSFCellStyle styleT = cellT.getCellStyle();
  653. XSSFColor colorT = styleT.getBottomBorderXSSFColor();
  654. assertEquals(5, colorT.getTheme());
  655. assertEquals("FFC0504D", colorT.getARGBHex());
  656. // Border from a style direct, red
  657. XSSFCell cellS = row.getCell(1);
  658. XSSFCellStyle styleS = cellS.getCellStyle();
  659. XSSFColor colorS = styleS.getBottomBorderXSSFColor();
  660. assertEquals(0, colorS.getTheme());
  661. assertEquals("FFFF0000", colorS.getARGBHex());
  662. }
  663. /**
  664. * Fonts where their colours come from the theme rather
  665. * then being set explicitly still should allow the
  666. * fetching of the RGB.
  667. */
  668. public void test50784() throws Exception {
  669. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50784-font_theme_colours.xlsx");
  670. XSSFSheet s = wb.getSheetAt(0);
  671. XSSFRow r = s.getRow(0);
  672. // Column 1 has a font with regular colours
  673. XSSFCell cr = r.getCell(1);
  674. XSSFFont fr = wb.getFontAt( cr.getCellStyle().getFontIndex() );
  675. XSSFColor colr = fr.getXSSFColor();
  676. // No theme, has colours
  677. assertEquals(0, colr.getTheme());
  678. assertNotNull( colr.getRgb() );
  679. // Column 0 has a font with colours from a theme
  680. XSSFCell ct = r.getCell(0);
  681. XSSFFont ft = wb.getFontAt( ct.getCellStyle().getFontIndex() );
  682. XSSFColor colt = ft.getXSSFColor();
  683. // Has a theme, which has the colours on it
  684. assertEquals(9, colt.getTheme());
  685. XSSFColor themeC = wb.getTheme().getThemeColor(colt.getTheme());
  686. assertNotNull( themeC.getRgb() );
  687. assertNotNull( colt.getRgb() );
  688. assertEquals( themeC.getARGBHex(), colt.getARGBHex() ); // The same colour
  689. }
  690. /**
  691. * New lines were being eaten when setting a font on
  692. * a rich text string
  693. */
  694. public void test48877() throws Exception {
  695. String text = "Use \n with word wrap on to create a new line.\n" +
  696. "This line finishes with two trailing spaces. ";
  697. XSSFWorkbook wb = new XSSFWorkbook();
  698. XSSFSheet sheet = wb.createSheet();
  699. Font font1 = wb.createFont();
  700. font1.setColor((short) 20);
  701. Font font2 = wb.createFont();
  702. font2.setColor(Font.COLOR_RED);
  703. Font font3 = wb.getFontAt((short)0);
  704. XSSFRow row = sheet.createRow(2);
  705. XSSFCell cell = row.createCell(2);
  706. XSSFRichTextString richTextString =
  707. wb.getCreationHelper().createRichTextString(text);
  708. // Check the text has the newline
  709. assertEquals(text, richTextString.getString());
  710. // Apply the font
  711. richTextString.applyFont(font3);
  712. richTextString.applyFont(0, 3, font1);
  713. cell.setCellValue(richTextString);
  714. // To enable newlines you need set a cell styles with wrap=true
  715. CellStyle cs = wb.createCellStyle();
  716. cs.setWrapText(true);
  717. cell.setCellStyle(cs);
  718. // Check the text has the
  719. assertEquals(text, cell.getStringCellValue());
  720. // Save the file and re-read it
  721. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  722. sheet = wb.getSheetAt(0);
  723. row = sheet.getRow(2);
  724. cell = row.getCell(2);
  725. assertEquals(text, cell.getStringCellValue());
  726. // Now add a 2nd, and check again
  727. int fontAt = text.indexOf("\n", 6);
  728. cell.getRichStringCellValue().applyFont(10, fontAt+1, font2);
  729. assertEquals(text, cell.getStringCellValue());
  730. assertEquals(4, cell.getRichStringCellValue().numFormattingRuns());
  731. assertEquals("Use", cell.getRichStringCellValue().getCTRst().getRList().get(0).getT());
  732. String r3 = cell.getRichStringCellValue().getCTRst().getRList().get(2).getT();
  733. assertEquals("line.\n", r3.substring(r3.length()-6));
  734. // Save and re-check
  735. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  736. sheet = wb.getSheetAt(0);
  737. row = sheet.getRow(2);
  738. cell = row.getCell(2);
  739. assertEquals(text, cell.getStringCellValue());
  740. // FileOutputStream out = new FileOutputStream("/tmp/test48877.xlsx");
  741. // wb.write(out);
  742. // out.close();
  743. }
  744. /**
  745. * Adding sheets when one has a table, then re-ordering
  746. */
  747. public void test50867() throws Exception {
  748. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("50867_with_table.xlsx");
  749. assertEquals(3, wb.getNumberOfSheets());
  750. XSSFSheet s1 = wb.getSheetAt(0);
  751. XSSFSheet s2 = wb.getSheetAt(1);
  752. XSSFSheet s3 = wb.getSheetAt(2);
  753. assertEquals(1, s1.getTables().size());
  754. assertEquals(0, s2.getTables().size());
  755. assertEquals(0, s3.getTables().size());
  756. XSSFTable t = s1.getTables().get(0);
  757. assertEquals("Tabella1", t.getName());
  758. assertEquals("Tabella1", t.getDisplayName());
  759. assertEquals("A1:C3", t.getCTTable().getRef());
  760. // Add a sheet and re-order
  761. XSSFSheet s4 = wb.createSheet("NewSheet");
  762. wb.setSheetOrder(s4.getSheetName(), 0);
  763. // Check on tables
  764. assertEquals(1, s1.getTables().size());
  765. assertEquals(0, s2.getTables().size());
  766. assertEquals(0, s3.getTables().size());
  767. assertEquals(0, s4.getTables().size());
  768. // Refetch to get the new order
  769. s1 = wb.getSheetAt(0);
  770. s2 = wb.getSheetAt(1);
  771. s3 = wb.getSheetAt(2);
  772. s4 = wb.getSheetAt(3);
  773. assertEquals(0, s1.getTables().size());
  774. assertEquals(1, s2.getTables().size());
  775. assertEquals(0, s3.getTables().size());
  776. assertEquals(0, s4.getTables().size());
  777. // Save and re-load
  778. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  779. s1 = wb.getSheetAt(0);
  780. s2 = wb.getSheetAt(1);
  781. s3 = wb.getSheetAt(2);
  782. s4 = wb.getSheetAt(3);
  783. assertEquals(0, s1.getTables().size());
  784. assertEquals(1, s2.getTables().size());
  785. assertEquals(0, s3.getTables().size());
  786. assertEquals(0, s4.getTables().size());
  787. t = s2.getTables().get(0);
  788. assertEquals("Tabella1", t.getName());
  789. assertEquals("Tabella1", t.getDisplayName());
  790. assertEquals("A1:C3", t.getCTTable().getRef());
  791. // Add some more tables, and check
  792. t = s2.createTable();
  793. t.setName("New 2");
  794. t.setDisplayName("New 2");
  795. t = s3.createTable();
  796. t.setName("New 3");
  797. t.setDisplayName("New 3");
  798. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  799. s1 = wb.getSheetAt(0);
  800. s2 = wb.getSheetAt(1);
  801. s3 = wb.getSheetAt(2);
  802. s4 = wb.getSheetAt(3);
  803. assertEquals(0, s1.getTables().size());
  804. assertEquals(2, s2.getTables().size());
  805. assertEquals(1, s3.getTables().size());
  806. assertEquals(0, s4.getTables().size());
  807. t = s2.getTables().get(0);
  808. assertEquals("Tabella1", t.getName());
  809. assertEquals("Tabella1", t.getDisplayName());
  810. assertEquals("A1:C3", t.getCTTable().getRef());
  811. t = s2.getTables().get(1);
  812. assertEquals("New 2", t.getName());
  813. assertEquals("New 2", t.getDisplayName());
  814. t = s3.getTables().get(0);
  815. assertEquals("New 3", t.getName());
  816. assertEquals("New 3", t.getDisplayName());
  817. // Check the relationships
  818. assertEquals(0, s1.getRelations().size());
  819. assertEquals(3, s2.getRelations().size());
  820. assertEquals(1, s3.getRelations().size());
  821. assertEquals(0, s4.getRelations().size());
  822. assertEquals(
  823. XSSFRelation.PRINTER_SETTINGS.getContentType(),
  824. s2.getRelations().get(0).getPackagePart().getContentType()
  825. );
  826. assertEquals(
  827. XSSFRelation.TABLE.getContentType(),
  828. s2.getRelations().get(1).getPackagePart().getContentType()
  829. );
  830. assertEquals(
  831. XSSFRelation.TABLE.getContentType(),
  832. s2.getRelations().get(2).getPackagePart().getContentType()
  833. );
  834. assertEquals(
  835. XSSFRelation.TABLE.getContentType(),
  836. s3.getRelations().get(0).getPackagePart().getContentType()
  837. );
  838. assertEquals(
  839. "/xl/tables/table3.xml",
  840. s3.getRelations().get(0).getPackagePart().getPartName().toString()
  841. );
  842. }
  843. /**
  844. * Setting repeating rows and columns shouldn't break
  845. * any print settings that were there before
  846. */
  847. public void test49253() throws Exception {
  848. XSSFWorkbook wb1 = new XSSFWorkbook();
  849. XSSFWorkbook wb2 = new XSSFWorkbook();
  850. // No print settings before repeating
  851. XSSFSheet s1 = wb1.createSheet();
  852. assertEquals(false, s1.getCTWorksheet().isSetPageSetup());
  853. assertEquals(true, s1.getCTWorksheet().isSetPageMargins());
  854. wb1.setRepeatingRowsAndColumns(0, 2, 3, 1, 2);
  855. assertEquals(true, s1.getCTWorksheet().isSetPageSetup());
  856. assertEquals(true, s1.getCTWorksheet().isSetPageMargins());
  857. XSSFPrintSetup ps1 = s1.getPrintSetup();
  858. assertEquals(false, ps1.getValidSettings());
  859. assertEquals(false, ps1.getLandscape());
  860. // Had valid print settings before repeating
  861. XSSFSheet s2 = wb2.createSheet();
  862. XSSFPrintSetup ps2 = s2.getPrintSetup();
  863. assertEquals(true, s2.getCTWorksheet().isSetPageSetup());
  864. assertEquals(true, s2.getCTWorksheet().isSetPageMargins());
  865. ps2.setLandscape(false);
  866. assertEquals(true, ps2.getValidSettings());
  867. assertEquals(false, ps2.getLandscape());
  868. wb2.setRepeatingRowsAndColumns(0, 2, 3, 1, 2);
  869. ps2 = s2.getPrintSetup();
  870. assertEquals(true, s2.getCTWorksheet().isSetPageSetup());
  871. assertEquals(true, s2.getCTWorksheet().isSetPageMargins());
  872. assertEquals(true, ps2.getValidSettings());
  873. assertEquals(false, ps2.getLandscape());
  874. }
  875. /**
  876. * Default Column style
  877. */
  878. public void test51037() throws Exception {
  879. XSSFWorkbook wb = new XSSFWorkbook();
  880. XSSFSheet s = wb.createSheet();
  881. CellStyle defaultStyle = wb.getCellStyleAt((short)0);
  882. assertEquals(0, defaultStyle.getIndex());
  883. CellStyle blueStyle = wb.createCellStyle();
  884. blueStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex());
  885. blueStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  886. assertEquals(1, blueStyle.getIndex());
  887. CellStyle pinkStyle = wb.createCellStyle();
  888. pinkStyle.setFillForegroundColor(IndexedColors.PINK.getIndex());
  889. pinkStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
  890. assertEquals(2, pinkStyle.getIndex());
  891. // Starts empty
  892. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  893. CTCols cols = s.getCTWorksheet().getColsArray(0);
  894. assertEquals(0, cols.sizeOfColArray());
  895. // Add some rows and columns
  896. XSSFRow r1 = s.createRow(0);
  897. XSSFRow r2 = s.createRow(1);
  898. r1.createCell(0);
  899. r1.createCell(2);
  900. r2.createCell(0);
  901. r2.createCell(3);
  902. // Check no style is there
  903. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  904. assertEquals(0, cols.sizeOfColArray());
  905. assertEquals(defaultStyle, s.getColumnStyle(0));
  906. assertEquals(defaultStyle, s.getColumnStyle(2));
  907. assertEquals(defaultStyle, s.getColumnStyle(3));
  908. // Apply the styles
  909. s.setDefaultColumnStyle(0, pinkStyle);
  910. s.setDefaultColumnStyle(3, blueStyle);
  911. // Check
  912. assertEquals(pinkStyle, s.getColumnStyle(0));
  913. assertEquals(defaultStyle, s.getColumnStyle(2));
  914. assertEquals(blueStyle, s.getColumnStyle(3));
  915. assertEquals(1, s.getCTWorksheet().sizeOfColsArray());
  916. assertEquals(2, cols.sizeOfColArray());
  917. assertEquals(1, cols.getColArray(0).getMin());
  918. assertEquals(1, cols.getColArray(0).getMax());
  919. assertEquals(pinkStyle.getIndex(), cols.getColArray(0).getStyle());
  920. assertEquals(4, cols.getColArray(1).getMin());
  921. assertEquals(4, cols.getColArray(1).getMax());
  922. assertEquals(blueStyle.getIndex(), cols.getColArray(1).getStyle());
  923. // Save, re-load and re-check
  924. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  925. s = wb.getSheetAt(0);
  926. defaultStyle = wb.getCellStyleAt(defaultStyle.getIndex());
  927. blueStyle = wb.getCellStyleAt(blueStyle.getIndex());
  928. pinkStyle = wb.getCellStyleAt(pinkStyle.getIndex());
  929. assertEquals(pinkStyle, s.getColumnStyle(0));
  930. assertEquals(defaultStyle, s.getColumnStyle(2));
  931. assertEquals(blueStyle, s.getColumnStyle(3));
  932. }
  933. /**
  934. * Repeatedly writing a file.
  935. * Something with the SharedStringsTable currently breaks...
  936. */
  937. public void DISABLEDtest46662() throws Exception {
  938. // New file
  939. XSSFWorkbook wb = new XSSFWorkbook();
  940. XSSFTestDataSamples.writeOutAndReadBack(wb);
  941. XSSFTestDataSamples.writeOutAndReadBack(wb);
  942. XSSFTestDataSamples.writeOutAndReadBack(wb);
  943. // Simple file
  944. wb = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
  945. XSSFTestDataSamples.writeOutAndReadBack(wb);
  946. XSSFTestDataSamples.writeOutAndReadBack(wb);
  947. XSSFTestDataSamples.writeOutAndReadBack(wb);
  948. // Complex file
  949. // TODO
  950. }
  951. /**
  952. * Colours and styles when the list has gaps in it
  953. */
  954. public void test51222() throws Exception {
  955. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51222.xlsx");
  956. XSSFSheet s = wb.getSheetAt(0);
  957. XSSFCell cA4_EEECE1 = s.getRow(3).getCell(0);
  958. XSSFCell cA5_1F497D = s.getRow(4).getCell(0);
  959. // Check the text
  960. assertEquals("A4", cA4_EEECE1.getRichStringCellValue().getString());
  961. assertEquals("A5", cA5_1F497D.getRichStringCellValue().getString());
  962. // Check the styles assigned to them
  963. assertEquals(4, cA4_EEECE1.getCTCell().getS());
  964. assertEquals(5, cA5_1F497D.getCTCell().getS());
  965. // Check we look up the correct style
  966. assertEquals(4, cA4_EEECE1.getCellStyle().getIndex());
  967. assertEquals(5, cA5_1F497D.getCellStyle().getIndex());
  968. // Check the fills on them at the low level
  969. assertEquals(5, cA4_EEECE1.getCellStyle().getCoreXf().getFillId());
  970. assertEquals(6, cA5_1F497D.getCellStyle().getCoreXf().getFillId());
  971. // These should reference themes 2 and 3
  972. assertEquals(2, wb.getStylesSource().getFillAt(5).getCTFill().getPatternFill().getFgColor().getTheme());
  973. assertEquals(3, wb.getStylesSource().getFillAt(6).getCTFill().getPatternFill().getFgColor().getTheme());
  974. // Ensure we get the right colours for these themes
  975. // TODO fix
  976. // assertEquals("FFEEECE1", wb.getTheme().getThemeColor(2).getARGBHex());
  977. // assertEquals("FF1F497D", wb.getTheme().getThemeColor(3).getARGBHex());
  978. // Finally check the colours on the styles
  979. // TODO fix
  980. // assertEquals("FFEEECE1", cA4_EEECE1.getCellStyle().getFillForegroundXSSFColor().getARGBHex());
  981. // assertEquals("FF1F497D", cA5_1F497D.getCellStyle().getFillForegroundXSSFColor().getARGBHex());
  982. }
  983. public void test51470() throws Exception {
  984. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51470.xlsx");
  985. XSSFSheet sh0 = wb.getSheetAt(0);
  986. XSSFSheet sh1 = wb.cloneSheet(0);
  987. List<POIXMLDocumentPart> rels0 = sh0.getRelations();
  988. List<POIXMLDocumentPart> rels1 = sh1.getRelations();
  989. assertEquals(1, rels0.size());
  990. assertEquals(1, rels1.size());
  991. assertEquals(rels0.get(0).getPackageRelationship(), rels1.get(0).getPackageRelationship());
  992. }
  993. /**
  994. * Add comments to Sheet 1, when Sheet 2 already has
  995. * comments (so /xl/comments1.xml is taken)
  996. */
  997. public void test51850() {
  998. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51850.xlsx");
  999. XSSFSheet sh1 = wb.getSheetAt(0);
  1000. XSSFSheet sh2 = wb.getSheetAt(1);
  1001. // Sheet 2 has comments
  1002. assertNotNull(sh2.getCommentsTable(false));
  1003. assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
  1004. // Sheet 1 doesn't (yet)
  1005. assertNull(sh1.getCommentsTable(false));
  1006. // Try to add comments to Sheet 1
  1007. CreationHelper factory = wb.getCreationHelper();
  1008. Drawing drawing = sh1.createDrawingPatriarch();
  1009. ClientAnchor anchor = factory.createClientAnchor();
  1010. anchor.setCol1(0);
  1011. anchor.setCol2(4);
  1012. anchor.setRow1(0);
  1013. anchor.setRow2(1);
  1014. Comment comment1 = drawing.createCellComment(anchor);
  1015. comment1.setString(
  1016. factory.createRichTextString("I like this cell. It's my favourite."));
  1017. comment1.setAuthor("Bob T. Fish");
  1018. Comment comment2 = drawing.createCellComment(anchor);
  1019. comment2.setString(
  1020. factory.createRichTextString("This is much less fun..."));
  1021. comment2.setAuthor("Bob T. Fish");
  1022. Cell c1 = sh1.getRow(0).createCell(4);
  1023. c1.setCellValue(2.3);
  1024. c1.setCellComment(comment1);
  1025. Cell c2 = sh1.getRow(0).createCell(5);
  1026. c2.setCellValue(2.1);
  1027. c2.setCellComment(comment2);
  1028. // Save and re-load
  1029. wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
  1030. sh1 = wb.getSheetAt(0);
  1031. sh2 = wb.getSheetAt(1);
  1032. // Check the comments
  1033. assertNotNull(sh2.getCommentsTable(false));
  1034. assertEquals(1, sh2.getCommentsTable(false).getNumberOfComments());
  1035. assertNotNull(sh1.getCommentsTable(false));
  1036. assertEquals(2, sh1.getCommentsTable(false).getNumberOfComments());
  1037. }
  1038. /**
  1039. * Sheet names with a , in them
  1040. */
  1041. public void test51963() throws Exception {
  1042. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("51963.xlsx");
  1043. XSSFSheet sheet = wb.getSheetAt(0);
  1044. assertEquals("Abc,1", sheet.getSheetName());
  1045. Name name = wb.getName("Intekon.ProdCodes");
  1046. assertEquals("'Abc,1'!$A$1:$A$2", name.getRefersToFormula());
  1047. AreaReference ref = new AreaReference(name.getRefersToFormula());
  1048. assertEquals(0, ref.getFirstCell().getRow());
  1049. assertEquals(0, ref.getFirstCell().getCol());
  1050. assertEquals(1, ref.getLastCell().getRow());
  1051. assertEquals(0, ref.getLastCell().getCol());
  1052. }
  1053. }