在输入新行的同时自动传播表或列表对象中的公式
Contents
[
Hide
]
可能的使用场景
有时,您希望在输入新数据时,表格或列表对象中的公式会自动传播到新行。 这是Microsoft Excel的默认行为。 为了实现与Aspose.Cells相同的功能,请使用ListColumn.Formula属性。
在输入新数据时自动传播表或列表对象中的公式
以下示例代码以这样的方式创建一个表格或列表对象,以便在输入新数据时列B中的公式会自动传播到新行。 请检查使用此代码生成的输出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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(PropagateFormulaInTableorListObject.class) + "articles/"; | |
// Create workbook object | |
Workbook book = new Workbook(); | |
// Access first worksheet | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Add column headings in cell A1 and B1 | |
sheet.getCells().get(0, 0).putValue("Column A"); | |
sheet.getCells().get(0, 1).putValue("Column B"); | |
// Add list object, set its name and style | |
int idx = sheet.getListObjects().add(0, 0, 1, sheet.getCells().getMaxColumn(), true); | |
ListObject listObject = sheet.getListObjects().get(idx); | |
listObject.setTableStyleType(TableStyleType.TABLE_STYLE_MEDIUM_2); | |
listObject.setDisplayName("Table"); | |
// Set the formula of second column so that it propagates to new rows | |
// automatically while entering data | |
listObject.getListColumns().get(1).setFormula("=[Column A] + 1"); | |
// Save the workbook in xlsx format | |
book.save(dataDir + "PropagateFormulaInTable_out.xlsx"); |