إعداد الصيغة لنطاق مسمى
Contents
[
Hide
]
إعداد الصيغة لنطاق مسمى
مثل تطبيق Excel ، توفر واجهات برمجة تطبيقات Aspose.Cells القدرة على تحديد صيغة لنطاق مسمى أثناء استخداميعود الىخاصية. يمكن أن يكون هناك العديد من سيناريوهات الاستخدام لهذه الميزة ، بعضها مفصل على النحو التالي.
تعيين صيغة بسيطة لنطاق مسمى
يمكن أن تكون الصيغة البسيطة مرجعاً لخلية أخرى في نفس ورقة العمل (أو ورقة مختلفة). يقوم المثال التالي بإنشاء نطاق مسمى في جدول بيانات جديد وتعيين مرجعه إلى خلية أخرى.
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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create an instance of Workbook | |
Workbook book = new Workbook(); | |
// Get the WorksheetCollection | |
WorksheetCollection worksheets = book.Worksheets; | |
// Add a new Named Range with name "NewNamedRange" | |
int index = worksheets.Names.Add("NewNamedRange"); | |
// Access the newly created Named Range | |
Name name = worksheets.Names[index]; | |
// Set RefersTo property of the Named Range to a formula. Formula references another cell in the same worksheet | |
name.RefersTo = "=Sheet1!$A$3"; | |
// Set the formula in the cell A1 to the newly created Named Range | |
worksheets[0].Cells["A1"].Formula = "NewNamedRange"; | |
// Insert the value in cell A3 which is being referenced in the Named Range | |
worksheets[0].Cells["A3"].PutValue("This is the value of A3"); | |
// Calculate formulas | |
book.CalculateFormula(); | |
// Save the result in XLSX format | |
book.Save(dataDir + "output_out.xlsx"); |
تعيين صيغة معقدة لنطاق مسمى
يمكن أن تكون الصيغة المعقدة أي شيء مثل نطاق ديناميكي أو صيغة تمتد عبر خلايا متعددة في أوراق عمل مختلفة. ينشئ المثال التالي نطاقًا ديناميكيًا باستخدام الدالة INDEX للحصول على القيمة من قائمة بناءً على موقعها.
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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create an instance of Workbook | |
Workbook book = new Workbook(); | |
// Get the WorksheetCollection | |
WorksheetCollection worksheets = book.Worksheets; | |
// Add a new Named Range with name "data" | |
int index = worksheets.Names.Add("data"); | |
// Access the newly created Named Range from the collection | |
Name data = worksheets.Names[index]; | |
// Set RefersTo property of the Named Range to a cell range in same worksheet | |
data.RefersTo = "=Sheet1!$A$1:$A$10"; | |
// Add another Named Range with name "range" | |
index = worksheets.Names.Add("range"); | |
// Access the newly created Named Range from the collection | |
Name range = worksheets.Names[index]; | |
// Set RefersTo property to a formula using the Named Range data | |
range.RefersTo = "=INDEX(data,Sheet1!$A$1,1):INDEX(data,Sheet1!$A$1,9)"; | |
// Save the workbook | |
book.Save(dataDir + "output_out.xlsx"); |
فيما يلي مثال آخر يستخدم نطاقًا مسمىًا لجمع القيم من خليتين في أوراق عمل مختلفة.
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 | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create an instance of Workbook | |
Workbook book = new Workbook(); | |
// Get the WorksheetCollection | |
WorksheetCollection worksheets = book.Worksheets; | |
// Insert some data in cell A1 of Sheet1 | |
worksheets["Sheet1"].Cells["A1"].PutValue(10); | |
// Add a new Worksheet and insert a value to cell A1 | |
worksheets[worksheets.Add()].Cells["A1"].PutValue(10); | |
// Add a new Named Range with name "range" | |
int index = worksheets.Names.Add("range"); | |
// Access the newly created Named Range from the collection | |
Name range = worksheets.Names[index]; | |
// Set RefersTo property of the Named Range to a SUM function | |
range.RefersTo = "=SUM(Sheet1!$A$1,Sheet2!$A$1)"; | |
// Insert the Named Range as formula to 3rd worksheet | |
worksheets[worksheets.Add()].Cells["A1"].Formula = "range"; | |
// Calculate formulas | |
book.CalculateFormula(); | |
// Save the result in XLSX format | |
book.Save(dataDir + "output_out.xlsx"); |