Detectar celdas combinadas en una hoja con JavaScript a través de C++
Contents
[
Hide
]
Este artículo proporciona información sobre cómo obtener áreas de celdas fusionadas en una hoja de cálculo.
Aspose.Cells for JavaEl Script vía C++ te permite obtener áreas de celdas combinadas en una hoja de cálculo. También puedes descombinar (dividir) esas celdas. Este artículo muestra el código más simple usando API Aspose.Cells para realizar la tarea.
El componente proporciona el método Cells.mergedAreas que puede obtener todas las celdas combinadas. El siguiente ejemplo muestra cómo detectar celdas combinadas en una hoja de cálculo.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Unmerge Areas</title>
</head>
<body>
<h1>Unmerge Merged Areas 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 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 a new Workbook by opening the uploaded excel file
const wkBook = new Workbook(new Uint8Array(arrayBuffer));
// Get a worksheet in the workbook
const wkSheet = wkBook.worksheets.get("Sheet2");
// Clear its contents
wkSheet.cells.clear();
// Get merged areas
const areas = wkSheet.cells.mergedAreas;
// Check if areas is null or not
if (!areas || areas.length === 0) {
console.warn("No merged areas to unmerge.");
resultDiv.innerHTML = '<p style="color: orange;">No merged areas to unmerge.</p>';
return;
}
// Define some variables
let frow, fcol, erow, ecol, trows, tcols;
// Loop through the arraylist and get each cellarea
// To unmerge it
for (let i = 0; i < areas.length; i++) {
frow = areas[i].startRow;
fcol = areas[i].startColumn;
erow = areas[i].endRow;
ecol = areas[i].endColumn;
trows = erow - frow + 1;
tcols = ecol - fcol + 1;
wkSheet.cells.unMerge(frow, fcol, trows, tcols);
}
// Saving the modified Excel file and provide download link
const outputData = wkBook.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'MergeTrial.out.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
resultDiv.innerHTML = '<p style="color: green;">Merged areas unmerged successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>