Configurar encabezados y pies de página con JavaScript vía C++
Configuración de encabezados y pies de página
Aspose.Cells for JavaScript vía C++ te permite añadir encabezados y pies de página a las hojas de cálculo en tiempo de ejecución, pero recomendamos establecer encabezados y pies de página manualmente en un archivo pre-diseñado para impresión. Puedes usar Microsoft Excel como herramienta GUI para configurar encabezados y pies de página, ahorrando esfuerzo y tiempo de desarrollo. Aspose.Cells puede importar el archivo y guardar las configuraciones.
Para agregar encabezados y pies de página en tiempo de ejecución, Aspose.Cells proporciona llamadas de API especiales y comandos de script para formatear encabezados y pies de página.
Comandos de Script
Los comandos de script son comandos especiales que le permiten configurar el formato de los encabezados y pies de página.
| Comandos de Script | Descripción |
|---|---|
| &P | Número de página actual |
| &G | Una imagen |
| &N | El número total de páginas |
| &D | La fecha actual |
| &T | La hora actual |
| &A | Nombre de la hoja de cálculo |
| &F | El nombre del archivo sin su ruta |
| &&Texto | Muestra &Texto. Por ejemplo: &&WO será mostrado como &WO |
| &"<FontName>" | Representa un nombre de fuente. Por ejemplo: &“Arial” |
| &"<FontName>, <FontStyle>" | Representa el nombre de la fuente con estilo. Por ejemplo: &“Arial,Negrita” |
| &<FontSize> | Representa el tamaño de la fuente. Por ejemplo: “&14abc”. Sin embargo, si este comando va seguido de un número normal a imprimir en el encabezado, esto debe separarse con un carácter de espacio del tamaño de la fuente. Por ejemplo: “&14 123”. |
Establecer Encabezados y Pies de Página
La clase PageSetup proporciona dos métodos, header(number, string) y footer(number, string), utilizados para agregar un encabezado y pie de página a una hoja de cálculo. Estos métodos solo toman dos parámetros:
- Sección – la sección donde se debe colocar el encabezado o pie de página. Hay tres secciones: izquierda, centro y derecha, representadas respectivamente por 0, 1 y 2.
- Script – el script a utilizar para el encabezado o pie de página. Este script contiene comandos de script para formatear encabezados o pies de página.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
<meta charset="utf-8" />
</head>
<body>
<h1>Set Headers and Footers 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');
let workbook;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
// Create a new workbook if no file is provided
workbook = new Workbook();
}
// Obtaining the reference of the PageSetup of the first worksheet
const worksheet = workbook.worksheets.get(0);
const pageSetup = worksheet.pageSetup;
// Setting worksheet name at the left section of the header
pageSetup.header = pageSetup.header || [];
pageSetup.header[0] = "&A";
// Setting current date and current time at the central section of the header
// and changing the font of the header
pageSetup.header[1] = "&\"Times New Roman,Bold\"&D-&T";
// Setting current file name at the right section of the header and changing the
// font of the header
pageSetup.header[2] = "&\"Times New Roman,Bold\"&12&F";
// Setting a string at the left section of the footer and changing the font
// of a part of this string ("123")
pageSetup.footer = pageSetup.footer || [];
pageSetup.footer[0] = "Hello World! &\"Courier New\"&14 123";
// Setting the current page number at the central section of the footer
pageSetup.footer[1] = "&P";
// Setting page count at the right section of footer
pageSetup.footer[2] = "&N";
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'SetHeadersAndFooters_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Headers and footers set successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Insertar una Imagen en un Encabezado o Pie de Página
La clase PageSetup tiene dos métodos adicionales, **headerPicture(number, number[])** y **footerPicture(number, number[])**, utilizados para agregar imágenes en el encabezado y pie de página. Estos métodos toman los siguientes parámetros:
- Sección – la sección de encabezado o pie de página donde se colocará la imagen. Hay tres secciones, izquierda, centro y derecha, representadas por los valores 0, 1 y 2 respectivamente.
- Arreglo de bytes – los datos gráficos (los datos binarios deben escribirse en el búfer de un arreglo de bytes).
Después de ejecutar el código a continuación y abrir el archivo, verificar el encabezado de la hoja de trabajo mediante:
- En el menú Archivo, seleccionar Configurar Página. Se mostrará un cuadro de diálogo.
- Seleccionar la pestaña Encabezado/Pie de página.
<!DOCTYPE html>
<html>
<head>
<title>Insert Image in Header/Footer Example</title>
</head>
<body>
<h1>Insert Image in Header/Footer Example</h1>
<p>Select an existing Excel file to modify (optional). If none is selected, a new workbook will be used.</p>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<p>Select an image to insert into the header:</p>
<input type="file" id="imageInput" accept="image/*" />
<br/><br/>
<button id="runExample">Insert Image into Header</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 imageInput = document.getElementById('imageInput');
const resultDiv = document.getElementById('result');
if (!imageInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an image file to insert into the header.</p>';
return;
}
// Read image bytes
const imageFile = imageInput.files[0];
const imageBuffer = await imageFile.arrayBuffer();
const binaryData = new Uint8Array(imageBuffer);
// Create or load workbook
let workbook;
if (fileInput.files.length) {
const excelFile = fileInput.files[0];
const excelBuffer = await excelFile.arrayBuffer();
workbook = new Workbook(new Uint8Array(excelBuffer));
} else {
workbook = new Workbook();
}
// Access the first worksheet's page setup
const pageSetup = workbook.worksheets.get(0).pageSetup;
// Set the header picture and header scripts (converted from setters to properties)
pageSetup.headerPicture = binaryData;
pageSetup.header = "&G";
pageSetup.header = "&A";
// Save the workbook as Excel97-2003 (.xls)
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'InsertImageInHeaderFooter_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
resultDiv.innerHTML = '<p style="color: green;">Image inserted into header successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>