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

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