Ajustar automáticamente filas y columnas con JavaScript a través de C++
Ajuste automático
Aspose.Cells proporciona una clase Workbook que representa un archivo de Microsoft Excel. La clase Workbook contiene una colección Workbook.worksheets que permite acceder a cada hoja en un archivo Excel. Una hoja de cálculo está representada por la clase Worksheet. La clase Worksheet ofrece una amplia gama de propiedades y métodos para gestionar una hoja de cálculo. Este artículo analiza cómo usar la clase Worksheet para ajustar filas o columnas automáticamente.
Ajuste automático de fila - Simple
El enfoque más sencillo para ajustar automáticamente el ancho y la altura de una fila es llamar al método autoFitRow de la clase Worksheet. El método autoFitRow toma un índice de fila (de la fila a redimensionar) como parámetro.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - AutoFit Row</title>
</head>
<body>
<h1>AutoFit Row 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');
if (!fileInput.files.length) {
document.getElementById('result').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 by opening the Excel file through the buffer
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.worksheets.get(0);
// Auto-fitting the 3rd row of the worksheet (row index 1 is the 2nd row; original code used 1)
worksheet.autoFitRow(1);
// 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.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Row auto-fitted successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Cómo AutoAjustar una Fila en un Rango de Celdas
Una fila está compuesta por muchas columnas. Aspose.Cells permite a los desarrolladores ajustar automáticamente una fila basada en el contenido en un rango de celdas dentro de la fila llamando a una versión sobrecargada del método autoFitRow. Toma los siguientes parámetros:
- Índice de la fila, el índice de la fila a ajustar automáticamente.
- Índice de la primera columna, el índice de la primera columna de la fila.
- Índice de la última columna, el índice de la última columna de la fila.
El método autoFitRow verifica el contenido de todas las columnas en la fila y luego ajusta automáticamente la fila.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - AutoFit Row</title>
</head>
<body>
<h1>Auto-Fit Row 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 } = 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');
if (!fileInput.files.length) {
document.getElementById('result').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 by opening the Excel file through the file buffer
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.worksheets.get(0);
// Auto-fitting the 3rd row of the worksheet (row index 1, startColumn 0, endColumn 5)
worksheet.autoFitRow(1, 0, 5);
// 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.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Row auto-fitted successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Cómo AutoAjustar una Columna en un Rango de Celdas
Una columna está compuesta por muchas filas. Es posible ajustar automáticamente una columna basada en el contenido en un rango de celdas en la columna llamando a una versión sobrecargada del método autoFitColumn que toma los siguientes parámetros:
- Índice de columna, el índice de la columna que se va a ajustar automáticamente.
- Índice de la primera fila, el índice de la primera fila de la columna.
- Índice de la última fila, el índice de la última fila de la columna.
El método autoFitColumn verifica el contenido de todas las filas en la columna y luego ajusta automáticamente la columna.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Aspose.Cells AutoFit Column 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, Worksheet, Cell, 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');
if (!fileInput.files.length) {
document.getElementById('result').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));
// Accessing the first worksheet in the Excel file
const worksheet = workbook.worksheets.get(0);
// Auto-fitting the Column of the worksheet (column index 4)
worksheet.autoFitColumn(4);
// 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.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Column auto-fitted successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Cómo AutoAjustar Filas para Celdas Fusionadas
Con Aspose.Cells, es posible ajustar automáticamente filas incluso para celdas que han sido combinadas usando la API AutoFitterOptions. La clase AutoFitterOptions proporciona una propiedad AutoFitterOptions.autoFitMergedCellsType() que puede ser utilizada para ajustar automáticamente filas para celdas combinadas. AutoFitterOptions.autoFitMergedCellsType() acepta un enumerable AutoFitMergedCellsType que tiene los siguientes miembros.
- Ninguno: Ignora las celdas combinadas.
- PrimeraLinea: Solo expande la altura de la primera fila.
- ÚltimaLinea: Solo expande la altura de la última fila.
- CadaLinea: Solo expande la altura de cada fila.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Autofit Rows for Merged Cells 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, AutoFitterOptions, AutoFitMergedCellsType } = 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');
// Create or load workbook
let wb;
if (fileInput.files && fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
wb = new Workbook(new Uint8Array(arrayBuffer));
} else {
wb = new Workbook();
}
// Get the first (default) worksheet
const worksheet = wb.worksheets.get(0);
// Create a range A1:B1
const range = worksheet.cells.createRange(0, 0, 1, 2);
// Merge the cells
range.merge();
// Insert value to the merged cell A1
const cell = worksheet.cells.get(0, 0);
cell.value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end";
// Create a style object
const style = cell.style;
// Set wrapping text on
style.isTextWrapped = true;
// Apply the style to the cell
cell.style = style;
// Create an object for AutoFitterOptions
const options = new AutoFitterOptions();
// Set auto-fit for merged cells
options.autoFitMergedCellsType = AutoFitMergedCellsType.EachLine;
// Autofit rows in the sheet (including the merged cells)
worksheet.autoFitRows(options);
// Save the Excel file
const outputData = wb.save(SaveFormat.Xlsx);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'AutofitRowsforMergedCells.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Operation completed successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
También puedes intentar usar las versiones sobrecargadas de los métodos autoFitRows y autoFitColumns que aceptan un rango de filas/columnas y una instancia de AutoFitterOptions para ajustar automáticamente las filas/columnas seleccionadas con tu AutoFitterOptions deseado, respectivamente.
Las firmas de los métodos mencionados anteriormente son las siguientes:
- autoFitRows(int startRow, int endRow, AutoFitterOptions options)
- autoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)