Document Solutions for Excel, Java Edition | Document Solutions
Features / Defined Names
In This Topic
    Defined Names
    In This Topic

    Defined names refer to names given to constants, tables, cell ranges, or formulas so that you can refer to them in a formula without making it too complex to understand. The defined names are especially useful in complex calculations, such as calculating taxes for a whole financial year, where you will have difficulty finding and understanding the cells having different investments, taxable incomes, etc.

    DsExcel supports defined names with the help of getNames and setNames methods in IWorkbook and IWorksheet interfaces and getName and setName methods in ITable interface.

    Name a Table

    Name a table by using getName method of ITable interface. The scope of the table name is workbook by default, as tables are created in workbook scope only. This name appears in Excel's Name Manager, as shown below.

    Refer to the following example code to name the table:

    Java
    Copy Code
    // Name a table.
    ITable table = worksheet.getTables().add(worksheet.getRange("A1:F7"), true);
    table.setName("Master Table");

    Name a Cell Range, Formula, and Constant

    Name a cell range, formula, and constant using the getNames method with Workbook and Worksheet objects. This method adds an IName object storing the name and referenced cell, formula, or constant. The name added to a workbook object is stored in the workbook scope, while the name added to a worksheet object is saved in the worksheet scope. It appears in Excel¡¯s Name Manager, as shown below.

    Refer to the following example code to name a cell range, a formula, and a constant in workbook scope:

    Java
    Copy Code
    // Name a range in workbook scope.
    workbook.getNames().add("Range1", "Data Sheet!A1:C7");
    
    // Name formula in workbook scope.
    workbook.getNames().add("Sum", "=SUM(F2:F7)");
    
    // Name a constant in workbook scope.
    workbook.getNames().add("pi", "3.14");
    
    // Name a cell in workbook scope.
    workbook.getNames().add("Weight", "E1");

    Refer to the following example code to name a cell range, a formula, and a constant in worksheet scope:

    Java
    Copy Code
    // Name a range in worksheet scope.
    workbook.getWorksheets().get(worksheet.getIndex()).getNames().add("Range2", "Data Sheet!D1:F7");
    
    // Name formula in worksheet scope.
    workbook.getWorksheets().get(worksheet.getIndex()).getNames().add("Count", "=COUNT(E2:E7)");
    
    // Name a constant in worksheet scope.
    workbook.getWorksheets().get(worksheet.getIndex()).getNames().add("Euler_Number", "2.71");
    
    // Name a cell in worksheet scope.
    workbook.getWorksheets().get(worksheet.getIndex()).getNames().add("Height", "F1");