Configurar márgenes con JavaScript vía C++
Configurando Márgenes
Aspose.Cells for JavaScript vía C++ proporciona una clase, Workbook, que representa un archivo de Excel. La clase Workbook contiene la colección Workbook.worksheets que permite acceder a cada hoja de cálculo en el archivo de Excel. Una hoja de cálculo está representada por la clase Worksheet.
La propiedad Worksheet.pageSetup se usa para configurar las opciones de configuración de página para una hoja de cálculo. El atributo Worksheet.pageSetup es un objeto de la clase Worksheet.pageSetup que permite a los desarrolladores establecer diferentes opciones de diseño de página para una hoja impresa. La clase Worksheet.pageSetup proporciona varias propiedades y métodos utilizados para ajustar la configuración de pagina.
Márgenes de Página
Establecer márgenes de página (izquierda, derecha, superior, inferior) usando miembros de clase Worksheet.pageSetup. Algunos de los miembros listados a continuación se usan para especificar los márgenes de página:
<!DOCTYPE html>
<html>
<head>
<title>Set Page Margins Example</title>
</head>
<body>
<h1>Set Page Margins 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) {
// Proceed with a new empty workbook if no file selected
}
const file = fileInput.files.length ? fileInput.files[0] : null;
let workbook;
if (file) {
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
const worksheets = workbook.worksheets;
const worksheet = worksheets.get(0);
const pageSetup = worksheet.pageSetup;
pageSetup.bottomMargin = 2;
pageSetup.leftMargin = 1;
pageSetup.rightMargin = 1;
pageSetup.topMargin = 3;
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'SetMargins_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Page margins set successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Centrar en la Página
Es posible centrar algo en una página horizontal y verticalmente. Para esto, hay algunos miembros útiles de la clase Worksheet.pageSetup, PageSetup.centerHorizontally y PageSetup.centerVertically.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Center On Page</title>
</head>
<body>
<h1>Center On Page 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 () => {
// Create a workbook object (blank workbook)
const workbook = new Workbook();
// Get the worksheets in the workbook
const worksheets = workbook.worksheets;
// Get the first (default) worksheet
const worksheet = worksheets.get(0);
// Get the pagesetup object
const pageSetup = worksheet.pageSetup;
// Specify Center on page Horizontally and Vertically
pageSetup.centerHorizontally = true;
pageSetup.centerVertically = true;
// Save the Workbook in Excel 97-2003 format (.xls)
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'CenterOnPage_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook processed successfully! Click the download link to get the file.</p>';
});
</script>
</html>
Márgenes de Encabezado y Pie de Página
Establece márgenes de encabezado y pie de página con los miembros de la clase Worksheet.pageSetup como PageSetup.headerMargin y PageSetup.footerMargin.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Set Header/Footer Margins</title>
</head>
<body>
<h1>Set Header/Footer Margins 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 () => {
// Create a new workbook (equivalent to new AsposeCells.Workbook() in JavaScript)
const workbook = new Workbook();
// Get the worksheets collection
const worksheets = workbook.worksheets;
// Get the first (default) worksheet
const worksheet = worksheets.get(0);
// Get the pageSetup object
const pageSetup = worksheet.pageSetup;
// Specify Header / Footer margins (converted from setHeaderMargin/setFooterMargin)
pageSetup.headerMargin = 2;
pageSetup.footerMargin = 2;
// Save the Workbook as Excel 97-2003 format
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'CenterOnPage_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Workbook processed successfully. Click the download link to get the file.</p>';
});
</script>
</html>