添加和引用命名区域
Contents
[
Hide
]
通常,列和行标签用于唯一地引用单元格。但您可以创建描述性名称来表示单元格、单元格范围、公式或常量值。单词名称可能指代一个表示单元格、单元格范围、公式或常量值的一串字符。例如,使用易于理解的名称,如“产品”,来引用难以理解的范围,如“销售!C20:C30”。标签可以用于引用同一工作表上的数据的公式;如果要表示另一个工作表上的范围,可以使用名称。命名区域是Microsoft Excel的最强大的功能之一。用户可以为一个区域分配一个名称,并在公式中使用该名称。Aspose.Cells.GridWeb支持此功能。
在公式中添加/引用命名范围
GridWeb控件提供了两个类(GridName和GridNameCollection)用于处理命名区域。以下代码片段将帮助您了解如何创建命名区域并在公式中访问它。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Inserting dummy data | |
GridWeb1.WorkSheets[0].Cells["B1"].PutValue(100); | |
GridWeb1.WorkSheets[0].Cells["B2"].PutValue(200); | |
GridWeb1.WorkSheets[0].Cells["B3"].PutValue(300); | |
GridWeb1.WorkSheets[0].Cells["B4"].PutValue(400); | |
// Add a new named range "MyRange" with based area B1:B4 | |
GridWeb1.WorkSheets.Names.Add("MyRange", "Sheet1!B1:B4"); | |
// Apply a formula to a cell that refers to a named range "MyRange" | |
GridWeb1.WorkSheets[0].Cells["A1"].Formula = "=SUM(MyRange)"; | |
// Apply a formula to A2 cell | |
GridWeb1.WorkSheets[0].Cells["A2"].Formula = "=Average(MyRange)"; | |
// Calculate the results of the formulas | |
GridWeb1.WorkSheets.CalculateFormula(); |