Spread WPF 18
Features / Formulas and Functions / Define Custom Names
In This Topic
    Define Custom Names
    In This Topic

    Spread for WPF allows you to define custom names as identifiers to represent specific information. These custom names can reference cells, cell ranges, calculated values, or formulas. During evaluation, the formula processes the referenced name and returns the result to the formula's owner object.

    Custom names can be defined at either the workbook level or the worksheet level, allowing flexibility in scope. Once defined, these custom names can be used in formulas to simplify referencing and enhance formula readability.

    Add Custom Name in Workbook

    To create a custom name for a workbook, access the specified workbook and use the Add method of the INames interface. Once created, the custom name can be used in formulas for easier referencing and improved readability.

    The following image illustrates the custom name defined for the formula.

     

    Refer to the following example code to define custom name in the workbook.

    Copy Code
    // Create custom name and set as formula
    spreadSheet1.Workbook.Names.Add("TotalSales", "SUM(B2:B6)", null, 7, 2, null);
    spreadSheet1.Workbook.ActiveSheet.Cells["C8"].Formula = "=TotalSales";
    spreadSheet1.Workbook.ActiveSheet.Cells["C8"].Activate();
    
    Copy Code
    ' Create custom name and set as formula
    spreadSheet1.Workbook.Names.Add("TotalSales", "SUM(B2:B6)", Nothing, 7, 2, Nothing)
    spreadSheet1.Workbook.ActiveSheet.Cells("C8").Formula = "=TotalSales"
    spreadSheet1.Workbook.ActiveSheet.Cells("C8").Activate()
    

    Add Custom Name in Worksheet

    To create a custom name for a worksheet, access the specified worksheet and use the Add method of the INames interface. When creating a custom name for a specific worksheet cell (e.g., cell A1 in worksheet [0]), you can reference it explicitly as "=!A1" or "=Sheet1!A1". Using "=A1" will associate the custom name with cell A1 of the currently active worksheet, rather than a specific one.

    The following GIF illustrates how to define custom names for the "Total Sales" and "Total Sales (excluding Charles)" formulas within the worksheet.

    Refer to the following example code to define a custom name in the worksheet.

    Copy Code
    // Add custom name in the worksheet.
    spreadSheet1.Workbook.Worksheets[0].Names.Add("TotalSales", "SUM(B2:B6)", null, 7, 2, null);
    spreadSheet1.Workbook.Worksheets[0].Names.Add("ExceptCharles", "=Sheet1!C8 - Sheet1!B6", null, 8, 2, null);
    spreadSheet1.Workbook.Worksheets[0].Cells["C8"].Formula = "=TotalSales";
    spreadSheet1.Workbook.Worksheets[0].Cells["C9"].Formula = "ExceptCharles";
    
    Copy Code
    ' Add custom name in the worksheet.
    spreadSheet1.Workbook.Worksheets(0).Names.Add("TotalSales", "SUM(B2:B6)", Nothing, 7, 2, Nothing)
    spreadSheet1.Workbook.Worksheets(0).Names.Add("ExceptCharles", "=Sheet1!C8 - Sheet1!B6", Nothing, 8, 2, Nothing)
    spreadSheet1.Workbook.Worksheets(0).Cells("C8").Formula = "=TotalSales"
    spreadSheet1.Workbook.Worksheets(0).Cells("C9").Formula = "ExceptCharles"