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

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