Ottieni l oggetto Cell tramite DisplayName di PivotField di PivotTable
Contents
[
Hide
]
Aspose.Cells for JavaScript via C++ fornisce il metodo PivotTable.cellByDisplayName(string) che puoi usare per accedere all’oggetto cella tramite il nome visualizzato del campo pivot. Questo metodo è utile quando si desidera evidenziare o formattare l’intestazione del campo pivot. Questo articolo spiega come recuperare l’oggetto cella tramite il nome visualizzato del campo dati e applicare quindi una formattazione.
Come ottenere l’oggetto Cell tramite DisplayName di PivotField di PivotTable
Il codice seguente accede alla prima tabella pivot del foglio di lavoro e ottiene la cella per nome visualizzato del secondo campo dati della tabella pivot. Cambia quindi il colore di riempimento e il colore del carattere della cella rispettivamente in azzurro chiaro e nero. Di seguito sono mostrati gli screenshot di come appare la tabella pivot prima e dopo l’esecuzione del codice.
| Tabella Pivot - Prima |
|---|
![]() |
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells Pivot Table Cell Styling 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, Utils } = 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 resultDiv = document.getElementById('result');
const fileInput = document.getElementById('fileInput');
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 the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Access first pivot table inside the worksheet
const pivotTable = worksheet.pivotTables.get(0);
// Access cell by display name of 2nd data field of the pivot table
const dataFieldDisplayName = pivotTable.dataFields.get(1).displayName;
const cell = pivotTable.cellByDisplayName(dataFieldDisplayName);
// Access cell style and set its fill color and font color
const style = cell.style;
style.foregroundColor = AsposeCells.Color.LightBlue;
style.font.color = AsposeCells.Color.Black;
// Set the style of the cell
pivotTable.format(cell.row, cell.column, style);
// Saving the modified Excel file
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;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
| Tabella Pivot - Dopo |
|---|
![]() |

