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.

BaseTestBugzillaIssues.java 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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.ss.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertNull;
  19. import static org.junit.Assert.assertTrue;
  20. import static org.junit.Assert.fail;
  21. import java.io.IOException;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.apache.poi.hssf.util.PaneInformation;
  26. import org.apache.poi.ss.ITestDataProvider;
  27. import org.apache.poi.ss.SpreadsheetVersion;
  28. import org.apache.poi.ss.util.CellRangeAddress;
  29. import org.junit.Test;
  30. /**
  31. * A base class for bugzilla issues that can be described in terms of common ss interfaces.
  32. *
  33. * @author Yegor Kozlov
  34. */
  35. public abstract class BaseTestBugzillaIssues {
  36. private final ITestDataProvider _testDataProvider;
  37. protected BaseTestBugzillaIssues(ITestDataProvider testDataProvider) {
  38. _testDataProvider = testDataProvider;
  39. }
  40. public static void assertAlmostEquals(double expected, double actual, float factor) {
  41. double diff = Math.abs(expected - actual);
  42. double fuzz = expected * factor;
  43. if (diff > fuzz)
  44. fail(actual + " not within " + fuzz + " of " + expected);
  45. }
  46. /**
  47. * Test writing a hyperlink
  48. * Open resulting sheet in Excel and check that A1 contains a hyperlink
  49. *
  50. * Also tests bug 15353 (problems with hyperlinks to Google)
  51. */
  52. @Test
  53. public final void bug23094() {
  54. Workbook wb = _testDataProvider.createWorkbook();
  55. Sheet s = wb.createSheet();
  56. Row r = s.createRow(0);
  57. r.createCell(0).setCellFormula("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")");
  58. r.createCell(1).setCellFormula("HYPERLINK(\"http://google.com\",\"Google\")");
  59. wb = _testDataProvider.writeOutAndReadBack(wb);
  60. r = wb.getSheetAt(0).getRow(0);
  61. Cell cell_0 = r.getCell(0);
  62. assertEquals("HYPERLINK(\"http://jakarta.apache.org\",\"Jakarta\")", cell_0.getCellFormula());
  63. Cell cell_1 = r.getCell(1);
  64. assertEquals("HYPERLINK(\"http://google.com\",\"Google\")", cell_1.getCellFormula());
  65. }
  66. /**
  67. * test writing a file with large number of unique strings,
  68. * open resulting file in Excel to check results!
  69. * @param num the number of strings to generate
  70. */
  71. public final void bug15375(int num) {
  72. Workbook wb = _testDataProvider.createWorkbook();
  73. Sheet sheet = wb.createSheet();
  74. CreationHelper factory = wb.getCreationHelper();
  75. String tmp1 = null;
  76. String tmp2 = null;
  77. String tmp3 = null;
  78. for (int i = 0; i < num; i++) {
  79. tmp1 = "Test1" + i;
  80. tmp2 = "Test2" + i;
  81. tmp3 = "Test3" + i;
  82. Row row = sheet.createRow(i);
  83. Cell cell = row.createCell(0);
  84. cell.setCellValue(factory.createRichTextString(tmp1));
  85. cell = row.createCell(1);
  86. cell.setCellValue(factory.createRichTextString(tmp2));
  87. cell = row.createCell(2);
  88. cell.setCellValue(factory.createRichTextString(tmp3));
  89. }
  90. wb = _testDataProvider.writeOutAndReadBack(wb);
  91. for (int i = 0; i < num; i++) {
  92. tmp1 = "Test1" + i;
  93. tmp2 = "Test2" + i;
  94. tmp3 = "Test3" + i;
  95. Row row = sheet.getRow(i);
  96. assertEquals(tmp1, row.getCell(0).getStringCellValue());
  97. assertEquals(tmp2, row.getCell(1).getStringCellValue());
  98. assertEquals(tmp3, row.getCell(2).getStringCellValue());
  99. }
  100. }
  101. /**
  102. * Merged regions were being removed from the parent in cloned sheets
  103. */
  104. @Test
  105. public final void bug22720() {
  106. Workbook workBook = _testDataProvider.createWorkbook();
  107. workBook.createSheet("TEST");
  108. Sheet template = workBook.getSheetAt(0);
  109. template.addMergedRegion(new CellRangeAddress(0, 1, 0, 2));
  110. template.addMergedRegion(new CellRangeAddress(1, 2, 0, 2));
  111. Sheet clone = workBook.cloneSheet(0);
  112. int originalMerged = template.getNumMergedRegions();
  113. assertEquals("2 merged regions", 2, originalMerged);
  114. //remove merged regions from clone
  115. for (int i=template.getNumMergedRegions()-1; i>=0; i--) {
  116. clone.removeMergedRegion(i);
  117. }
  118. assertEquals("Original Sheet's Merged Regions were removed", originalMerged, template.getNumMergedRegions());
  119. //check if template's merged regions are OK
  120. if (template.getNumMergedRegions()>0) {
  121. // fetch the first merged region...EXCEPTION OCCURS HERE
  122. template.getMergedRegion(0);
  123. }
  124. //make sure we dont exception
  125. }
  126. @Test
  127. public final void bug28031() {
  128. Workbook wb = _testDataProvider.createWorkbook();
  129. Sheet sheet = wb.createSheet();
  130. wb.setSheetName(0, "Sheet1");
  131. Row row = sheet.createRow(0);
  132. Cell cell = row.createCell(0);
  133. String formulaText =
  134. "IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))";
  135. cell.setCellFormula(formulaText);
  136. assertEquals(formulaText, cell.getCellFormula());
  137. wb = _testDataProvider.writeOutAndReadBack(wb);
  138. cell = wb.getSheetAt(0).getRow(0).getCell(0);
  139. assertEquals("IF(ROUND(A2*B2*C2,2)>ROUND(B2*D2,2),ROUND(A2*B2*C2,2),ROUND(B2*D2,2))", cell.getCellFormula());
  140. }
  141. /**
  142. * Bug 21334: "File error: data may have been lost" with a file
  143. * that contains macros and this formula:
  144. * {=SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),""))>0,1))}
  145. */
  146. @Test
  147. public final void bug21334() {
  148. Workbook wb = _testDataProvider.createWorkbook();
  149. Sheet sh = wb.createSheet();
  150. Cell cell = sh.createRow(0).createCell(0);
  151. String formula = "SUM(IF(FREQUENCY(IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"),IF(LEN(V4:V220)>0,MATCH(V4:V220,V4:V220,0),\"\"))>0,1))";
  152. cell.setCellFormula(formula);
  153. Workbook wb_sv = _testDataProvider.writeOutAndReadBack(wb);
  154. Cell cell_sv = wb_sv.getSheetAt(0).getRow(0).getCell(0);
  155. assertEquals(formula, cell_sv.getCellFormula());
  156. }
  157. /** another test for the number of unique strings issue
  158. *test opening the resulting file in Excel*/
  159. @Test
  160. public final void bug22568() {
  161. int r=2000;int c=3;
  162. Workbook wb = _testDataProvider.createWorkbook();
  163. Sheet sheet = wb.createSheet("ExcelTest") ;
  164. int col_cnt=0, rw_cnt=0 ;
  165. col_cnt = c;
  166. rw_cnt = r;
  167. Row rw ;
  168. rw = sheet.createRow(0) ;
  169. //Header row
  170. for(int j=0; j<col_cnt; j++){
  171. Cell cell = rw.createCell(j) ;
  172. cell.setCellValue("Col " + (j+1));
  173. }
  174. for(int i=1; i<rw_cnt; i++){
  175. rw = sheet.createRow(i) ;
  176. for(int j=0; j<col_cnt; j++){
  177. Cell cell = rw.createCell(j) ;
  178. cell.setCellValue("Row:" + (i+1) + ",Column:" + (j+1));
  179. }
  180. }
  181. sheet.setDefaultColumnWidth(18) ;
  182. wb = _testDataProvider.writeOutAndReadBack(wb);
  183. sheet = wb.getSheetAt(0);
  184. rw = sheet.getRow(0);
  185. //Header row
  186. for(int j=0; j<col_cnt; j++){
  187. Cell cell = rw.getCell(j) ;
  188. assertEquals("Col " + (j+1), cell.getStringCellValue());
  189. }
  190. for(int i=1; i<rw_cnt; i++){
  191. rw = sheet.getRow(i) ;
  192. for(int j=0; j<col_cnt; j++){
  193. Cell cell = rw.getCell(j) ;
  194. assertEquals("Row:" + (i+1) + ",Column:" + (j+1), cell.getStringCellValue());
  195. }
  196. }
  197. }
  198. /**
  199. * Bug 42448: Can't parse SUMPRODUCT(A!C7:A!C67, B8:B68) / B69
  200. */
  201. @Test
  202. public final void bug42448(){
  203. Workbook wb = _testDataProvider.createWorkbook();
  204. Cell cell = wb.createSheet().createRow(0).createCell(0);
  205. cell.setCellFormula("SUMPRODUCT(A!C7:A!C67, B8:B68) / B69");
  206. assertTrue("no errors parsing formula", true);
  207. }
  208. @Test
  209. public final void bug18800() {
  210. Workbook book = _testDataProvider.createWorkbook();
  211. book.createSheet("TEST");
  212. Sheet sheet = book.cloneSheet(0);
  213. book.setSheetName(1,"CLONE");
  214. sheet.createRow(0).createCell(0).setCellValue("Test");
  215. book = _testDataProvider.writeOutAndReadBack(book);
  216. sheet = book.getSheet("CLONE");
  217. Row row = sheet.getRow(0);
  218. Cell cell = row.getCell(0);
  219. assertEquals("Test", cell.getRichStringCellValue().getString());
  220. }
  221. private static void addNewSheetWithCellsA1toD4(Workbook book, int sheet) {
  222. Sheet sht = book .createSheet("s" + sheet);
  223. for (int r=0; r < 4; r++) {
  224. Row row = sht.createRow (r);
  225. for (int c=0; c < 4; c++) {
  226. Cell cel = row.createCell(c);
  227. cel.setCellValue(sheet*100 + r*10 + c);
  228. }
  229. }
  230. }
  231. @Test
  232. public final void bug43093() {
  233. Workbook xlw = _testDataProvider.createWorkbook();
  234. addNewSheetWithCellsA1toD4(xlw, 1);
  235. addNewSheetWithCellsA1toD4(xlw, 2);
  236. addNewSheetWithCellsA1toD4(xlw, 3);
  237. addNewSheetWithCellsA1toD4(xlw, 4);
  238. Sheet s2 = xlw.getSheet("s2");
  239. Row s2r3 = s2.getRow(3);
  240. Cell s2E4 = s2r3.createCell(4);
  241. s2E4.setCellFormula("SUM(s3!B2:C3)");
  242. FormulaEvaluator eva = xlw.getCreationHelper().createFormulaEvaluator();
  243. double d = eva.evaluate(s2E4).getNumberValue();
  244. assertEquals(d, (311+312+321+322), 0.0000001);
  245. }
  246. @Test
  247. public final void bug46729_testMaxFunctionArguments(){
  248. String[] func = {"COUNT", "AVERAGE", "MAX", "MIN", "OR", "SUBTOTAL", "SKEW"};
  249. SpreadsheetVersion ssVersion = _testDataProvider.getSpreadsheetVersion();
  250. Workbook wb = _testDataProvider.createWorkbook();
  251. Cell cell = wb.createSheet().createRow(0).createCell(0);
  252. String fmla;
  253. for (String name : func) {
  254. fmla = createFunction(name, 5);
  255. cell.setCellFormula(fmla);
  256. fmla = createFunction(name, ssVersion.getMaxFunctionArgs());
  257. cell.setCellFormula(fmla);
  258. try {
  259. fmla = createFunction(name, ssVersion.getMaxFunctionArgs() + 1);
  260. cell.setCellFormula(fmla);
  261. fail("Expected FormulaParseException");
  262. } catch (RuntimeException e){
  263. assertTrue(e.getMessage().startsWith("Too many arguments to function '"+name+"'"));
  264. }
  265. }
  266. }
  267. private static String createFunction(String name, int maxArgs){
  268. StringBuffer fmla = new StringBuffer();
  269. fmla.append(name);
  270. fmla.append("(");
  271. for(int i=0; i < maxArgs; i++){
  272. if(i > 0) fmla.append(',');
  273. fmla.append("A1");
  274. }
  275. fmla.append(")");
  276. return fmla.toString();
  277. }
  278. @Test
  279. public final void bug506819_testAutoSize() {
  280. Workbook wb = _testDataProvider.createWorkbook();
  281. BaseTestSheetAutosizeColumn.fixFonts(wb);
  282. Sheet sheet = wb.createSheet("Sheet1");
  283. Row row = sheet.createRow(0);
  284. Cell cell0 = row.createCell(0);
  285. String longValue = "www.hostname.com, www.hostname.com, " +
  286. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  287. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  288. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  289. "www.hostname.com, www.hostname.com, www.hostname.com, " +
  290. "www.hostname.com, www.hostname.com, www.hostname.com, www.hostname.com";
  291. cell0.setCellValue(longValue);
  292. sheet.autoSizeColumn(0);
  293. assertEquals(255*256, sheet.getColumnWidth(0)); // maximum column width is 255 characters
  294. sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 506819 reports exception at this point
  295. }
  296. /**
  297. * CreateFreezePane column/row order check
  298. */
  299. @Test
  300. public void bug49381() throws Exception {
  301. Workbook wb = _testDataProvider.createWorkbook();
  302. int colSplit = 1;
  303. int rowSplit = 2;
  304. int leftmostColumn = 3;
  305. int topRow = 4;
  306. Sheet s = wb.createSheet();
  307. // Populate
  308. for(int rn=0; rn<= topRow; rn++) {
  309. Row r = s.createRow(rn);
  310. for(int cn=0; cn<leftmostColumn; cn++) {
  311. Cell c = r.createCell(cn, Cell.CELL_TYPE_NUMERIC);
  312. c.setCellValue(100*rn + cn);
  313. }
  314. }
  315. // Create the Freeze Pane
  316. s.createFreezePane(colSplit, rowSplit, leftmostColumn, topRow);
  317. PaneInformation paneInfo = s.getPaneInformation();
  318. // Check it
  319. assertEquals(colSplit, paneInfo.getVerticalSplitPosition());
  320. assertEquals(rowSplit, paneInfo.getHorizontalSplitPosition());
  321. assertEquals(leftmostColumn, paneInfo.getVerticalSplitLeftColumn());
  322. assertEquals(topRow, paneInfo.getHorizontalSplitTopRow());
  323. // Now a row only freezepane
  324. s.createFreezePane(0, 3);
  325. paneInfo = s.getPaneInformation();
  326. assertEquals(0, paneInfo.getVerticalSplitPosition());
  327. assertEquals(3, paneInfo.getHorizontalSplitPosition());
  328. assertEquals(0, paneInfo.getVerticalSplitLeftColumn());
  329. assertEquals(3, paneInfo.getHorizontalSplitTopRow());
  330. // Now a column only freezepane
  331. s.createFreezePane(4, 0);
  332. paneInfo = s.getPaneInformation();
  333. assertEquals(4, paneInfo.getVerticalSplitPosition());
  334. assertEquals(0, paneInfo.getHorizontalSplitPosition());
  335. assertEquals(4 , paneInfo.getVerticalSplitLeftColumn());
  336. assertEquals(0, paneInfo.getHorizontalSplitTopRow());
  337. }
  338. /**
  339. * Test hyperlinks
  340. * open resulting file in excel, and check that there is a link to Google
  341. */
  342. @Test
  343. public void bug15353() {
  344. String hyperlinkF = "HYPERLINK(\"http://google.com\",\"Google\")";
  345. Workbook wb = _testDataProvider.createWorkbook();
  346. Sheet sheet = wb.createSheet("My sheet");
  347. Row row = sheet.createRow( 0 );
  348. Cell cell = row.createCell( 0 );
  349. cell.setCellFormula(hyperlinkF);
  350. assertEquals(hyperlinkF, cell.getCellFormula());
  351. wb = _testDataProvider.writeOutAndReadBack(wb);
  352. sheet = wb.getSheet("My Sheet");
  353. row = sheet.getRow( 0 );
  354. cell = row.getCell( 0 );
  355. assertEquals(hyperlinkF, cell.getCellFormula());
  356. }
  357. /**
  358. * HLookup and VLookup with optional arguments
  359. */
  360. @Test
  361. public void bug51024() throws Exception {
  362. Workbook wb = _testDataProvider.createWorkbook();
  363. Sheet s = wb.createSheet();
  364. Row r1 = s.createRow(0);
  365. Row r2 = s.createRow(1);
  366. r1.createCell(0).setCellValue("v A1");
  367. r2.createCell(0).setCellValue("v A2");
  368. r1.createCell(1).setCellValue("v B1");
  369. Cell c = r1.createCell(4);
  370. FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
  371. c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1)");
  372. assertEquals("v A1", eval.evaluate(c).getStringValue());
  373. c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1, 1)");
  374. assertEquals("v A1", eval.evaluate(c).getStringValue());
  375. c.setCellFormula("VLOOKUP(\"v A1\", A1:B2, 1, )");
  376. assertEquals("v A1", eval.evaluate(c).getStringValue());
  377. c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1)");
  378. assertEquals("v A1", eval.evaluate(c).getStringValue());
  379. c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1, 1)");
  380. assertEquals("v A1", eval.evaluate(c).getStringValue());
  381. c.setCellFormula("HLOOKUP(\"v A1\", A1:B2, 1, )");
  382. assertEquals("v A1", eval.evaluate(c).getStringValue());
  383. }
  384. @Test
  385. public void stackoverflow23114397() throws Exception {
  386. Workbook wb = _testDataProvider.createWorkbook();
  387. DataFormat format = wb.getCreationHelper().createDataFormat();
  388. // How close the sizing should be, given that not all
  389. // systems will have quite the same fonts on them
  390. float fontAccuracy = 0.22f;
  391. // x%
  392. CellStyle iPercent = wb.createCellStyle();
  393. iPercent.setDataFormat(format.getFormat("0%"));
  394. // x.x%
  395. CellStyle d1Percent = wb.createCellStyle();
  396. d1Percent.setDataFormat(format.getFormat("0.0%"));
  397. // x.xx%
  398. CellStyle d2Percent = wb.createCellStyle();
  399. d2Percent.setDataFormat(format.getFormat("0.00%"));
  400. Sheet s = wb.createSheet();
  401. Row r1 = s.createRow(0);
  402. for (int i=0; i<3; i++) {
  403. r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(0);
  404. }
  405. for (int i=3; i<6; i++) {
  406. r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(1);
  407. }
  408. for (int i=6; i<9; i++) {
  409. r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(0.12345);
  410. }
  411. for (int i=9; i<12; i++) {
  412. r1.createCell(i, Cell.CELL_TYPE_NUMERIC).setCellValue(1.2345);
  413. }
  414. for (int i=0; i<12; i+=3) {
  415. r1.getCell(i+0).setCellStyle(iPercent);
  416. r1.getCell(i+1).setCellStyle(d1Percent);
  417. r1.getCell(i+2).setCellStyle(d2Percent);
  418. }
  419. for (int i=0; i<12; i++) {
  420. s.autoSizeColumn(i);
  421. }
  422. // Check the 0(.00)% ones
  423. assertAlmostEquals(980, s.getColumnWidth(0), fontAccuracy);
  424. assertAlmostEquals(1400, s.getColumnWidth(1), fontAccuracy);
  425. assertAlmostEquals(1700, s.getColumnWidth(2), fontAccuracy);
  426. // Check the 100(.00)% ones
  427. assertAlmostEquals(1500, s.getColumnWidth(3), fontAccuracy);
  428. assertAlmostEquals(1950, s.getColumnWidth(4), fontAccuracy);
  429. assertAlmostEquals(2225, s.getColumnWidth(5), fontAccuracy);
  430. // Check the 12(.34)% ones
  431. assertAlmostEquals(1225, s.getColumnWidth(6), fontAccuracy);
  432. assertAlmostEquals(1650, s.getColumnWidth(7), fontAccuracy);
  433. assertAlmostEquals(1950, s.getColumnWidth(8), fontAccuracy);
  434. // Check the 123(.45)% ones
  435. assertAlmostEquals(1500, s.getColumnWidth(9), fontAccuracy);
  436. assertAlmostEquals(1950, s.getColumnWidth(10), fontAccuracy);
  437. assertAlmostEquals(2225, s.getColumnWidth(11), fontAccuracy);
  438. }
  439. /**
  440. * =ISNUMBER(SEARCH("AM",A1)) evaluation
  441. */
  442. @Test
  443. public void stackoverflow26437323() throws Exception {
  444. Workbook wb = _testDataProvider.createWorkbook();
  445. Sheet s = wb.createSheet();
  446. Row r1 = s.createRow(0);
  447. Row r2 = s.createRow(1);
  448. // A1 is a number
  449. r1.createCell(0).setCellValue(1.1);
  450. // B1 is a string, with the wanted text in it
  451. r1.createCell(1).setCellValue("This is text with AM in it");
  452. // C1 is a string, with different text
  453. r1.createCell(2).setCellValue("This some other text");
  454. // D1 is a blank cell
  455. r1.createCell(3, Cell.CELL_TYPE_BLANK);
  456. // E1 is null
  457. // A2 will hold our test formulas
  458. Cell cf = r2.createCell(0, Cell.CELL_TYPE_FORMULA);
  459. // First up, check that TRUE and ISLOGICAL both behave
  460. cf.setCellFormula("TRUE()");
  461. cf = evaluateCell(wb, cf);
  462. assertEquals(true, cf.getBooleanCellValue());
  463. cf.setCellFormula("ISLOGICAL(TRUE())");
  464. cf = evaluateCell(wb, cf);
  465. assertEquals(true, cf.getBooleanCellValue());
  466. cf.setCellFormula("ISLOGICAL(4)");
  467. cf = evaluateCell(wb, cf);
  468. assertEquals(false, cf.getBooleanCellValue());
  469. // Now, check ISNUMBER / ISTEXT / ISNONTEXT
  470. cf.setCellFormula("ISNUMBER(A1)");
  471. cf = evaluateCell(wb, cf);
  472. assertEquals(true, cf.getBooleanCellValue());
  473. cf.setCellFormula("ISNUMBER(B1)");
  474. cf = evaluateCell(wb, cf);
  475. assertEquals(false, cf.getBooleanCellValue());
  476. cf.setCellFormula("ISNUMBER(C1)");
  477. cf = evaluateCell(wb, cf);
  478. assertEquals(false, cf.getBooleanCellValue());
  479. cf.setCellFormula("ISNUMBER(D1)");
  480. cf = evaluateCell(wb, cf);
  481. assertEquals(false, cf.getBooleanCellValue());
  482. cf.setCellFormula("ISNUMBER(E1)");
  483. cf = evaluateCell(wb, cf);
  484. assertEquals(false, cf.getBooleanCellValue());
  485. cf.setCellFormula("ISTEXT(A1)");
  486. cf = evaluateCell(wb, cf);
  487. assertEquals(false, cf.getBooleanCellValue());
  488. cf.setCellFormula("ISTEXT(B1)");
  489. cf = evaluateCell(wb, cf);
  490. assertEquals(true, cf.getBooleanCellValue());
  491. cf.setCellFormula("ISTEXT(C1)");
  492. cf = evaluateCell(wb, cf);
  493. assertEquals(true, cf.getBooleanCellValue());
  494. cf.setCellFormula("ISTEXT(D1)");
  495. cf = evaluateCell(wb, cf);
  496. assertEquals(false, cf.getBooleanCellValue());
  497. cf.setCellFormula("ISTEXT(E1)");
  498. cf = evaluateCell(wb, cf);
  499. assertEquals(false, cf.getBooleanCellValue());
  500. cf.setCellFormula("ISNONTEXT(A1)");
  501. cf = evaluateCell(wb, cf);
  502. assertEquals(true, cf.getBooleanCellValue());
  503. cf.setCellFormula("ISNONTEXT(B1)");
  504. cf = evaluateCell(wb, cf);
  505. assertEquals(false, cf.getBooleanCellValue());
  506. cf.setCellFormula("ISNONTEXT(C1)");
  507. cf = evaluateCell(wb, cf);
  508. assertEquals(false, cf.getBooleanCellValue());
  509. cf.setCellFormula("ISNONTEXT(D1)");
  510. cf = evaluateCell(wb, cf);
  511. assertEquals(true, cf.getBooleanCellValue());
  512. cf.setCellFormula("ISNONTEXT(E1)");
  513. cf = evaluateCell(wb, cf);
  514. assertEquals(true, cf.getBooleanCellValue()); // Blank and Null the same
  515. // Next up, SEARCH on its own
  516. cf.setCellFormula("SEARCH(\"am\", A1)");
  517. cf = evaluateCell(wb, cf);
  518. assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
  519. cf.setCellFormula("SEARCH(\"am\", B1)");
  520. cf = evaluateCell(wb, cf);
  521. assertEquals(19, (int)cf.getNumericCellValue());
  522. cf.setCellFormula("SEARCH(\"am\", C1)");
  523. cf = evaluateCell(wb, cf);
  524. assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
  525. cf.setCellFormula("SEARCH(\"am\", D1)");
  526. cf = evaluateCell(wb, cf);
  527. assertEquals(FormulaError.VALUE.getCode(), cf.getErrorCellValue());
  528. // Finally, bring it all together
  529. cf.setCellFormula("ISNUMBER(SEARCH(\"am\", A1))");
  530. cf = evaluateCell(wb, cf);
  531. assertEquals(false, cf.getBooleanCellValue());
  532. cf.setCellFormula("ISNUMBER(SEARCH(\"am\", B1))");
  533. cf = evaluateCell(wb, cf);
  534. assertEquals(true, cf.getBooleanCellValue());
  535. cf.setCellFormula("ISNUMBER(SEARCH(\"am\", C1))");
  536. cf = evaluateCell(wb, cf);
  537. assertEquals(false, cf.getBooleanCellValue());
  538. cf.setCellFormula("ISNUMBER(SEARCH(\"am\", D1))");
  539. cf = evaluateCell(wb, cf);
  540. assertEquals(false, cf.getBooleanCellValue());
  541. cf.setCellFormula("ISNUMBER(SEARCH(\"am\", E1))");
  542. cf = evaluateCell(wb, cf);
  543. assertEquals(false, cf.getBooleanCellValue());
  544. }
  545. private Cell evaluateCell(Workbook wb, Cell c) {
  546. Sheet s = c.getSheet();
  547. wb.getCreationHelper().createFormulaEvaluator().evaluateFormulaCell(c);
  548. return s.getRow(c.getRowIndex()).getCell(c.getColumnIndex());
  549. }
  550. /**
  551. * Should be able to write then read formulas with references
  552. * to cells in other files, eg '[refs/airport.xls]Sheet1'!$A$2
  553. * or 'http://192.168.1.2/[blank.xls]Sheet1'!$A$1 .
  554. * Additionally, if a reference to that file is provided, it should
  555. * be possible to evaluate them too
  556. * TODO Fix this to evaluate for XSSF
  557. * TODO Fix this to work at all for HSSF
  558. */
  559. // @Test
  560. public void bug46670() throws Exception {
  561. Workbook wb = _testDataProvider.createWorkbook();
  562. Sheet s = wb.createSheet();
  563. Row r1 = s.createRow(0);
  564. // References to try
  565. String ext = "xls";
  566. if (! (wb instanceof HSSFWorkbook)) ext += "x";
  567. String refLocal = "'[test."+ext+"]Sheet1'!$A$2";
  568. String refHttp = "'[http://example.com/test."+ext+"]Sheet1'!$A$2";
  569. String otherCellText = "In Another Workbook";
  570. // Create the references
  571. Cell c1 = r1.createCell(0, Cell.CELL_TYPE_FORMULA);
  572. c1.setCellFormula(refLocal);
  573. Cell c2 = r1.createCell(1, Cell.CELL_TYPE_FORMULA);
  574. c2.setCellFormula(refHttp);
  575. // Check they were set correctly
  576. assertEquals(refLocal, c1.getCellFormula());
  577. assertEquals(refHttp, c2.getCellFormula());
  578. // Reload, and ensure they were serialised and read correctly
  579. wb = _testDataProvider.writeOutAndReadBack(wb);
  580. s = wb.getSheetAt(0);
  581. r1 = s.getRow(0);
  582. c1 = r1.getCell(0);
  583. c2 = r1.getCell(1);
  584. assertEquals(refLocal, c1.getCellFormula());
  585. assertEquals(refHttp, c2.getCellFormula());
  586. // Try to evalutate, without giving a way to get at the other file
  587. try {
  588. evaluateCell(wb, c1);
  589. fail("Shouldn't be able to evaluate without the other file");
  590. } catch (Exception e) {}
  591. try {
  592. evaluateCell(wb, c2);
  593. fail("Shouldn't be able to evaluate without the other file");
  594. } catch (Exception e) {}
  595. // Set up references to the other file
  596. Workbook wb2 = _testDataProvider.createWorkbook();
  597. wb2.createSheet().createRow(1).createCell(0).setCellValue(otherCellText);
  598. Map<String,FormulaEvaluator> evaluators = new HashMap<String, FormulaEvaluator>();
  599. evaluators.put(refLocal, wb2.getCreationHelper().createFormulaEvaluator());
  600. evaluators.put(refHttp, wb2.getCreationHelper().createFormulaEvaluator());
  601. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  602. evaluator.setupReferencedWorkbooks(evaluators);
  603. // Try to evaluate, with the other file
  604. evaluator.evaluateFormulaCell(c1);
  605. evaluator.evaluateFormulaCell(c2);
  606. assertEquals(otherCellText, c1.getStringCellValue());
  607. assertEquals(otherCellText, c2.getStringCellValue());
  608. }
  609. @Test
  610. public void test56574OverwriteExistingRow() throws IOException {
  611. Workbook wb = _testDataProvider.createWorkbook();
  612. Sheet sheet = wb.createSheet();
  613. { // create the Formula-Cell
  614. Row row = sheet.createRow(0);
  615. Cell cell = row.createCell(0);
  616. cell.setCellFormula("A2");
  617. }
  618. { // check that it is there now
  619. Row row = sheet.getRow(0);
  620. /* CTCell[] cArray = ((XSSFRow)row).getCTRow().getCArray();
  621. assertEquals(1, cArray.length);*/
  622. Cell cell = row.getCell(0);
  623. assertEquals(Cell.CELL_TYPE_FORMULA, cell.getCellType());
  624. }
  625. { // overwrite the row
  626. Row row = sheet.createRow(0);
  627. assertNotNull(row);
  628. }
  629. { // creating a row in place of another should remove the existing data,
  630. // check that the cell is gone now
  631. Row row = sheet.getRow(0);
  632. /*CTCell[] cArray = ((XSSFRow)row).getCTRow().getCArray();
  633. assertEquals(0, cArray.length);*/
  634. Cell cell = row.getCell(0);
  635. assertNull(cell);
  636. }
  637. // the calculation chain in XSSF is empty in a newly created workbook, so we cannot check if it is correctly updated
  638. /*assertNull(((XSSFWorkbook)wb).getCalculationChain());
  639. assertNotNull(((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain());
  640. assertNotNull(((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain().getCArray());
  641. assertEquals(0, ((XSSFWorkbook)wb).getCalculationChain().getCTCalcChain().getCArray().length);*/
  642. wb.close();
  643. }
  644. }