Calculate Page Setup Scaling Factor with JavaScript via C++
Contents
[
Hide
]
When you set Page Setup Scaling using Fit to n page(s) wide by m tall option, Microsoft Excel calculates the Page Setup Scaling Factor. You can calculate the same thing using SheetRender.pageScale property. This property returns a double value that can be converted to percentage value. For example, if it returns 0.5 then it means scaling factor is 50%.
The following sample code illustrates how to calculate page setup scaling factor using SheetRender.pageScale property.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Page Scale</title>
</head>
<body>
<h1>Page Scale 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, SheetRender, ImageOrPrintOptions, PaperSizeType, 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();
// Opening the Excel file through the file stream
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Access first worksheet
const worksheet = workbook.worksheets.get(0);
// Put some data in these cells
worksheet.cells.get("A4").putValue("Test");
worksheet.cells.get("S4").putValue("Test");
// Set paper size
worksheet.pageSetup.paperSize = PaperSizeType.PaperA4;
// Set fit to pages wide as 1
worksheet.pageSetup.fitToPagesWide = 1;
// Calculate page scale via sheet render
const sr = new SheetRender(worksheet, new ImageOrPrintOptions());
// Convert page scale double value to percentage
const strPageScale = (sr.pageScale * 100).toFixed(0) + "%";
// Write the page scale value
document.getElementById('result').innerHTML = `<p>Page Scale: ${strPageScale}</p>`;
console.log(strPageScale);
});
</script>
</html>