在使用C++的JavaScript中自动在表格或列表对象中输入新行时传播公式
Contents
[
Hide
]
可能的使用场景
有时,您希望在输入新数据时,表格或列表对象中的公式能自动传播到新行。这是微软Excel的默认行为。要在使用C++的JavaScript中实现相同的功能,请使用ListColumn.formula属性。
** 自动在新行输入时传播表格或列表对象中的公式**
以下示例代码以此方式创建了一个表格或列表对象,使得在第 B 列中的公式会在输入新数据时自动传播到新行。请查看此代码生成的 输出 Excel 文件。如果在 A3 单元格中输入任何数字,你会看到 B2 单元格中的公式会自动传播到 B3。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells ListObject Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
// Create workbook object
const book = new Workbook();
// Access first worksheet
const sheet = book.worksheets.get(0);
// Add column headings in cell A1 and B1
sheet.cells.get(0, 0).value = "Column A";
sheet.cells.get(0, 1).value = "Column B";
// Add list object, set its name and style
const listObject = sheet.listObjects.get(
sheet.listObjects.add(0, 0, 1, sheet.cells.maxColumn, true)
);
listObject.tableStyleType = AsposeCells.TableStyleType.TableStyleMedium2;
listObject.displayName = "Table";
// Set the formula of second column so that it propagates to new rows automatically while entering data
listObject.listColumns.get(1).formula = "=[Column A] + 1";
// Save the workbook in xlsx format
const outputData = book.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output_out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook created successfully! Click the download link to get the file.</p>';
});
</script>
</html>