在输入新行的同时自动传播表或列表对象中的公式
Contents
[
Hide
]
可能的使用场景
有时,您希望表或列表对象中的公式在输入新数据时自动传播到新行。这是Microsoft Excel的默认行为。为了使用Aspose.Cells实现相同的功能,请使用ListColumn.Formula属性。
在输入新数据时自动传播表或列表对象中的公式
以下示例代码以一种使列B中的公式在输入新数据时自动传播到新行的方式创建了一个Table或List对象。请检查使用此代码生成的输出Excel文件。如果在A3单元格中输入任何数字,您会看到,B2单元格中的公式会自动传播到B3单元格。
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 workbook object | |
Workbook book = new Workbook(); | |
// Access first worksheet | |
Worksheet sheet = book.Worksheets[0]; | |
// Add column headings in cell A1 and B1 | |
sheet.Cells[0, 0].PutValue("Column A"); | |
sheet.Cells[0, 1].PutValue("Column B"); | |
// Add list object, set its name and style | |
ListObject listObject = sheet.ListObjects[sheet.ListObjects.Add(0, 0, 1, sheet.Cells.MaxColumn, true)]; | |
listObject.TableStyleType = TableStyleType.TableStyleMedium2; | |
listObject.DisplayName = "Table"; | |
// Set the formula of second column so that it propagates to new rows automatically while entering data | |
listObject.ListColumns[1].Formula = "=[Column A] + 1"; | |
// Save the workbook in xlsx format | |
book.Save(dataDir + "output_out.xlsx"); |