123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755 |
- /* ====================================================================
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ==================================================================== */
-
- package org.apache.poi.ss.usermodel;
-
- import static org.junit.Assert.assertEquals;
- import static org.junit.Assert.assertFalse;
- import static org.junit.Assert.assertNotNull;
- import static org.junit.Assert.assertNull;
- import static org.junit.Assert.assertTrue;
- import static org.junit.Assert.fail;
-
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.List;
-
- import org.apache.poi.ss.ITestDataProvider;
- import org.apache.poi.ss.util.AreaReference;
- import org.apache.poi.ss.util.CellReference;
- import org.apache.poi.util.IOUtils;
- import org.junit.Test;
-
- /**
- * Tests of implementations of {@link org.apache.poi.ss.usermodel.Name}.
- *
- * @author Yegor Kozlov
- */
- public abstract class BaseTestNamedRange {
-
- private final ITestDataProvider _testDataProvider;
-
- protected BaseTestNamedRange(ITestDataProvider testDataProvider) {
- _testDataProvider = testDataProvider;
- }
-
- @Test
- public final void testCreate() throws Exception {
- // Create a new workbook
- Workbook wb = _testDataProvider.createWorkbook();
- wb.createSheet("Test1");
- wb.createSheet("Testing Named Ranges");
-
- Name name1 = wb.createName();
- name1.setNameName("testOne");
-
- //setting a duplicate name should throw IllegalArgumentException
- Name name2 = wb.createName();
- try {
- name2.setNameName("testOne");
- fail("expected exception");
- } catch (IllegalArgumentException e){
- assertEquals("The workbook already contains this name: testOne", e.getMessage());
- }
- //the check for duplicates is case-insensitive
- try {
- name2.setNameName("TESTone");
- fail("expected exception");
- } catch (IllegalArgumentException e){
- assertEquals("The workbook already contains this name: TESTone", e.getMessage());
- }
-
- name2.setNameName("testTwo");
-
- String ref1 = "Test1!$A$1:$B$1";
- name1.setRefersToFormula(ref1);
- assertEquals(ref1, name1.getRefersToFormula());
- assertEquals("Test1", name1.getSheetName());
-
- String ref2 = "'Testing Named Ranges'!$A$1:$B$1";
- name1.setRefersToFormula(ref2);
- assertEquals("'Testing Named Ranges'!$A$1:$B$1", name1.getRefersToFormula());
- assertEquals("Testing Named Ranges", name1.getSheetName());
-
- assertEquals(-1, name1.getSheetIndex());
- name1.setSheetIndex(-1);
- assertEquals(-1, name1.getSheetIndex());
- try {
- name1.setSheetIndex(2);
- fail("should throw IllegalArgumentException");
- } catch(IllegalArgumentException e){
- assertEquals("Sheet index (2) is out of range (0..1)", e.getMessage());
- }
-
- name1.setSheetIndex(1);
- assertEquals(1, name1.getSheetIndex());
-
- //-1 means the name applies to the entire workbook
- name1.setSheetIndex(-1);
- assertEquals(-1, name1.getSheetIndex());
-
- //names cannot be blank and must begin with a letter or underscore and not contain spaces
- String[] invalidNames = {"", "123", "1Name", "Named Range"};
- for (String name : invalidNames) {
- try {
- name1.setNameName(name);
- fail("should have thrown exceptiuon due to invalid name: " + name);
- } catch (IllegalArgumentException e) {
- // expected during successful test
- }
- }
-
- wb.close();
- }
-
- @Test
- public final void testUnicodeNamedRange() throws Exception {
- Workbook wb1 = _testDataProvider.createWorkbook();
- wb1.createSheet("Test");
- Name name = wb1.createName();
- name.setNameName("\u03B1");
- name.setRefersToFormula("Test!$D$3:$E$8");
-
-
- Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
- Name name2 = wb2.getNameAt(0);
-
- assertEquals("\u03B1", name2.getNameName());
- assertEquals("Test!$D$3:$E$8", name2.getRefersToFormula());
-
- wb2.close();
- wb1.close();
- }
-
- @Test
- public final void testAddRemove() throws Exception {
- Workbook wb = _testDataProvider.createWorkbook();
- assertEquals(0, wb.getNumberOfNames());
- Name name1 = wb.createName();
- name1.setNameName("name1");
- assertEquals(1, wb.getNumberOfNames());
-
- Name name2 = wb.createName();
- name2.setNameName("name2");
- assertEquals(2, wb.getNumberOfNames());
-
- Name name3 = wb.createName();
- name3.setNameName("name3");
- assertEquals(3, wb.getNumberOfNames());
-
- wb.removeName("name2");
- assertEquals(2, wb.getNumberOfNames());
-
- wb.removeName(0);
- assertEquals(1, wb.getNumberOfNames());
-
- wb.close();
- }
-
- @Test
- public final void testScope() throws Exception {
- Workbook wb = _testDataProvider.createWorkbook();
- wb.createSheet();
- wb.createSheet();
-
- Name name;
-
- name = wb.createName();
- name.setNameName("aaa");
- name = wb.createName();
- try {
- name.setNameName("aaa");
- fail("Expected exception");
- } catch(Exception e){
- assertEquals("The workbook already contains this name: aaa", e.getMessage());
- }
-
- name = wb.createName();
- name.setSheetIndex(0);
- name.setNameName("aaa");
- name = wb.createName();
- name.setSheetIndex(0);
- try {
- name.setNameName("aaa");
- fail("Expected exception");
- } catch(Exception e){
- assertEquals("The sheet already contains this name: aaa", e.getMessage());
- }
-
- name = wb.createName();
- name.setSheetIndex(1);
- name.setNameName("aaa");
- name = wb.createName();
- name.setSheetIndex(1);
- try {
- name.setNameName("aaa");
- fail("Expected exception");
- } catch(Exception e){
- assertEquals("The sheet already contains this name: aaa", e.getMessage());
- }
-
- assertEquals(3, wb.getNames("aaa").size());
-
- wb.close();
- }
-
- /**
- * Test case provided by czhang@cambian.com (Chun Zhang)
- * <p>
- * Addresses Bug <a href="http://issues.apache.org/bugzilla/show_bug.cgi?id=13775" target="_bug">#13775</a>
- */
- @Test
- public final void testMultiNamedRange() throws Exception {
-
- // Create a new workbook
- Workbook wb1 = _testDataProvider.createWorkbook();
-
- // Create a worksheet 'sheet1' in the new workbook
- wb1.createSheet ();
- wb1.setSheetName (0, "sheet1");
-
- // Create another worksheet 'sheet2' in the new workbook
- wb1.createSheet ();
- wb1.setSheetName (1, "sheet2");
-
- // Create a new named range for worksheet 'sheet1'
- Name namedRange1 = wb1.createName();
-
- // Set the name for the named range for worksheet 'sheet1'
- namedRange1.setNameName("RangeTest1");
-
- // Set the reference for the named range for worksheet 'sheet1'
- namedRange1.setRefersToFormula("sheet1" + "!$A$1:$L$41");
-
- // Create a new named range for worksheet 'sheet2'
- Name namedRange2 = wb1.createName();
-
- // Set the name for the named range for worksheet 'sheet2'
- namedRange2.setNameName("RangeTest2");
-
- // Set the reference for the named range for worksheet 'sheet2'
- namedRange2.setRefersToFormula("sheet2" + "!$A$1:$O$21");
-
- // Write the workbook to a file
- // Read the Excel file and verify its content
- Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
- Name nm1 = wb2.getName("RangeTest1");
- assertTrue("Name is "+nm1.getNameName(),"RangeTest1".equals(nm1.getNameName()));
- assertTrue("Reference is "+nm1.getRefersToFormula(),(wb2.getSheetName(0)+"!$A$1:$L$41").equals(nm1.getRefersToFormula()));
-
- Name nm2 = wb2.getName("RangeTest2");
- assertTrue("Name is "+nm2.getNameName(),"RangeTest2".equals(nm2.getNameName()));
- assertTrue("Reference is "+nm2.getRefersToFormula(),(wb2.getSheetName(1)+"!$A$1:$O$21").equals(nm2.getRefersToFormula()));
-
- wb2.close();
- wb1.close();
- }
-
- /**
- * Test to see if the print areas can be retrieved/created in memory
- */
- @Test
- public final void testSinglePrintArea() throws Exception {
- Workbook workbook = _testDataProvider.createWorkbook();
- workbook.createSheet("Test Print Area");
- String sheetName = workbook.getSheetName(0);
-
- String reference = "$A$1:$B$1";
- workbook.setPrintArea(0, reference);
-
- String retrievedPrintArea = workbook.getPrintArea(0);
-
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
- assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
-
- workbook.close();
- }
-
- /**
- * For Convenience, don't force sheet names to be used
- */
- @Test
- public final void testSinglePrintAreaWOSheet() throws Exception
- {
- Workbook workbook = _testDataProvider.createWorkbook();
- workbook.createSheet("Test Print Area");
- String sheetName = workbook.getSheetName(0);
-
- String reference = "$A$1:$B$1";
- workbook.setPrintArea(0, reference);
-
- String retrievedPrintArea = workbook.getPrintArea(0);
-
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
- assertEquals("'" + sheetName + "'!" + reference, retrievedPrintArea);
-
- workbook.close();
- }
-
- /**
- * Test to see if the print area made it to the file
- */
- @Test
- public final void testPrintAreaFile() throws Exception {
- Workbook wb1 = _testDataProvider.createWorkbook();
- wb1.createSheet("Test Print Area");
- String sheetName = wb1.getSheetName(0);
-
-
- String reference = "$A$1:$B$1";
- wb1.setPrintArea(0, reference);
-
- Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
-
- String retrievedPrintArea = wb2.getPrintArea(0);
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
- assertEquals("References Match", "'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
-
- wb2.close();
- wb1.close();
- }
-
- /**
- * Test to see if multiple print areas made it to the file
- */
- @Test
- public final void testMultiplePrintAreaFile() throws Exception {
- Workbook wb1 = _testDataProvider.createWorkbook();
-
- wb1.createSheet("Sheet1");
- wb1.createSheet("Sheet2");
- wb1.createSheet("Sheet3");
- String reference1 = "$A$1:$B$1";
- String reference2 = "$B$2:$D$5";
- String reference3 = "$D$2:$F$5";
-
- wb1.setPrintArea(0, reference1);
- wb1.setPrintArea(1, reference2);
- wb1.setPrintArea(2, reference3);
-
- //Check created print areas
- String retrievedPrintArea;
-
- retrievedPrintArea = wb1.getPrintArea(0);
- assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
- assertEquals("Sheet1!" + reference1, retrievedPrintArea);
-
- retrievedPrintArea = wb1.getPrintArea(1);
- assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea);
- assertEquals("Sheet2!" + reference2, retrievedPrintArea);
-
- retrievedPrintArea = wb1.getPrintArea(2);
- assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea);
- assertEquals("Sheet3!" + reference3, retrievedPrintArea);
-
- // Check print areas after re-reading workbook
- Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
-
- retrievedPrintArea = wb2.getPrintArea(0);
- assertNotNull("Print Area Not Found (Sheet 1)", retrievedPrintArea);
- assertEquals("Sheet1!" + reference1, retrievedPrintArea);
-
- retrievedPrintArea = wb2.getPrintArea(1);
- assertNotNull("Print Area Not Found (Sheet 2)", retrievedPrintArea);
- assertEquals("Sheet2!" + reference2, retrievedPrintArea);
-
- retrievedPrintArea = wb2.getPrintArea(2);
- assertNotNull("Print Area Not Found (Sheet 3)", retrievedPrintArea);
- assertEquals("Sheet3!" + reference3, retrievedPrintArea);
-
- wb2.close();
- wb1.close();
- }
-
- /**
- * Tests the setting of print areas with coordinates (Row/Column designations)
- *
- */
- @Test
- public final void testPrintAreaCoords() throws Exception {
- Workbook workbook = _testDataProvider.createWorkbook();
- workbook.createSheet("Test Print Area");
- String sheetName = workbook.getSheetName(0);
-
- workbook.setPrintArea(0, 0, 1, 0, 0);
-
- String retrievedPrintArea = workbook.getPrintArea(0);
-
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
- assertEquals("'" + sheetName + "'!$A$1:$B$1", retrievedPrintArea);
-
- workbook.close();
- }
-
-
- /**
- * Tests the parsing of union area expressions, and re-display in the presence of sheet names
- * with special characters.
- */
- @Test
- public final void testPrintAreaUnion() throws Exception {
- Workbook workbook = _testDataProvider.createWorkbook();
- workbook.createSheet("Test Print Area");
-
- String reference = "$A$1:$B$1,$D$1:$F$2";
- workbook.setPrintArea(0, reference);
- String retrievedPrintArea = workbook.getPrintArea(0);
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
- assertEquals("'Test Print Area'!$A$1:$B$1,'Test Print Area'!$D$1:$F$2", retrievedPrintArea);
-
- workbook.close();
- }
-
- /**
- * Verifies an existing print area is deleted
- *
- */
- @Test
- public final void testPrintAreaRemove() throws Exception {
- Workbook workbook = _testDataProvider.createWorkbook();
- workbook.createSheet("Test Print Area");
- workbook.getSheetName(0);
-
- workbook.setPrintArea(0, 0, 1, 0, 0);
-
- String retrievedPrintArea = workbook.getPrintArea(0);
-
- assertNotNull("Print Area not defined for first sheet", retrievedPrintArea);
-
- workbook.removePrintArea(0);
- assertNull("PrintArea was not removed", workbook.getPrintArea(0));
- workbook.close();
- }
-
- /**
- * Test that multiple named ranges can be added written and read
- */
- @Test
- public final void testMultipleNamedWrite() throws Exception {
- Workbook wb1 = _testDataProvider.createWorkbook();
-
-
- wb1.createSheet("testSheet1");
- String sheetName = wb1.getSheetName(0);
-
- assertEquals("testSheet1", sheetName);
-
- //Creating new Named Range
- Name newNamedRange = wb1.createName();
-
- newNamedRange.setNameName("RangeTest");
- newNamedRange.setRefersToFormula(sheetName + "!$D$4:$E$8");
-
- //Creating another new Named Range
- Name newNamedRange2 = wb1.createName();
-
- newNamedRange2.setNameName("AnotherTest");
- newNamedRange2.setRefersToFormula(sheetName + "!$F$1:$G$6");
-
- wb1.getNameAt(0);
-
- Workbook wb2 = _testDataProvider.writeOutAndReadBack(wb1);
- Name nm =wb2.getName("RangeTest");
- assertTrue("Name is "+nm.getNameName(),"RangeTest".equals(nm.getNameName()));
- assertTrue("Reference is "+nm.getRefersToFormula(),(wb2.getSheetName(0)+"!$D$4:$E$8").equals(nm.getRefersToFormula()));
-
- nm = wb2.getName("AnotherTest");
- assertTrue("Name is "+nm.getNameName(),"AnotherTest".equals(nm.getNameName()));
- assertTrue("Reference is "+nm.getRefersToFormula(),newNamedRange2.getRefersToFormula().equals(nm.getRefersToFormula()));
-
- wb2.close();
- wb1.close();
- }
- /**
- * Verifies correct functioning for "single cell named range" (aka "named cell")
- */
- @Test
- public final void testNamedCell_1() throws Exception {
-
- // setup for this testcase
- String sheetName = "Test Named Cell";
- String cellName = "named_cell";
- String cellValue = "TEST Value";
- Workbook wb = _testDataProvider.createWorkbook();
- Sheet sheet = wb.createSheet(sheetName);
- CreationHelper factory = wb.getCreationHelper();
- sheet.createRow(0).createCell(0).setCellValue(factory.createRichTextString(cellValue));
-
- // create named range for a single cell using areareference
- Name namedCell = wb.createName();
- namedCell.setNameName(cellName);
- String reference = "'" + sheetName + "'" + "!A1:A1";
- namedCell.setRefersToFormula(reference);
-
- // retrieve the newly created named range
- Name aNamedCell = wb.getName(cellName);
- assertNotNull(aNamedCell);
-
- // retrieve the cell at the named range and test its contents
- @SuppressWarnings("deprecation")
- AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
- assertTrue("Should be exactly 1 cell in the named cell :'" +cellName+"'", aref.isSingleCell());
-
- CellReference cref = aref.getFirstCell();
- assertNotNull(cref);
- Sheet s = wb.getSheet(cref.getSheetName());
- assertNotNull(s);
- Row r = sheet.getRow(cref.getRow());
- Cell c = r.getCell(cref.getCol());
- String contents = c.getRichStringCellValue().getString();
- assertEquals("Contents of cell retrieved by its named reference", contents, cellValue);
-
- wb.close();
- }
-
- /**
- * Verifies correct functioning for "single cell named range" (aka "named cell")
- */
- @Test
- public final void testNamedCell_2() throws Exception {
-
- // setup for this testcase
- String sname = "TestSheet", cname = "TestName", cvalue = "TestVal";
- Workbook wb = _testDataProvider.createWorkbook();
- CreationHelper factory = wb.getCreationHelper();
- Sheet sheet = wb.createSheet(sname);
- sheet.createRow(0).createCell(0).setCellValue(factory.createRichTextString(cvalue));
-
- // create named range for a single cell using cellreference
- Name namedCell = wb.createName();
- namedCell.setNameName(cname);
- String reference = sname+"!A1";
- namedCell.setRefersToFormula(reference);
-
- // retrieve the newly created named range
- Name aNamedCell = wb.getName(cname);
- assertNotNull(aNamedCell);
-
- // retrieve the cell at the named range and test its contents
- CellReference cref = new CellReference(aNamedCell.getRefersToFormula());
- assertNotNull(cref);
- Sheet s = wb.getSheet(cref.getSheetName());
- assertNotNull(s);
- Row r = sheet.getRow(cref.getRow());
- Cell c = r.getCell(cref.getCol());
- String contents = c.getRichStringCellValue().getString();
- assertEquals("Contents of cell retrieved by its named reference", contents, cvalue);
-
- wb.close();
- }
-
-
- /**
- * Bugzilla attachment 23444 (from bug 46973) has a NAME record with the following encoding:
- * <pre>
- * 00000000 | 18 00 17 00 00 00 00 08 00 00 00 00 00 00 00 00 | ................
- * 00000010 | 00 00 00 55 50 53 53 74 61 74 65 | ...UPSState
- * </pre>
- *
- * This caused trouble for anything that requires {@link Name#getRefersToFormula()}
- * It is easy enough to re-create the the same data (by not setting the formula). Excel
- * seems to gracefully remove this uninitialized name record. It would be nice if POI
- * could do the same, but that would involve adjusting subsequent name indexes across
- * all formulas. <p/>
- *
- * For the moment, POI has been made to behave more sensibly with uninitialized name
- * records.
- */
- @Test
- public final void testUninitialisedNameGetRefersToFormula_bug46973() throws Exception {
- Workbook wb = _testDataProvider.createWorkbook();
- Name n = wb.createName();
- n.setNameName("UPSState");
- String formula = n.getRefersToFormula();
-
- // bug 46973: fails here with IllegalArgumentException
- // ptgs must not be null
-
- assertNull(formula);
- // according to exact definition of isDeleted()
- assertFalse(n.isDeleted());
- wb.close();
- }
-
- @Test
- public final void testDeletedCell() throws Exception {
- Workbook wb = _testDataProvider.createWorkbook();
- Name n = wb.createName();
- n.setNameName("MyName");
- // contrived example to expose bug:
- n.setRefersToFormula("if(A1,\"#REF!\", \"\")");
-
- assertFalse("Identified bug in recoginising formulas referring to deleted cells", n.isDeleted());
-
- wb.close();
- }
-
- @Test
- public final void testFunctionNames() throws Exception {
- Workbook wb = _testDataProvider.createWorkbook();
- Name n = wb.createName();
- assertFalse(n.isFunctionName());
-
- n.setFunction(false);
- assertFalse(n.isFunctionName());
-
- n.setFunction(true);
- assertTrue(n.isFunctionName());
-
- n.setFunction(false);
- assertFalse(n.isFunctionName());
-
- wb.close();
- }
-
- @Test
- public final void testDefferedSetting() throws Exception {
- Workbook wb = _testDataProvider.createWorkbook();
- Name n1 = wb.createName();
- assertNull(n1.getRefersToFormula());
- assertEquals("", n1.getNameName());
-
- Name n2 = wb.createName();
- assertNull(n2.getRefersToFormula());
- assertEquals("", n2.getNameName());
-
- n1.setNameName("sale_1");
- n1.setRefersToFormula("10");
-
- n2.setNameName("sale_2");
- n2.setRefersToFormula("20");
-
- try {
- n2.setNameName("sale_1");
- fail("Expected exception");
- } catch(Exception e){
- assertEquals("The workbook already contains this name: sale_1", e.getMessage());
- }
-
- wb.close();
- }
-
- @Test
- public void testBug56930() throws IOException {
- Workbook wb = _testDataProvider.createWorkbook();
-
- // x1 on sheet1 defines "x=1"
- wb.createSheet("sheet1");
- Name x1 = wb.createName();
-
- x1.setNameName("x");
- x1.setRefersToFormula("1");
- x1.setSheetIndex(wb.getSheetIndex("sheet1"));
-
- // x2 on sheet2 defines "x=2"
- wb.createSheet("sheet2");
- Name x2 = wb.createName();
- x2.setNameName("x");
- x2.setRefersToFormula("2");
- x2.setSheetIndex(wb.getSheetIndex("sheet2"));
-
- List<? extends Name> names = wb.getNames("x");
- assertEquals("Had: " + names, 2, names.size());
- assertEquals("1", names.get(0).getRefersToFormula());
- assertEquals("2", names.get(1).getRefersToFormula());
-
- assertEquals("1", wb.getName("x").getRefersToFormula());
- wb.removeName("x");
- assertEquals("2", wb.getName("x").getRefersToFormula());
-
- wb.close();
- }
-
- // bug 56781: name validation only checks for first character's validity and presence of spaces
- // bug 60246: validate name does not allow DOT in named ranges
- @Test
- public void testValid() throws IOException {
- Workbook wb = _testDataProvider.createWorkbook();
-
- Name name = wb.createName();
- for (String valid : Arrays.asList(
- "Hello",
- "number1",
- "_underscore",
- "underscore_",
- "p.e.r.o.i.d.s",
- "\\Backslash",
- "Backslash\\"
- )) {
- name.setNameName(valid);
- }
-
- wb.close();
- }
-
- @Test
- public void testInvalid() throws IOException {
- Workbook wb = _testDataProvider.createWorkbook();
-
- Name name = wb.createName();
- try {
- name.setNameName("");
- fail("expected exception: (blank)");
- } catch (final IllegalArgumentException e) {
- assertEquals("Name cannot be blank", e.getMessage());
- }
-
- for (String invalid : Arrays.asList(
- "1number",
- "Sheet1!A1",
- "Exclamation!",
- "Has Space",
- "Colon:",
- "A-Minus",
- "A+Plus",
- "Dollar$",
- ".periodAtBeginning",
- "R", //special shorthand
- "C", //special shorthand
- "A1", // A1-style cell reference
- "R1C1", // R1C1-style cell reference
- "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
- "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
- "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
- "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters..."+
- "NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters.NameThatIsLongerThan255Characters"
- )) {
- try {
- name.setNameName(invalid);
- fail("expected exception: " + invalid);
- } catch (final IllegalArgumentException e) {
- assertTrue(invalid,
- e.getMessage().startsWith("Invalid name: '"+invalid+"'"));
- }
- }
-
- }
-
- // bug 60260: renaming a sheet with a named range referring to a unicode (non-ASCII) sheet name
- @Test
- public void renameSheetWithNamedRangeReferringToUnicodeSheetName() {
- Workbook wb = _testDataProvider.createWorkbook();
- wb.createSheet("Sheet\u30FB1");
-
- Name name = wb.createName();
- name.setNameName("test_named_range");
- name.setRefersToFormula("'Sheet\u30FB201'!A1:A6");
-
- wb.setSheetName(0, "Sheet 1");
- IOUtils.closeQuietly(wb);
- }
- }
|