Modificare l allineamento delle celle e mantenere la formattazione esistente
Possibili Scenari di Utilizzo
A volte, si desidera cambiare l’allineamento di più celle ma anche mantenere la formattazione esistente. Aspose.Cells for JavaScript tramite C++ permette di farlo usando il metodo StyleFlag.alignments(boolean). Se lo imposterai true, avverranno le modifiche nell’allineamento, altrimenti no. Nota che, l’oggetto StyleFlag viene passato come parametro al metodo Range.applyStyle(Style, StyleFlag) che effettivamente applica la formattazione a un intervallo di celle.
Modifica dell’allineamento delle celle e mantenimento della formattazione esistente
Il seguente codice di esempio carica il file Excel di esempio, crea l’intervallo e centra l’allineamento in modo orizzontale e verticale e mantiene intatta la formattazione esistente. Lo screenshot seguente confronta il file Excel di esempio e il file Excel di output e mostra che tutta la formattazione esistente delle celle è la stessa tranne che le celle sono ora allineate al centro in modo orizzontale e verticale.

Codice di Esempio
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Change Cells Alignment and Keep Existing Formatting</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, StyleFlag, TextAlignmentType, 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 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();
// Instantiate Workbook from uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Create cells range
const range = worksheet.cells.createRange("B2:D7");
// Create style object
const style = workbook.createStyle();
// Set the horizontal and vertical alignment to center using property assignments
style.horizontalAlignment = TextAlignmentType.Center;
style.verticalAlignment = TextAlignmentType.Center;
// Create style flag object
const flag = new StyleFlag();
flag.alignments = true; // Set style flag alignments true
// Apply style to range of cells
range.applyStyle(style, flag);
// Save the workbook in XLSX format and provide 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 = 'outputChangeCellsAlignmentAndKeepExistingFormatting.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
resultDiv.innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>