添加 Cell 公式
Contents
[
Hide
]
Aspose.Cells.GridWeb 提供的最有价值的功能是支持公式或函数。 Aspose.Cells.GridWeb 有自己的公式引擎,可以计算工作表中的公式。 Aspose.Cells.GridWeb 支持内置和用户定义的函数或公式。本主题详细讨论使用 Aspose.Cells.GridWeb API 向单元格添加公式。
将公式添加到 Cells
如何添加和计算公式?
可以使用单元格的公式属性在单元格中添加、访问和修改公式。 Aspose.Cells.GridWeb支持从简单到复杂的用户自定义公式。但是,Aspose.Cells.GridWeb也提供了大量的内置函数或公式(类似于Microsoft Excel)。要查看内置函数的完整列表,请参阅此支持的功能列表。
公式语法应与 Microsoft Excel 语法兼容。例如,所有公式都必须以等号 (=) 开头。
要动态添加公式,Aspose.Cells.GridWeb 即使您不使用 = 符号也会将其识别为公式,但如果最终用户在 GUI 中工作,他必须使用“=”符号。
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 | |
// Accessing the worksheet of the Grid that is currently active | |
GridWorksheet sheet = GridWeb1.WorkSheets[GridWeb1.ActiveSheetIndex]; | |
// Putting some values to cells | |
sheet.Cells["A1"].PutValue("1st Value"); | |
sheet.Cells["A2"].PutValue("2nd Value"); | |
sheet.Cells["A3"].PutValue("Sum"); | |
sheet.Cells["B1"].PutValue(125.56); | |
sheet.Cells["B2"].PutValue(23.93); | |
// Adding a simple formula to "B3" cell | |
sheet.Cells["B3"].Formula = "=SUM(B1:B2)"; |
公式添加到 B3 单元格但未由 GridWeb 计算
在上面的截图中,可以看到B3中已经添加了一个公式,但是还没有进行计算。要计算所有公式,请在将公式添加到工作表后调用 GridWeb 控件的 GridWorksheetCollection 的 CalculateFormula 方法,如下所示。
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 | |
// Calculating all formulas added in worksheets | |
GridWeb1.WorkSheets.CalculateFormula(); |
用户还可以通过单击计算公式提交.
单击 GridWeb 的提交按钮
重要的:如果用户点击救球要么撤消按钮或工作表选项卡,所有公式均由 GridWeb 自动计算。
计算后的公式结果
从其他工作表中引用 Cells
使用 Aspose.Cells.GridWeb,可以在其公式中引用存储在不同工作表中的值,从而创建复杂的公式。
从不同工作表引用单元格值的语法是 SheetName!CellName。
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 | |
// Adding a bit complex formula to "A1" cell | |
sheet1.Cells["B6"].Formula = "=(SUM(A1:A5)/AVERAGE(B1:B5))-Sheet2!B1"; |