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.

BaseTestNamedRange.java 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertTrue;
  21. import static org.junit.Assert.fail;
  22. import java.io.IOException;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. import org.apache.poi.ss.ITestDataProvider;
  26. import org.apache.poi.ss.util.AreaReference;
  27. import org.apache.poi.ss.util.CellReference;
  28. import org.apache.poi.util.IOUtils;
  29. import org.junit.Test;
  30. /**
  31. * Tests of implementations of {@link org.apache.poi.ss.usermodel.Name}.
  32. *
  33. * @author Yegor Kozlov
  34. */
  35. public abstract class BaseTestNamedRange {
  36. private final ITestDataProvider _testDataProvider;
  37. protected BaseTestNamedRange(ITestDataProvider testDataProvider) {
  38. _testDataProvider = testDataProvider;
  39. }
  40. @Test
  41. public final void testCreate() throws Exception {
  42. // Create a new workbook
  43. Workbook wb = _testDataProvider.createWorkbook();
  44. wb.createSheet("Test1");
  45. wb.createSheet("Testing Named Ranges");
  46. Name name1 = wb.createName();
  47. name1.setNameName("testOne");
  48. //setting a duplicate name should throw IllegalArgumentException
  49. Name name2 = wb.createName();
  50. try {
  51. name2.setNameName("testOne");
  52. fail("expected exception");
  53. } catch (IllegalArgumentException e){
  54. assertEquals("The workbook already contains this name: testOne", e.getMessage());
  55. }
  56. //the check for duplicates is case-insensitive
  57. try {
  58. name2.setNameName("TESTone");
  59. fail("expected exception");
  60. } catch (IllegalArgumentException e){
  61. assertEquals("The workbook already contains this name: TESTone", e.getMessage());
  62. }
  63. name2.setNameName("testTwo");
  64. String ref1 = "Test1!$A$1:$B$1";
  65. name1.setRefersToFormula(ref1);
  66. assertEquals(ref1, name1.getRefersToFormula());
  67. assertEquals("Test1", name1.getSheetName());
  68. String ref2 = "'Testing Named Ranges'!$A$1:$B$1";
  69. name1.setRefersToFormula(ref2);
  70. assertEquals("'Testing Named Ranges'!$A$1:$B$1", name1.getRefersToFormula());
  71. assertEquals("Testing Named Ranges", name1.getSheetName());
  72. assertEquals(-1, name1.getSheetIndex());
  73. name1.setSheetIndex(-1);
  74. assertEquals(-1, name1.getSheetIndex());
  75. try {
  76. name1.setSheetIndex(2);
  77. fail("should throw IllegalArgumentException");
  78. } catch(IllegalArgumentException e){
  79. assertEquals("Sheet index (2) is out of range (0..1)", e.getMessage());
  80. }
  81. name1.setSheetIndex(1);
  82. assertEquals(1, name1.getSheetIndex());
  83. //-1 means the name applies to the entire workbook
  84. name1.setSheetIndex(-1);
  85. assertEquals(-1, name1.getSheetIndex());
  86. //names cannot be blank and must begin with a letter or underscore and not contain spaces
  87. String[] invalidNames = {"", "123", "1Name", "Named Range"};
  88. for (String name : invalidNames) {
  89. try {
  90. name1.setNameName(name);
  91. fail("should have thrown exceptiuon due to invalid name: " + name);
  92. } catch (IllegalArgumentException e) {
  93. // expected during successful test
  94. }
  95. }
  96. wb.close();
  97. }
  98. @Test
  99. public final void testUnicodeNamedRange() throws Exception {
  100. Workbook wb1 = _testDataProvider.createWorkbook();
  101. wb1.createSheet("Test");
  102. Name name = wb1.createName();
  103. name.setNameName("\u03B1");
  104. name.setRefersToFormula("Test!$D$3:$E$8");
  105. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  106. Name name2 = wb2.getNameAt(0);
  107. assertEquals("\u03B1", name2.getNameName());
  108. assertEquals("Test!$D$3:$E$8", name2.getRefersToFormula());
  109. wb2.close();
  110. wb1.close();
  111. }
  112. @Test
  113. public final void testAddRemove() throws Exception {
  114. Workbook wb = _testDataProvider.createWorkbook();
  115. assertEquals(0, wb.getNumberOfNames());
  116. Name name1 = wb.createName();
  117. name1.setNameName("name1");
  118. assertEquals(1, wb.getNumberOfNames());
  119. Name name2 = wb.createName();
  120. name2.setNameName("name2");
  121. assertEquals(2, wb.getNumberOfNames());
  122. Name name3 = wb.createName();
  123. name3.setNameName("name3");
  124. assertEquals(3, wb.getNumberOfNames());
  125. wb.removeName("name2");
  126. assertEquals(2, wb.getNumberOfNames());
  127. wb.removeName(0);
  128. assertEquals(1, wb.getNumberOfNames());
  129. wb.close();
  130. }
  131. @Test
  132. public final void testScope() throws Exception {
  133. Workbook wb = _testDataProvider.createWorkbook();
  134. wb.createSheet();
  135. wb.createSheet();
  136. Name name;
  137. name = wb.createName();
  138. name.setNameName("aaa");
  139. name = wb.createName();
  140. try {
  141. name.setNameName("aaa");
  142. fail("Expected exception");
  143. } catch(Exception e){
  144. assertEquals("The workbook already contains this name: aaa", e.getMessage());
  145. }
  146. name = wb.createName();
  147. name.setSheetIndex(0);
  148. name.setNameName("aaa");
  149. name = wb.createName();
  150. name.setSheetIndex(0);
  151. try {
  152. name.setNameName("aaa");
  153. fail("Expected exception");
  154. } catch(Exception e){
  155. assertEquals("The sheet already contains this name: aaa", e.getMessage());
  156. }
  157. name = wb.createName();
  158. name.setSheetIndex(1);
  159. name.setNameName("aaa");
  160. name = wb.createName();
  161. name.setSheetIndex(1);
  162. try {
  163. name.setNameName("aaa");
  164. fail("Expected exception");
  165. } catch(Exception e){
  166. assertEquals("The sheet already contains this name: aaa", e.getMessage());
  167. }
  168. assertEquals(3, wb.getNames("aaa").size());
  169. wb.close();
  170. }
  171. /**
  172. * Test case provided by czhang@cambian.com (Chun Zhang)
  173. * <p>
  174. * Addresses Bug <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=13775" target="_bug">#13775</a>
  175. */
  176. @Test
  177. public final void testMultiNamedRange() throws Exception {
  178. // Create a new workbook
  179. Workbook wb1 = _testDataProvider.createWorkbook();
  180. // Create a worksheet 'sheet1' in the new workbook
  181. wb1.createSheet ();
  182. wb1.setSheetName (0, "sheet1");
  183. // Create another worksheet 'sheet2' in the new workbook
  184. wb1.createSheet ();
  185. wb1.setSheetName (1, "sheet2");
  186. // Create a new named range for worksheet 'sheet1'
  187. Name namedRange1 = wb1.createName();
  188. // Set the name for the named range for worksheet 'sheet1'
  189. namedRange1.setNameName("RangeTest1");
  190. // Set the reference for the named range for worksheet 'sheet1'
  191. namedRange1.setRefersToFormula("sheet1" + "!$A$1:$L$41");
  192. // Create a new named range for worksheet 'sheet2'
  193. Name namedRange2 = wb1.createName();
  194. // Set the name for the named range for worksheet 'sheet2'
  195. namedRange2.setNameName("RangeTest2");
  196. // Set the reference for the named range for worksheet 'sheet2'
  197. namedRange2.setRefersToFormula("sheet2" + "!$A$1:$O$21");
  198. // Write the workbook to a file
  199. // Read the Excel file and verify its content
  200. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  201. Name nm1 = wb2.getName("RangeTest1");
  202. assertTrue("Name is "+nm1.getNameName(),"RangeTest1".equals(nm1.getNameName()));
  203. assertTrue("Reference is "+nm1.getRefersToFormula(),(wb2.getSheetName(0)+"!$A$1:$L$41").equals(nm1.getRefersToFormula()));
  204. Name nm2 = wb2.getName("RangeTest2");
  205. assertTrue("Name is "+nm2.getNameName(),"RangeTest2".equals(nm2.getNameName()));
  206. assertTrue("Reference is "+nm2.getRefersToFormula(),(wb2.getSheetName(1)+"!$A$1:$O$21").equals(nm2.getRefersToFormula()));
  207. wb2.close();
  208. wb1.close();
  209. }
  210. /**
  211. * Test to see if the print areas can be retrieved/created in memory
  212. */
  213. @Test
  214. public final void testSinglePrintArea() throws Exception {
  215. Workbook workbook = _testDataProvider.createWorkbook();
  216. workbook.createSheet("Test Print Area");
  217. String sheetName = workbook.getSheetName(0);
  218. String reference = "$A$1:$B$1";
  219. workbook.setPrintArea(0, reference);
  220. String retrievedPrintArea = workbook.getPrintArea(0);
  221. assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
  222. assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
  223. workbook.close();
  224. }
  225. /**
  226. * For Convenience, don't force sheet names to be used
  227. */
  228. @Test
  229. public final void testSinglePrintAreaWOSheet() throws Exception
  230. {
  231. Workbook workbook = _testDataProvider.createWorkbook();
  232. workbook.createSheet("Test Print Area");
  233. String sheetName = workbook.getSheetName(0);
  234. String reference = "$A$1:$B$1";
  235. workbook.setPrintArea(0, reference);
  236. String retrievedPrintArea = workbook.getPrintArea(0);
  237. assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
  238. assertEquals("'" + sheetName + "'!" + reference, retrievedPrintArea);
  239. workbook.close();
  240. }
  241. /**
  242. * Test to see if the print area made it to the file
  243. */
  244. @Test
  245. public final void testPrintAreaFile() throws Exception {
  246. Workbook wb1 = _testDataProvider.createWorkbook();
  247. wb1.createSheet("Test Print Area");
  248. String sheetName = wb1.getSheetName(0);
  249. String reference = "$A$1:$B$1";
  250. wb1.setPrintArea(0, reference);
  251. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  252. String retrievedPrintArea = wb2.getPrintArea(0);
  253. assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
  254. assertEquals("References Match", "'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
  255. wb2.close();
  256. wb1.close();
  257. }
  258. /**
  259. * Test to see if multiple print areas made it to the file
  260. */
  261. @Test
  262. public final void testMultiplePrintAreaFile() throws Exception {
  263. Workbook wb1 = _testDataProvider.createWorkbook();
  264. wb1.createSheet("Sheet1");
  265. wb1.createSheet("Sheet2");
  266. wb1.createSheet("Sheet3");
  267. String reference1 = "$A$1:$B$1";
  268. String reference2 = "$B$2:$D$5";
  269. String reference3 = "$D$2:$F$5";
  270. wb1.setPrintArea(0, reference1);
  271. wb1.setPrintArea(1, reference2);
  272. wb1.setPrintArea(2, reference3);
  273. //Check created print areas
  274. String retrievedPrintArea;
  275. retrievedPrintArea = wb1.getPrintArea(0);
  276. assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
  277. assertEquals("Sheet1!" + reference1, retrievedPrintArea);
  278. retrievedPrintArea = wb1.getPrintArea(1);
  279. assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea);
  280. assertEquals("Sheet2!" + reference2, retrievedPrintArea);
  281. retrievedPrintArea = wb1.getPrintArea(2);
  282. assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea);
  283. assertEquals("Sheet3!" + reference3, retrievedPrintArea);
  284. // Check print areas after re-reading workbook
  285. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  286. retrievedPrintArea = wb2.getPrintArea(0);
  287. assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
  288. assertEquals("Sheet1!" + reference1, retrievedPrintArea);
  289. retrievedPrintArea = wb2.getPrintArea(1);
  290. assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea);
  291. assertEquals("Sheet2!" + reference2, retrievedPrintArea);
  292. retrievedPrintArea = wb2.getPrintArea(2);
  293. assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea);
  294. assertEquals("Sheet3!" + reference3, retrievedPrintArea);
  295. wb2.close();
  296. wb1.close();
  297. }
  298. /**
  299. * Tests the setting of print areas with coordinates (Row/Column designations)
  300. *
  301. */
  302. @Test
  303. public final void testPrintAreaCoords() throws Exception {
  304. Workbook workbook = _testDataProvider.createWorkbook();
  305. workbook.createSheet("Test Print Area");
  306. String sheetName = workbook.getSheetName(0);
  307. workbook.setPrintArea(0, 0, 1, 0, 0);
  308. String retrievedPrintArea = workbook.getPrintArea(0);
  309. assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
  310. assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
  311. workbook.close();
  312. }
  313. /**
  314. * Tests the parsing of union area expressions, and re-display in the presence of sheet names
  315. * with special characters.
  316. */
  317. @Test
  318. public final void testPrintAreaUnion() throws Exception {
  319. Workbook workbook = _testDataProvider.createWorkbook();
  320. workbook.createSheet("Test Print Area");
  321. String reference = "$A$1:$B$1,$D$1:$F$2";
  322. workbook.setPrintArea(0, reference);
  323. String retrievedPrintArea = workbook.getPrintArea(0);
  324. assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
  325. assertEquals("'Test Print Area'!$A$1:$B$1,'Test Print Area'!$D$1:$F$2", retrievedPrintArea);
  326. workbook.close();
  327. }
  328. /**
  329. * Verifies an existing print area is deleted
  330. *
  331. */
  332. @Test
  333. public final void testPrintAreaRemove() throws Exception {
  334. Workbook workbook = _testDataProvider.createWorkbook();
  335. workbook.createSheet("Test Print Area");
  336. workbook.getSheetName(0);
  337. workbook.setPrintArea(0, 0, 1, 0, 0);
  338. String retrievedPrintArea = workbook.getPrintArea(0);
  339. assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
  340. workbook.removePrintArea(0);
  341. assertNull("PrintArea was not removed", workbook.getPrintArea(0));
  342. workbook.close();
  343. }
  344. /**
  345. * Test that multiple named ranges can be added written and read
  346. */
  347. @Test
  348. public final void testMultipleNamedWrite() throws Exception {
  349. Workbook wb1 = _testDataProvider.createWorkbook();
  350. wb1.createSheet("testSheet1");
  351. String sheetName = wb1.getSheetName(0);
  352. assertEquals("testSheet1", sheetName);
  353. //Creating new Named Range
  354. Name newNamedRange = wb1.createName();
  355. newNamedRange.setNameName("RangeTest");
  356. newNamedRange.setRefersToFormula(sheetName + "!$D$4:$E$8");
  357. //Creating another new Named Range
  358. Name newNamedRange2 = wb1.createName();
  359. newNamedRange2.setNameName("AnotherTest");
  360. newNamedRange2.setRefersToFormula(sheetName + "!$F$1:$G$6");
  361. wb1.getNameAt(0);
  362. Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
  363. Name nm =wb2.getName("RangeTest");
  364. assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName()));
  365. assertTrue("Reference is "+nm.getRefersToFormula(),(wb2.getSheetName(0)+"!$D$4:$E$8").equals(nm.getRefersToFormula()));
  366. nm = wb2.getName("AnotherTest");
  367. assertTrue("Name is "+nm.getNameName(),"AnotherTest".equals(nm.getNameName()));
  368. assertTrue("Reference is "+nm.getRefersToFormula(),newNamedRange2.getRefersToFormula().equals(nm.getRefersToFormula()));
  369. wb2.close();
  370. wb1.close();
  371. }
  372. /**
  373. * Verifies correct functioning for "single cell named range" (aka "named cell")
  374. */
  375. @Test
  376. public final void testNamedCell_1() throws Exception {
  377. // setup for this testcase
  378. String sheetName = "Test Named Cell";
  379. String cellName = "named_cell";
  380. String cellValue = "TEST Value";
  381. Workbook wb = _testDataProvider.createWorkbook();
  382. Sheet sheet = wb.createSheet(sheetName);
  383. CreationHelper factory = wb.getCreationHelper();
  384. sheet.createRow(0).createCell(0).setCellValue(factory.createRichTextString(cellValue));
  385. // create named range for a single cell using areareference
  386. Name namedCell = wb.createName();
  387. namedCell.setNameName(cellName);
  388. String reference = "'" + sheetName + "'" + "!A1:A1";
  389. namedCell.setRefersToFormula(reference);
  390. // retrieve the newly created named range
  391. Name aNamedCell = wb.getName(cellName);
  392. assertNotNull(aNamedCell);
  393. // retrieve the cell at the named range and test its contents
  394. @SuppressWarnings("deprecation")
  395. AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
  396. assertTrue("Should be exactly 1 cell in the named cell :'" +cellName+"'", aref.isSingleCell());
  397. CellReference cref = aref.getFirstCell();
  398. assertNotNull(cref);
  399. Sheet s = wb.getSheet(cref.getSheetName());
  400. assertNotNull(s);
  401. Row r = sheet.getRow(cref.getRow());
  402. Cell c = r.getCell(cref.getCol());
  403. String contents = c.getRichStringCellValue().getString();
  404. assertEquals("Contents of cell retrieved by its named reference", contents, cellValue);
  405. wb.close();
  406. }
  407. /**
  408. * Verifies correct functioning for "single cell named range" (aka "named cell")
  409. */
  410. @Test
  411. public final void testNamedCell_2() throws Exception {
  412. // setup for this testcase
  413. String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
  414. Workbook wb = _testDataProvider.createWorkbook();
  415. CreationHelper factory = wb.getCreationHelper();
  416. Sheet sheet = wb.createSheet(sname);
  417. sheet.createRow(0).createCell(0).setCellValue(factory.createRichTextString(cvalue));
  418. // create named range for a single cell using cellreference
  419. Name namedCell = wb.createName();
  420. namedCell.setNameName(cname);
  421. String reference = sname+"!A1";
  422. namedCell.setRefersToFormula(reference);
  423. // retrieve the newly created named range
  424. Name aNamedCell = wb.getName(cname);
  425. assertNotNull(aNamedCell);
  426. // retrieve the cell at the named range and test its contents
  427. CellReference cref = new CellReference(aNamedCell.getRefersToFormula());
  428. assertNotNull(cref);
  429. Sheet s = wb.getSheet(cref.getSheetName());
  430. assertNotNull(s);
  431. Row r = sheet.getRow(cref.getRow());
  432. Cell c = r.getCell(cref.getCol());
  433. String contents = c.getRichStringCellValue().getString();
  434. assertEquals("Contents of cell retrieved by its named reference", contents, cvalue);
  435. wb.close();
  436. }
  437. /**
  438. * Bugzilla attachment 23444 (from bug 46973) has a NAME record with the following encoding:
  439. * <pre>
  440. * 00000000 | 18 00 17 00 00 00 00 08 00 00 00 00 00 00 00 00 | ................
  441. * 00000010 | 00 00 00 55 50 53 53 74 61 74 65 | ...UPSState
  442. * </pre>
  443. *
  444. * This caused trouble for anything that requires {@link Name#getRefersToFormula()}
  445. * It is easy enough to re-create the the same data (by not setting the formula). Excel
  446. * seems to gracefully remove this uninitialized name record. It would be nice if POI
  447. * could do the same, but that would involve adjusting subsequent name indexes across
  448. * all formulas. <p/>
  449. *
  450. * For the moment, POI has been made to behave more sensibly with uninitialized name
  451. * records.
  452. */
  453. @Test
  454. public final void testUninitialisedNameGetRefersToFormula_bug46973() throws Exception {
  455. Workbook wb = _testDataProvider.createWorkbook();
  456. Name n = wb.createName();
  457. n.setNameName("UPSState");
  458. String formula = n.getRefersToFormula();
  459. // bug 46973: fails here with IllegalArgumentException
  460. // ptgs must not be null
  461. assertNull(formula);
  462. // according to exact definition of isDeleted()
  463. assertFalse(n.isDeleted());
  464. wb.close();
  465. }
  466. @Test
  467. public final void testDeletedCell() throws Exception {
  468. Workbook wb = _testDataProvider.createWorkbook();
  469. Name n = wb.createName();
  470. n.setNameName("MyName");
  471. // contrived example to expose bug:
  472. n.setRefersToFormula("if(A1,\"#REF!\", \"\")");
  473. assertFalse("Identified bug in recoginising formulas referring to deleted cells", n.isDeleted());
  474. wb.close();
  475. }
  476. @Test
  477. public final void testFunctionNames() throws Exception {
  478. Workbook wb = _testDataProvider.createWorkbook();
  479. Name n = wb.createName();
  480. assertFalse(n.isFunctionName());
  481. n.setFunction(false);
  482. assertFalse(n.isFunctionName());
  483. n.setFunction(true);
  484. assertTrue(n.isFunctionName());
  485. n.setFunction(false);
  486. assertFalse(n.isFunctionName());
  487. wb.close();
  488. }
  489. @Test
  490. public final void testDefferedSetting() throws Exception {
  491. Workbook wb = _testDataProvider.createWorkbook();
  492. Name n1 = wb.createName();
  493. assertNull(n1.getRefersToFormula());
  494. assertEquals("", n1.getNameName());
  495. Name n2 = wb.createName();
  496. assertNull(n2.getRefersToFormula());
  497. assertEquals("", n2.getNameName());
  498. n1.setNameName("sale_1");
  499. n1.setRefersToFormula("10");
  500. n2.setNameName("sale_2");
  501. n2.setRefersToFormula("20");
  502. try {
  503. n2.setNameName("sale_1");
  504. fail("Expected exception");
  505. } catch(Exception e){
  506. assertEquals("The workbook already contains this name: sale_1", e.getMessage());
  507. }
  508. wb.close();
  509. }
  510. @Test
  511. public void testBug56930() throws IOException {
  512. Workbook wb = _testDataProvider.createWorkbook();
  513. // x1 on sheet1 defines "x=1"
  514. wb.createSheet("sheet1");
  515. Name x1 = wb.createName();
  516. x1.setNameName("x");
  517. x1.setRefersToFormula("1");
  518. x1.setSheetIndex(wb.getSheetIndex("sheet1"));
  519. // x2 on sheet2 defines "x=2"
  520. wb.createSheet("sheet2");
  521. Name x2 = wb.createName();
  522. x2.setNameName("x");
  523. x2.setRefersToFormula("2");
  524. x2.setSheetIndex(wb.getSheetIndex("sheet2"));
  525. List<? extends Name> names = wb.getNames("x");
  526. assertEquals("Had: " + names, 2, names.size());
  527. assertEquals("1", names.get(0).getRefersToFormula());
  528. assertEquals("2", names.get(1).getRefersToFormula());
  529. assertEquals("1", wb.getName("x").getRefersToFormula());
  530. wb.removeName("x");
  531. assertEquals("2", wb.getName("x").getRefersToFormula());
  532. wb.close();
  533. }
  534. // bug 56781: name validation only checks for first character's validity and presence of spaces
  535. // bug 60246: validate name does not allow DOT in named ranges
  536. @Test
  537. public void testValid() throws IOException {
  538. Workbook wb = _testDataProvider.createWorkbook();
  539. Name name = wb.createName();
  540. for (String valid : Arrays.asList(
  541. "Hello",
  542. "number1",
  543. "_underscore",
  544. "underscore_",
  545. "p.e.r.o.i.d.s",
  546. "\\Backslash",
  547. "Backslash\\"
  548. )) {
  549. name.setNameName(valid);
  550. }
  551. wb.close();
  552. }
  553. @Test
  554. public void testInvalid() throws IOException {
  555. Workbook wb = _testDataProvider.createWorkbook();
  556. Name name = wb.createName();
  557. try {
  558. name.setNameName("");
  559. fail("expected exception: (blank)");
  560. } catch (final IllegalArgumentException e) {
  561. assertEquals("Name cannot be blank", e.getMessage());
  562. }
  563. for (String invalid : Arrays.asList(
  564. "1number",
  565. "Sheet1!A1",
  566. "Exclamation!",
  567. "Has Space",
  568. "Colon:",
  569. "A-Minus",
  570. "A+Plus",
  571. "Dollar$",
  572. ".periodAtBeginning",
  573. "R", //special shorthand
  574. "C", //special shorthand
  575. "A1", // A1-style cell reference
  576. "R1C1", // R1C1-style cell reference
  577. "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
  578. "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
  579. "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
  580. "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
  581. "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters"
  582. )) {
  583. try {
  584. name.setNameName(invalid);
  585. fail("expected exception: " + invalid);
  586. } catch (final IllegalArgumentException e) {
  587. assertTrue(invalid,
  588. e.getMessage().startsWith("Invalid name: '"+invalid+"'"));
  589. }
  590. }
  591. }
  592. // bug 60260: renaming a sheet with a named range referring to a unicode (non-ASCII) sheet name
  593. @Test
  594. public void renameSheetWithNamedRangeReferringToUnicodeSheetName() {
  595. Workbook wb = _testDataProvider.createWorkbook();
  596. wb.createSheet("Sheet\u30FB1");
  597. Name name = wb.createName();
  598. name.setNameName("test_named_range");
  599. name.setRefersToFormula("'Sheet\u30FB201'!A1:A6");
  600. wb.setSheetName(0, "Sheet 1");
  601. IOUtils.closeQuietly(wb);
  602. }
  603. }