指定数据透视项的绝对位置
Contents
[
Hide
]
有时用户需要指定Pivot项的绝对位置,Aspose.Cells for JavaScript通过C++ API提供了一些新属性和一个方法以满足此需求。
- 添加了PivotItem.position属性,可用于指定所有数据透视项的位置索引,而不论父节点如何。添加了PivotItem.positionInSameParentNode属性,可用于指定在同一父节点下的数据透视项的位置索引。
- 添加了PivotItem.move方法,以根据计数值将项上移或下移,其中计数是要将数据透视项上移或下移的位置数。如果计数值小于零,则数据透视项将上移,如果计数值大于零,则数据透视项将下移,Boolean类型的isSameParent参数指定移动操作是否必须在同一父节点中执行。
- 已废弃*PivotItem.move(int count)*方法,建议使用新增的方法 PivotItem.move(number, boolean)。
以下示例代码创建了一个数据透视表,然后指定了数据透视项在同一父节点中的位置。您可以下载源Excel和输出Excel文件进行参考。如果打开输出Excel文件,您将看到数据透视项“4H12”位于父节点“K11”中的第0个位置,而“DIF400”位于第3个位置。同样,CA32位于第1个位置,AAA3位于第2个位置。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells PivotTable Example</title>
</head>
<body>
<h1>PivotTable 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, PivotFieldType, PivotFieldSubtotalType } = 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 resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.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 uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Add pivot worksheet and get data worksheet
const wsPivot = workbook.worksheets.add("pvtNew Hardware");
const wsData = workbook.worksheets.get("New Hardware - Yearly");
// Get the pivottables collection for the pivot sheet
const pivotTables = wsPivot.pivotTables;
// Add PivotTable to the worksheet
const index = pivotTables.add("='New Hardware - Yearly'!A1:D621", "A3", "HWCounts_PivotTable");
// Get the PivotTable object
const pvtTable = pivotTables.get(index);
// Add vendor row field
pvtTable.addFieldToArea(PivotFieldType.Row, "Vendor");
// Add item row field
pvtTable.addFieldToArea(PivotFieldType.Row, "Item");
// Add data field
pvtTable.addFieldToArea(PivotFieldType.Data, "2014");
// Turn off the subtotals for the vendor row field
const pivotField = pvtTable.rowFields.get("Vendor");
pivotField.subtotals = PivotFieldSubtotalType.None;
// Turn off grand total
pvtTable.columnGrand = false;
/*
* Please call the PivotTable.refreshData() and PivotTable.calculateData()
* before using PivotItem.setPosition,
* PivotItem.setPositionInSameParentNode and PivotItem.move methods.
*/
pvtTable.refreshData();
pvtTable.calculateData();
pvtTable.rowFields.get("Item").pivotItems.get("4H12").positionInSameParentNode = 0;
pvtTable.rowFields.get("Item").pivotItems.get("DIF400").positionInSameParentNode = 3;
/*
* As a result of using PivotItem.setPositionInSameParentNode,
* it will change the original sort sequence.
* So when you use PivotItem.setPositionInSameParentNode in another parent node.
* You need call the method named "calculateData" again.
*/
pvtTable.calculateData();
pvtTable.rowFields.get("Item").pivotItems.get("CA32").positionInSameParentNode = 1;
pvtTable.rowFields.get("Item").pivotItems.get("AAA3").positionInSameParentNode = 2;
// Saving the modified Excel file and providing a download link
const outputData = workbook.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 Modified Excel File';
resultDiv.innerHTML = '<p style="color: green;">PivotTable created successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
请注意,在使用PivotItem.position、PivotItem.positionInSameParentNode属性和PivotItem.move方法之前,必须先调用PivotTable.RefreshData和PivotTable.CalculateData方法。