Specificare la posizione assoluta dell elemento pivot
Contents
[
Hide
]
A volte, l’utente ha bisogno di specificare la posizione assoluta degli elementi pivot, l’API di Aspose.Cells for JavaScript tramite C++ ha esposto alcune nuove proprietà e un metodo per soddisfare questa esigenza.
- Aggiunta la proprietà PivotItem.position che può essere utilizzata per specificare l’indice di posizione in tutti i PivotItems indipendentemente dal nodo genitore. Aggiunta la proprietà PivotItem.positionInSameParentNode che può essere utilizzata per specificare l’indice di posizione nei PivotItems sotto lo stesso nodo genitore.
- Aggiunta del metodo PivotItem.move per spostare l’elemento su o giù in base al valore del conteggio, dove il conteggio è il numero di posizioni per spostare l’elemento pivot su o giù. Se il valore del conteggio è inferiore a zero, l’elemento verrà spostato verso l’alto mentre se il valore del conteggio è maggiore di zero, l’elemento pivot si sposterà verso il basso, il parametro di tipo Boolean isSameParent specifica se l’operazione di spostamento deve essere eseguita nello stesso nodo padre o meno.
- Il metodo PivotItem.move(int count) è stato desueto, pertanto si consiglia di usare invece il nuovo metodo PivotItem.move(number, boolean).
Il seguente codice di esempio crea una tabella pivot e specifica quindi le posizioni degli elementi pivot nello stesso nodo padre. Puoi scaricare i file Excel di origine e Excel di output per il tuo riferimento. Se apri il file Excel di output, vedrai che l’elemento pivot “4H12” si trova nella posizione 0 nel padre “K11” e “DIF400” si trova nella terza posizione. Allo stesso modo, CA32 si trova nella posizione 1 e AAA3 è nella posizione 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>
Si prega di notare che è necessario chiamare i metodi PivotTable.RefreshData e PivotTable.CalculateData prima di utilizzare le proprietà PivotItem.position, PivotItem.positionInSameParentNode e il metodo PivotItem.move.