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.

TestXSSFFormulaParser.java 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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 static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertTrue;
  19. import static org.junit.Assert.fail;
  20. import java.io.IOException;
  21. import java.util.Arrays;
  22. import org.apache.poi.hssf.HSSFTestDataSamples;
  23. import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
  24. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  25. import org.apache.poi.ss.formula.FormulaParseException;
  26. import org.apache.poi.ss.formula.FormulaParser;
  27. import org.apache.poi.ss.formula.FormulaParsingWorkbook;
  28. import org.apache.poi.ss.formula.FormulaRenderingWorkbook;
  29. import org.apache.poi.ss.formula.FormulaType;
  30. import org.apache.poi.ss.formula.WorkbookDependentFormula;
  31. import org.apache.poi.ss.formula.ptg.Area3DPtg;
  32. import org.apache.poi.ss.formula.ptg.Area3DPxg;
  33. import org.apache.poi.ss.formula.ptg.AreaPtg;
  34. import org.apache.poi.ss.formula.ptg.AttrPtg;
  35. import org.apache.poi.ss.formula.ptg.ErrPtg;
  36. import org.apache.poi.ss.formula.ptg.FuncPtg;
  37. import org.apache.poi.ss.formula.ptg.FuncVarPtg;
  38. import org.apache.poi.ss.formula.ptg.IntPtg;
  39. import org.apache.poi.ss.formula.ptg.IntersectionPtg;
  40. import org.apache.poi.ss.formula.ptg.MemAreaPtg;
  41. import org.apache.poi.ss.formula.ptg.MemFuncPtg;
  42. import org.apache.poi.ss.formula.ptg.NamePtg;
  43. import org.apache.poi.ss.formula.ptg.NameXPxg;
  44. import org.apache.poi.ss.formula.ptg.ParenthesisPtg;
  45. import org.apache.poi.ss.formula.ptg.Ptg;
  46. import org.apache.poi.ss.formula.ptg.Ref3DPtg;
  47. import org.apache.poi.ss.formula.ptg.Ref3DPxg;
  48. import org.apache.poi.ss.formula.ptg.RefPtg;
  49. import org.apache.poi.ss.formula.ptg.StringPtg;
  50. import org.apache.poi.ss.usermodel.Cell;
  51. import org.apache.poi.ss.usermodel.CellType;
  52. import org.apache.poi.ss.usermodel.FormulaEvaluator;
  53. import org.apache.poi.ss.usermodel.Row;
  54. import org.apache.poi.ss.usermodel.Sheet;
  55. import org.apache.poi.ss.usermodel.Workbook;
  56. import org.apache.poi.ss.util.CellReference;
  57. import org.apache.poi.xssf.XSSFTestDataSamples;
  58. import org.junit.Test;
  59. public final class TestXSSFFormulaParser {
  60. private static Ptg[] parse(FormulaParsingWorkbook fpb, String fmla) {
  61. return FormulaParser.parse(fmla, fpb, FormulaType.CELL, -1);
  62. }
  63. private static Ptg[] parse(FormulaParsingWorkbook fpb, String fmla, int rowIndex) {
  64. return FormulaParser.parse(fmla, fpb, FormulaType.CELL, -1, rowIndex);
  65. }
  66. @Test
  67. public void basicParsing() throws IOException {
  68. XSSFWorkbook wb = new XSSFWorkbook();
  69. XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
  70. Ptg[] ptgs;
  71. ptgs = parse(fpb, "ABC10");
  72. assertEquals(1, ptgs.length);
  73. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof RefPtg);
  74. ptgs = parse(fpb, "A500000");
  75. assertEquals(1, ptgs.length);
  76. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof RefPtg);
  77. ptgs = parse(fpb, "ABC500000");
  78. assertEquals(1, ptgs.length);
  79. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof RefPtg);
  80. //highest allowed rows and column (XFD and 0x100000)
  81. ptgs = parse(fpb, "XFD1048576");
  82. assertEquals(1, ptgs.length);
  83. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof RefPtg);
  84. //column greater than XFD
  85. try {
  86. /*ptgs =*/ parse(fpb, "XFE10");
  87. fail("expected exception");
  88. } catch (FormulaParseException e){
  89. assertEquals("Specified named range 'XFE10' does not exist in the current workbook.", e.getMessage());
  90. }
  91. //row greater than 0x100000
  92. try {
  93. /*ptgs =*/ parse(fpb, "XFD1048577");
  94. fail("expected exception");
  95. } catch (FormulaParseException e){
  96. assertEquals("Specified named range 'XFD1048577' does not exist in the current workbook.", e.getMessage());
  97. }
  98. // Formula referencing one cell
  99. ptgs = parse(fpb, "ISEVEN(A1)");
  100. assertEquals(3, ptgs.length);
  101. assertEquals(NameXPxg.class, ptgs[0].getClass());
  102. assertEquals(RefPtg.class, ptgs[1].getClass());
  103. assertEquals(FuncVarPtg.class, ptgs[2].getClass());
  104. assertEquals("ISEVEN", ptgs[0].toFormulaString());
  105. assertEquals("A1", ptgs[1].toFormulaString());
  106. assertEquals("#external#", ptgs[2].toFormulaString());
  107. // Formula referencing an area
  108. ptgs = parse(fpb, "SUM(A1:B3)");
  109. assertEquals(2, ptgs.length);
  110. assertEquals(AreaPtg.class, ptgs[0].getClass());
  111. assertEquals(AttrPtg.class, ptgs[1].getClass());
  112. assertEquals("A1:B3", ptgs[0].toFormulaString());
  113. assertEquals("SUM", ptgs[1].toFormulaString());
  114. // Formula referencing one cell in a different sheet
  115. ptgs = parse(fpb, "SUM(Sheet1!A1)");
  116. assertEquals(2, ptgs.length);
  117. assertEquals(Ref3DPxg.class, ptgs[0].getClass());
  118. assertEquals(AttrPtg.class, ptgs[1].getClass());
  119. assertEquals("Sheet1!A1", ptgs[0].toFormulaString());
  120. assertEquals("SUM", ptgs[1].toFormulaString());
  121. // Formula referencing an area in a different sheet
  122. ptgs = parse(fpb, "SUM(Sheet1!A1:B3)");
  123. assertEquals(2, ptgs.length);
  124. assertEquals(Area3DPxg.class,ptgs[0].getClass());
  125. assertEquals(AttrPtg.class, ptgs[1].getClass());
  126. assertEquals("Sheet1!A1:B3", ptgs[0].toFormulaString());
  127. assertEquals("SUM", ptgs[1].toFormulaString());
  128. wb.close();
  129. }
  130. @Test
  131. public void builtInFormulas() throws IOException {
  132. XSSFWorkbook wb = new XSSFWorkbook();
  133. XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
  134. Ptg[] ptgs;
  135. ptgs = parse(fpb, "LOG10");
  136. assertEquals(1, ptgs.length);
  137. assertTrue("",(ptgs[0] instanceof RefPtg));
  138. ptgs = parse(fpb, "LOG10(100)");
  139. assertEquals(2, ptgs.length);
  140. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof IntPtg);
  141. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof FuncPtg);
  142. wb.close();
  143. }
  144. @Test
  145. public void formulaReferencesSameWorkbook() throws IOException {
  146. // Use a test file with "other workbook" style references
  147. // to itself
  148. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56737.xlsx");
  149. XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
  150. Ptg[] ptgs;
  151. // Reference to a named range in our own workbook, as if it
  152. // were defined in a different workbook
  153. ptgs = parse(fpb, "[0]!NR_Global_B2");
  154. assertEquals(1, ptgs.length);
  155. assertEquals(NameXPxg.class, ptgs[0].getClass());
  156. assertEquals(0, ((NameXPxg)ptgs[0]).getExternalWorkbookNumber());
  157. assertEquals(null, ((NameXPxg)ptgs[0]).getSheetName());
  158. assertEquals("NR_Global_B2",((NameXPxg)ptgs[0]).getNameName());
  159. assertEquals("[0]!NR_Global_B2",((NameXPxg)ptgs[0]).toFormulaString());
  160. wb.close();
  161. }
  162. @Test
  163. public void formulaReferencesOtherSheets() throws IOException {
  164. // Use a test file with the named ranges in place
  165. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56737.xlsx");
  166. XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
  167. Ptg[] ptgs;
  168. // Reference to a single cell in a different sheet
  169. ptgs = parse(fpb, "Uses!A1");
  170. assertEquals(1, ptgs.length);
  171. assertEquals(Ref3DPxg.class, ptgs[0].getClass());
  172. assertEquals(-1, ((Ref3DPxg)ptgs[0]).getExternalWorkbookNumber());
  173. assertEquals("A1", ((Ref3DPxg)ptgs[0]).format2DRefAsString());
  174. assertEquals("Uses!A1", ((Ref3DPxg)ptgs[0]).toFormulaString());
  175. // Reference to a single cell in a different sheet, which needs quoting
  176. ptgs = parse(fpb, "'Testing 47100'!A1");
  177. assertEquals(1, ptgs.length);
  178. assertEquals(Ref3DPxg.class, ptgs[0].getClass());
  179. assertEquals(-1, ((Ref3DPxg)ptgs[0]).getExternalWorkbookNumber());
  180. assertEquals("Testing 47100", ((Ref3DPxg)ptgs[0]).getSheetName());
  181. assertEquals("A1", ((Ref3DPxg)ptgs[0]).format2DRefAsString());
  182. assertEquals("'Testing 47100'!A1", ((Ref3DPxg)ptgs[0]).toFormulaString());
  183. // Reference to a sheet scoped named range from another sheet
  184. ptgs = parse(fpb, "Defines!NR_To_A1");
  185. assertEquals(1, ptgs.length);
  186. assertEquals(NameXPxg.class, ptgs[0].getClass());
  187. assertEquals(-1, ((NameXPxg)ptgs[0]).getExternalWorkbookNumber());
  188. assertEquals("Defines", ((NameXPxg)ptgs[0]).getSheetName());
  189. assertEquals("NR_To_A1",((NameXPxg)ptgs[0]).getNameName());
  190. assertEquals("Defines!NR_To_A1",((NameXPxg)ptgs[0]).toFormulaString());
  191. // Reference to a workbook scoped named range
  192. ptgs = parse(fpb, "NR_Global_B2");
  193. assertEquals(1, ptgs.length);
  194. assertEquals(NamePtg.class, ptgs[0].getClass());
  195. assertEquals("NR_Global_B2",((NamePtg)ptgs[0]).toFormulaString(fpb));
  196. wb.close();
  197. }
  198. @Test
  199. public void formulaReferencesOtherWorkbook() throws IOException {
  200. // Use a test file with the external linked table in place
  201. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("ref-56737.xlsx");
  202. XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
  203. Ptg[] ptgs;
  204. // Reference to a single cell in a different workbook
  205. ptgs = parse(fpb, "[1]Uses!$A$1");
  206. assertEquals(1, ptgs.length);
  207. assertEquals(Ref3DPxg.class, ptgs[0].getClass());
  208. assertEquals(1, ((Ref3DPxg)ptgs[0]).getExternalWorkbookNumber());
  209. assertEquals("Uses",((Ref3DPxg)ptgs[0]).getSheetName());
  210. assertEquals("$A$1",((Ref3DPxg)ptgs[0]).format2DRefAsString());
  211. assertEquals("[1]Uses!$A$1",((Ref3DPxg)ptgs[0]).toFormulaString());
  212. // Reference to a sheet-scoped named range in a different workbook
  213. ptgs = parse(fpb, "[1]Defines!NR_To_A1");
  214. assertEquals(1, ptgs.length);
  215. assertEquals(NameXPxg.class, ptgs[0].getClass());
  216. assertEquals(1, ((NameXPxg)ptgs[0]).getExternalWorkbookNumber());
  217. assertEquals("Defines", ((NameXPxg)ptgs[0]).getSheetName());
  218. assertEquals("NR_To_A1",((NameXPxg)ptgs[0]).getNameName());
  219. assertEquals("[1]Defines!NR_To_A1",((NameXPxg)ptgs[0]).toFormulaString());
  220. // Reference to a global named range in a different workbook
  221. ptgs = parse(fpb, "[1]!NR_Global_B2");
  222. assertEquals(1, ptgs.length);
  223. assertEquals(NameXPxg.class, ptgs[0].getClass());
  224. assertEquals(1, ((NameXPxg)ptgs[0]).getExternalWorkbookNumber());
  225. assertEquals(null, ((NameXPxg)ptgs[0]).getSheetName());
  226. assertEquals("NR_Global_B2",((NameXPxg)ptgs[0]).getNameName());
  227. assertEquals("[1]!NR_Global_B2",((NameXPxg)ptgs[0]).toFormulaString());
  228. wb.close();
  229. }
  230. /**
  231. * A handful of functions (such as SUM, COUNTA, MIN) support
  232. * multi-sheet references (eg Sheet1:Sheet3!A1 = Cell A1 from
  233. * Sheets 1 through Sheet 3) and multi-sheet area references
  234. * (eg Sheet1:Sheet3!A1:B2 = Cells A1 through B2 from Sheets
  235. * 1 through Sheet 3).
  236. * This test, based on common test files for HSSF and XSSF, checks
  237. * that we can read and parse these kinds of references
  238. * (but not evaluate - that's elsewhere in the test suite)
  239. */
  240. @Test
  241. public void multiSheetReferencesHSSFandXSSF() throws IOException {
  242. Workbook[] wbs = new Workbook[] {
  243. HSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xls"),
  244. XSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xlsx")
  245. };
  246. for (Workbook wb : wbs) {
  247. Sheet s1 = wb.getSheetAt(0);
  248. Ptg[] ptgs;
  249. // Check the contents
  250. Cell sumF = s1.getRow(2).getCell(0);
  251. assertNotNull(sumF);
  252. assertEquals("SUM(Sheet1:Sheet3!A1)", sumF.getCellFormula());
  253. Cell avgF = s1.getRow(2).getCell(1);
  254. assertNotNull(avgF);
  255. assertEquals("AVERAGE(Sheet1:Sheet3!A1)", avgF.getCellFormula());
  256. Cell countAF = s1.getRow(2).getCell(2);
  257. assertNotNull(countAF);
  258. assertEquals("COUNTA(Sheet1:Sheet3!C1)", countAF.getCellFormula());
  259. Cell maxF = s1.getRow(4).getCell(1);
  260. assertNotNull(maxF);
  261. assertEquals("MAX(Sheet1:Sheet3!A$1)", maxF.getCellFormula());
  262. Cell sumFA = s1.getRow(2).getCell(7);
  263. assertNotNull(sumFA);
  264. assertEquals("SUM(Sheet1:Sheet3!A1:B2)", sumFA.getCellFormula());
  265. Cell avgFA = s1.getRow(2).getCell(8);
  266. assertNotNull(avgFA);
  267. assertEquals("AVERAGE(Sheet1:Sheet3!A1:B2)", avgFA.getCellFormula());
  268. Cell maxFA = s1.getRow(4).getCell(8);
  269. assertNotNull(maxFA);
  270. assertEquals("MAX(Sheet1:Sheet3!A$1:B$2)", maxFA.getCellFormula());
  271. Cell countFA = s1.getRow(5).getCell(8);
  272. assertNotNull(countFA);
  273. assertEquals("COUNT(Sheet1:Sheet3!$A$1:$B$2)", countFA.getCellFormula());
  274. // Create a formula parser
  275. final FormulaParsingWorkbook fpb;
  276. if (wb instanceof HSSFWorkbook)
  277. fpb = HSSFEvaluationWorkbook.create((HSSFWorkbook)wb);
  278. else
  279. fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook)wb);
  280. // Check things parse as expected:
  281. // SUM to one cell over 3 workbooks, relative reference
  282. ptgs = parse(fpb, "SUM(Sheet1:Sheet3!A1)");
  283. assertEquals(2, ptgs.length);
  284. if (wb instanceof HSSFWorkbook) {
  285. assertEquals(Ref3DPtg.class, ptgs[0].getClass());
  286. } else {
  287. assertEquals(Ref3DPxg.class, ptgs[0].getClass());
  288. }
  289. assertEquals("Sheet1:Sheet3!A1", toFormulaString(ptgs[0], fpb));
  290. assertEquals(AttrPtg.class, ptgs[1].getClass());
  291. assertEquals("SUM", toFormulaString(ptgs[1], fpb));
  292. // MAX to one cell over 3 workbooks, absolute row reference
  293. ptgs = parse(fpb, "MAX(Sheet1:Sheet3!A$1)");
  294. assertEquals(2, ptgs.length);
  295. if (wb instanceof HSSFWorkbook) {
  296. assertEquals(Ref3DPtg.class, ptgs[0].getClass());
  297. } else {
  298. assertEquals(Ref3DPxg.class, ptgs[0].getClass());
  299. }
  300. assertEquals("Sheet1:Sheet3!A$1", toFormulaString(ptgs[0], fpb));
  301. assertEquals(FuncVarPtg.class, ptgs[1].getClass());
  302. assertEquals("MAX", toFormulaString(ptgs[1], fpb));
  303. // MIN to one cell over 3 workbooks, absolute reference
  304. ptgs = parse(fpb, "MIN(Sheet1:Sheet3!$A$1)");
  305. assertEquals(2, ptgs.length);
  306. if (wb instanceof HSSFWorkbook) {
  307. assertEquals(Ref3DPtg.class, ptgs[0].getClass());
  308. } else {
  309. assertEquals(Ref3DPxg.class, ptgs[0].getClass());
  310. }
  311. assertEquals("Sheet1:Sheet3!$A$1", toFormulaString(ptgs[0], fpb));
  312. assertEquals(FuncVarPtg.class, ptgs[1].getClass());
  313. assertEquals("MIN", toFormulaString(ptgs[1], fpb));
  314. // SUM to a range of cells over 3 workbooks
  315. ptgs = parse(fpb, "SUM(Sheet1:Sheet3!A1:B2)");
  316. assertEquals(2, ptgs.length);
  317. if (wb instanceof HSSFWorkbook) {
  318. assertEquals(Area3DPtg.class, ptgs[0].getClass());
  319. } else {
  320. assertEquals(Area3DPxg.class, ptgs[0].getClass());
  321. }
  322. assertEquals("Sheet1:Sheet3!A1:B2", toFormulaString(ptgs[0], fpb));
  323. assertEquals(AttrPtg.class, ptgs[1].getClass());
  324. assertEquals("SUM", toFormulaString(ptgs[1], fpb));
  325. // MIN to a range of cells over 3 workbooks, absolute reference
  326. ptgs = parse(fpb, "MIN(Sheet1:Sheet3!$A$1:$B$2)");
  327. assertEquals(2, ptgs.length);
  328. if (wb instanceof HSSFWorkbook) {
  329. assertEquals(Area3DPtg.class, ptgs[0].getClass());
  330. } else {
  331. assertEquals(Area3DPxg.class, ptgs[0].getClass());
  332. }
  333. assertEquals("Sheet1:Sheet3!$A$1:$B$2", toFormulaString(ptgs[0], fpb));
  334. assertEquals(FuncVarPtg.class, ptgs[1].getClass());
  335. assertEquals("MIN", toFormulaString(ptgs[1], fpb));
  336. // Check we can round-trip - try to set a new one to a new single cell
  337. Cell newF = s1.getRow(0).createCell(10, CellType.FORMULA);
  338. newF.setCellFormula("SUM(Sheet2:Sheet3!A1)");
  339. assertEquals("SUM(Sheet2:Sheet3!A1)", newF.getCellFormula());
  340. // Check we can round-trip - try to set a new one to a cell range
  341. newF = s1.getRow(0).createCell(11, CellType.FORMULA);
  342. newF.setCellFormula("MIN(Sheet1:Sheet2!A1:B2)");
  343. assertEquals("MIN(Sheet1:Sheet2!A1:B2)", newF.getCellFormula());
  344. wb.close();
  345. }
  346. }
  347. private static String toFormulaString(Ptg ptg, FormulaParsingWorkbook wb) {
  348. if (ptg instanceof WorkbookDependentFormula) {
  349. return ((WorkbookDependentFormula)ptg).toFormulaString((FormulaRenderingWorkbook)wb);
  350. }
  351. return ptg.toFormulaString();
  352. }
  353. @Test
  354. public void test58648Single() throws IOException {
  355. XSSFWorkbook wb = new XSSFWorkbook();
  356. XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
  357. Ptg[] ptgs;
  358. ptgs = parse(fpb, "(ABC10 )");
  359. assertEquals("Had: " + Arrays.toString(ptgs),
  360. 2, ptgs.length);
  361. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof RefPtg);
  362. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof ParenthesisPtg);
  363. wb.close();
  364. }
  365. @Test
  366. public void test58648Basic() throws IOException {
  367. XSSFWorkbook wb = new XSSFWorkbook();
  368. XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
  369. Ptg[] ptgs;
  370. // verify whitespaces in different places
  371. ptgs = parse(fpb, "(ABC10)");
  372. assertEquals("Had: " + Arrays.toString(ptgs),
  373. 2, ptgs.length);
  374. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof RefPtg);
  375. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof ParenthesisPtg);
  376. ptgs = parse(fpb, "( ABC10)");
  377. assertEquals("Had: " + Arrays.toString(ptgs),
  378. 2, ptgs.length);
  379. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof RefPtg);
  380. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof ParenthesisPtg);
  381. ptgs = parse(fpb, "(ABC10 )");
  382. assertEquals("Had: " + Arrays.toString(ptgs),
  383. 2, ptgs.length);
  384. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof RefPtg);
  385. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof ParenthesisPtg);
  386. ptgs = parse(fpb, "((ABC10))");
  387. assertEquals("Had: " + Arrays.toString(ptgs),
  388. 3, ptgs.length);
  389. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof RefPtg);
  390. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof ParenthesisPtg);
  391. assertTrue("Had " + Arrays.toString(ptgs), ptgs[2] instanceof ParenthesisPtg);
  392. ptgs = parse(fpb, "((ABC10) )");
  393. assertEquals("Had: " + Arrays.toString(ptgs),
  394. 3, ptgs.length);
  395. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof RefPtg);
  396. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof ParenthesisPtg);
  397. assertTrue("Had " + Arrays.toString(ptgs), ptgs[2] instanceof ParenthesisPtg);
  398. ptgs = parse(fpb, "( (ABC10))");
  399. assertEquals("Had: " + Arrays.toString(ptgs),
  400. 3, ptgs.length);
  401. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof RefPtg);
  402. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof ParenthesisPtg);
  403. assertTrue("Had " + Arrays.toString(ptgs), ptgs[2] instanceof ParenthesisPtg);
  404. wb.close();
  405. }
  406. @Test
  407. public void test58648FormulaParsing() throws IOException {
  408. Workbook wb = XSSFTestDataSamples.openSampleWorkbook("58648.xlsx");
  409. FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
  410. for (int i = 0; i < wb.getNumberOfSheets(); i++) {
  411. Sheet xsheet = wb.getSheetAt(i);
  412. for (Row row : xsheet) {
  413. for (Cell cell : row) {
  414. if (cell.getCellTypeEnum() == CellType.FORMULA) {
  415. try {
  416. evaluator.evaluateFormulaCellEnum(cell);
  417. } catch (Exception e) {
  418. CellReference cellRef = new CellReference(cell.getRowIndex(), cell.getColumnIndex());
  419. throw new RuntimeException("error at: " + cellRef.toString(), e);
  420. }
  421. }
  422. }
  423. }
  424. }
  425. Sheet sheet = wb.getSheet("my-sheet");
  426. Cell cell = sheet.getRow(1).getCell(4);
  427. assertEquals(5d, cell.getNumericCellValue(), 0d);
  428. wb.close();
  429. }
  430. @Test
  431. public void testWhitespaceInFormula() throws IOException {
  432. XSSFWorkbook wb = new XSSFWorkbook();
  433. XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
  434. Ptg[] ptgs;
  435. // verify whitespaces in different places
  436. ptgs = parse(fpb, "INTERCEPT(A2:A5, B2:B5)");
  437. assertEquals("Had: " + Arrays.toString(ptgs),
  438. 3, ptgs.length);
  439. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof AreaPtg);
  440. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof AreaPtg);
  441. assertTrue("Had " + Arrays.toString(ptgs), ptgs[2] instanceof FuncPtg);
  442. ptgs = parse(fpb, " INTERCEPT ( \t \r A2 : \nA5 , B2 : B5 ) \t");
  443. assertEquals("Had: " + Arrays.toString(ptgs),
  444. 3, ptgs.length);
  445. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof AreaPtg);
  446. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof AreaPtg);
  447. assertTrue("Had " + Arrays.toString(ptgs), ptgs[2] instanceof FuncPtg);
  448. ptgs = parse(fpb, "(VLOOKUP(\"item1\", A2:B3, 2, FALSE) - VLOOKUP(\"item2\", A2:B3, 2, FALSE) )");
  449. assertEquals("Had: " + Arrays.toString(ptgs),
  450. 12, ptgs.length);
  451. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof StringPtg);
  452. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof AreaPtg);
  453. assertTrue("Had " + Arrays.toString(ptgs), ptgs[2] instanceof IntPtg);
  454. ptgs = parse(fpb, "A1:B1 B1:B2");
  455. assertEquals("Had: " + Arrays.toString(ptgs),
  456. 4, ptgs.length);
  457. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof MemAreaPtg);
  458. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof AreaPtg);
  459. assertTrue("Had " + Arrays.toString(ptgs), ptgs[2] instanceof AreaPtg);
  460. assertTrue("Had " + Arrays.toString(ptgs), ptgs[3] instanceof IntersectionPtg);
  461. ptgs = parse(fpb, "A1:B1 B1:B2");
  462. assertEquals("Had: " + Arrays.toString(ptgs),
  463. 4, ptgs.length);
  464. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof MemAreaPtg);
  465. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof AreaPtg);
  466. assertTrue("Had " + Arrays.toString(ptgs), ptgs[2] instanceof AreaPtg);
  467. assertTrue("Had " + Arrays.toString(ptgs), ptgs[3] instanceof IntersectionPtg);
  468. wb.close();
  469. }
  470. @Test
  471. public void testWhitespaceInComplexFormula() throws IOException {
  472. XSSFWorkbook wb = new XSSFWorkbook();
  473. XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
  474. Ptg[] ptgs;
  475. // verify whitespaces in different places
  476. ptgs = parse(fpb, "SUM(A1:INDEX(1:1048576,MAX(IFERROR(MATCH(99^99,B:B,1),0),IFERROR(MATCH(\"zzzz\",B:B,1),0)),MAX(IFERROR(MATCH(99^99,1:1,1),0),IFERROR(MATCH(\"zzzz\",1:1,1),0))))");
  477. assertEquals("Had: " + Arrays.toString(ptgs),
  478. 40, ptgs.length);
  479. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof MemFuncPtg);
  480. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof RefPtg);
  481. assertTrue("Had " + Arrays.toString(ptgs), ptgs[2] instanceof AreaPtg);
  482. assertTrue("Had " + Arrays.toString(ptgs), ptgs[3] instanceof NameXPxg);
  483. ptgs = parse(fpb, "SUM ( A1 : INDEX( 1 : 1048576 , MAX( IFERROR ( MATCH ( 99 ^ 99 , B : B , 1 ) , 0 ) , IFERROR ( MATCH ( \"zzzz\" , B:B , 1 ) , 0 ) ) , MAX ( IFERROR ( MATCH ( 99 ^ 99 , 1 : 1 , 1 ) , 0 ) , IFERROR ( MATCH ( \"zzzz\" , 1 : 1 , 1 ) , 0 ) ) ) )");
  484. assertEquals("Had: " + Arrays.toString(ptgs),
  485. 40, ptgs.length);
  486. assertTrue("Had " + Arrays.toString(ptgs), ptgs[0] instanceof MemFuncPtg);
  487. assertTrue("Had " + Arrays.toString(ptgs), ptgs[1] instanceof RefPtg);
  488. assertTrue("Had " + Arrays.toString(ptgs), ptgs[2] instanceof AreaPtg);
  489. assertTrue("Had " + Arrays.toString(ptgs), ptgs[3] instanceof NameXPxg);
  490. wb.close();
  491. }
  492. @Test
  493. public void parseStructuredReferences() throws IOException {
  494. XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("StructuredReferences.xlsx");
  495. XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create(wb);
  496. Ptg[] ptgs;
  497. /*
  498. The following cases are tested (copied from FormulaParser.parseStructuredReference)
  499. 1 Table1[col]
  500. 2 Table1[[#Totals],[col]]
  501. 3 Table1[#Totals]
  502. 4 Table1[#All]
  503. 5 Table1[#Data]
  504. 6 Table1[#Headers]
  505. 7 Table1[#Totals]
  506. 8 Table1[#This Row]
  507. 9 Table1[[#All],[col]]
  508. 10 Table1[[#Headers],[col]]
  509. 11 Table1[[#Totals],[col]]
  510. 12 Table1[[#All],[col1]:[col2]]
  511. 13 Table1[[#Data],[col1]:[col2]]
  512. 14 Table1[[#Headers],[col1]:[col2]]
  513. 15 Table1[[#Totals],[col1]:[col2]]
  514. 16 Table1[[#Headers],[#Data],[col2]]
  515. 17 Table1[[#This Row], [col1]]
  516. 18 Table1[ [col1]:[col2] ]
  517. */
  518. final String tbl = "\\_Prime.1";
  519. final String noTotalsRowReason = ": Tables without a Totals row should return #REF! on [#Totals]";
  520. ////// Case 1: Evaluate Table1[col] with apostrophe-escaped #-signs ////////
  521. ptgs = parse(fpb, "SUM("+tbl+"[calc='#*'#])");
  522. assertEquals(2, ptgs.length);
  523. // Area3DPxg [sheet=Table ! A2:A7]
  524. assertTrue(ptgs[0] instanceof Area3DPxg);
  525. Area3DPxg ptg0 = (Area3DPxg) ptgs[0];
  526. assertEquals("Table", ptg0.getSheetName());
  527. assertEquals("A2:A7", ptg0.format2DRefAsString());
  528. // Note: structured references are evaluated and resolved to regular 3D area references.
  529. assertEquals("Table!A2:A7", ptg0.toFormulaString());
  530. // AttrPtg [sum ]
  531. assertTrue(ptgs[1] instanceof AttrPtg);
  532. AttrPtg ptg1 = (AttrPtg) ptgs[1];
  533. assertTrue(ptg1.isSum());
  534. ////// Case 1: Evaluate "Table1[col]" ////////
  535. ptgs = parse(fpb, tbl+"[Name]");
  536. assertEquals(1, ptgs.length);
  537. assertEquals("Table1[col]", "Table!B2:B7", ptgs[0].toFormulaString());
  538. ////// Case 2: Evaluate "Table1[[#Totals],[col]]" ////////
  539. ptgs = parse(fpb, tbl+"[[#Totals],[col]]");
  540. assertEquals(1, ptgs.length);
  541. assertEquals("Table1[[#Totals],[col]]" + noTotalsRowReason, ErrPtg.REF_INVALID, ptgs[0]);
  542. ////// Case 3: Evaluate "Table1[#Totals]" ////////
  543. ptgs = parse(fpb, tbl+"[#Totals]");
  544. assertEquals(1, ptgs.length);
  545. assertEquals("Table1[#Totals]" + noTotalsRowReason, ErrPtg.REF_INVALID, ptgs[0]);
  546. ////// Case 4: Evaluate "Table1[#All]" ////////
  547. ptgs = parse(fpb, tbl+"[#All]");
  548. assertEquals(1, ptgs.length);
  549. assertEquals("Table1[#All]", "Table!A1:C7", ptgs[0].toFormulaString());
  550. ////// Case 5: Evaluate "Table1[#Data]" (excludes Header and Data rows) ////////
  551. ptgs = parse(fpb, tbl+"[#Data]");
  552. assertEquals(1, ptgs.length);
  553. assertEquals("Table1[#Data]", "Table!A2:C7", ptgs[0].toFormulaString());
  554. ////// Case 6: Evaluate "Table1[#Headers]" ////////
  555. ptgs = parse(fpb, tbl+"[#Headers]");
  556. assertEquals(1, ptgs.length);
  557. assertEquals("Table1[#Headers]", "Table!A1:C1", ptgs[0].toFormulaString());
  558. ////// Case 7: Evaluate "Table1[#Totals]" ////////
  559. ptgs = parse(fpb, tbl+"[#Totals]");
  560. assertEquals(1, ptgs.length);
  561. assertEquals("Table1[#Totals]" + noTotalsRowReason, ErrPtg.REF_INVALID, ptgs[0]);
  562. ////// Case 8: Evaluate "Table1[#This Row]" ////////
  563. ptgs = parse(fpb, tbl+"[#This Row]", 2);
  564. assertEquals(1, ptgs.length);
  565. assertEquals("Table1[#This Row]", "Table!A3:C3", ptgs[0].toFormulaString());
  566. ////// Evaluate "Table1[@]" (equivalent to "Table1[#This Row]") ////////
  567. ptgs = parse(fpb, tbl+"[@]", 2);
  568. assertEquals(1, ptgs.length);
  569. assertEquals("Table!A3:C3", ptgs[0].toFormulaString());
  570. ////// Evaluate "Table1[#This Row]" when rowIndex is outside Table ////////
  571. ptgs = parse(fpb, tbl+"[#This Row]", 10);
  572. assertEquals(1, ptgs.length);
  573. assertEquals("Table1[#This Row]", ErrPtg.VALUE_INVALID, ptgs[0]);
  574. ////// Evaluate "Table1[@]" when rowIndex is outside Table ////////
  575. ptgs = parse(fpb, tbl+"[@]", 10);
  576. assertEquals(1, ptgs.length);
  577. assertEquals("Table1[@]", ErrPtg.VALUE_INVALID, ptgs[0]);
  578. ////// Evaluate "Table1[[#Data],[col]]" ////////
  579. ptgs = parse(fpb, tbl+"[[#Data], [Number]]");
  580. assertEquals(1, ptgs.length);
  581. assertEquals("Table1[[#Data],[col]]", "Table!C2:C7", ptgs[0].toFormulaString());
  582. ////// Case 9: Evaluate "Table1[[#All],[col]]" ////////
  583. ptgs = parse(fpb, tbl+"[[#All], [Number]]");
  584. assertEquals(1, ptgs.length);
  585. assertEquals("Table1[[#All],[col]]", "Table!C1:C7", ptgs[0].toFormulaString());
  586. ////// Case 10: Evaluate "Table1[[#Headers],[col]]" ////////
  587. ptgs = parse(fpb, tbl+"[[#Headers], [Number]]");
  588. assertEquals(1, ptgs.length);
  589. // also acceptable: Table1!B1
  590. assertEquals("Table1[[#Headers],[col]]", "Table!C1:C1", ptgs[0].toFormulaString());
  591. ////// Case 11: Evaluate "Table1[[#Totals],[col]]" ////////
  592. ptgs = parse(fpb, tbl+"[[#Totals],[Name]]");
  593. assertEquals(1, ptgs.length);
  594. assertEquals("Table1[[#Totals],[col]]" + noTotalsRowReason, ErrPtg.REF_INVALID, ptgs[0]);
  595. ////// Case 12: Evaluate "Table1[[#All],[col1]:[col2]]" ////////
  596. ptgs = parse(fpb, tbl+"[[#All], [Name]:[Number]]");
  597. assertEquals(1, ptgs.length);
  598. assertEquals("Table1[[#All],[col1]:[col2]]", "Table!B1:C7", ptgs[0].toFormulaString());
  599. ////// Case 13: Evaluate "Table1[[#Data],[col]:[col2]]" ////////
  600. ptgs = parse(fpb, tbl+"[[#Data], [Name]:[Number]]");
  601. assertEquals(1, ptgs.length);
  602. assertEquals("Table1[[#Data],[col]:[col2]]", "Table!B2:C7", ptgs[0].toFormulaString());
  603. ////// Case 14: Evaluate "Table1[[#Headers],[col1]:[col2]]" ////////
  604. ptgs = parse(fpb, tbl+"[[#Headers], [Name]:[Number]]");
  605. assertEquals(1, ptgs.length);
  606. assertEquals("Table1[[#Headers],[col1]:[col2]]", "Table!B1:C1", ptgs[0].toFormulaString());
  607. ////// Case 15: Evaluate "Table1[[#Totals],[col]:[col2]]" ////////
  608. ptgs = parse(fpb, tbl+"[[#Totals], [Name]:[Number]]");
  609. assertEquals(1, ptgs.length);
  610. assertEquals("Table1[[#Totals],[col]:[col2]]" + noTotalsRowReason, ErrPtg.REF_INVALID, ptgs[0]);
  611. ////// Case 16: Evaluate "Table1[[#Headers],[#Data],[col]]" ////////
  612. ptgs = parse(fpb, tbl+"[[#Headers],[#Data],[Number]]");
  613. assertEquals(1, ptgs.length);
  614. assertEquals("Table1[[#Headers],[#Data],[col]]", "Table!C1:C7", ptgs[0].toFormulaString());
  615. ////// Case 17: Evaluate "Table1[[#This Row], [col1]]" ////////
  616. ptgs = parse(fpb, tbl+"[[#This Row], [Number]]", 2);
  617. assertEquals(1, ptgs.length);
  618. // also acceptable: Table!C3
  619. assertEquals("Table1[[#This Row], [col1]]", "Table!C3:C3", ptgs[0].toFormulaString());
  620. ////// Case 18: Evaluate "Table1[[col]:[col2]]" ////////
  621. ptgs = parse(fpb, tbl+"[[Name]:[Number]]");
  622. assertEquals(1, ptgs.length);
  623. assertEquals("Table1[[col]:[col2]]", "Table!B2:C7", ptgs[0].toFormulaString());
  624. wb.close();
  625. }
  626. }