刷新和计算包含计算项的数据透视表
Contents
[
Hide
]
Aspose.Cells for JavaScript通过C++现在支持刷新和计算具有计算项的数据透视表。请像往常一样使用PivotTable.refreshData和PivotTable.calculateData来执行此功能。
刷新和计算包含计算项的数据透视表
以下示例代码加载了包含三个计算项(如"add"、“div”、“div2”)的Pivot表的源Excel文件。我们首先将单元格D2的值改为20,然后使用Aspose.Cells for JavaScript通过C++ API刷新和计算Pivot表,并以PDF格式保存工作簿。结果显示在输出PDF,证明Aspose.Cells for JavaScript通过C++成功刷新了包含计算项的Pivot表。你可以手动在Excel中将D2设置为20,然后通过Alt+F5或点击刷新按钮来验证。
<!DOCTYPE html>
<html>
<head>
<title>Refresh and Calculate Pivot Table Items</title>
</head>
<body>
<h1>Refresh and Calculate Pivot Table Items</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 () => {
const fileInput = document.getElementById('fileInput');
const result = document.getElementById('result');
const downloadLink = document.getElementById('downloadLink');
if (!fileInput.files.length) {
result.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Accessing the first worksheet in the Excel file
const sheet = workbook.worksheets.get(0);
// Change the value of cell D2
const cell = sheet.cells.get("D2");
cell.value = 20;
// Refresh and calculate all the pivot tables inside this sheet
sheet.refreshPivotTables();
// Save the workbook as PDF and provide a download link
const outputData = workbook.save(SaveFormat.Pdf);
const blob = new Blob([outputData]);
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'RefreshAndCalculateItems_out.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
result.innerHTML = '<p style="color: green;">Pivot tables refreshed and calculated. Click the download link to get the PDF.</p>';
});
</script>
</html>