Impostare le Opzioni di Stampa con JavaScript via C++
Opzioni di stampa
Queste opzioni di stampa consentono agli utenti di:
- Selezionare un’area di stampa specifica su un foglio di lavoro.
- Stampare i titoli.
- Stampare le linee di griglia.
- Stampare gli intitoli di riga/colonna.
- Ottenere una qualità di bozza.
- Stampare commenti.
- Stampare errori di cella. Definire l’ordinamento delle pagine.
Aspose.Cells for JavaScript tramite C++ supporta tutte le opzioni di stampa offerte da Microsoft Excel e gli sviluppatori possono facilmente configurare queste opzioni per i fogli di lavoro utilizzando le proprietà offerte dalla classe PageSetup. Come vengono utilizzate queste proprietà è discusso più dettagliatamente di seguito.
Impostare l’area di stampa
Per impostazione predefinita, l’area di stampa incorpora tutte le aree del foglio di lavoro che contengono dati. Gli sviluppatori possono stabilire un’area di stampa specifica del foglio di lavoro.
Per selezionare una specifica area di stampa, utilizzare la proprietà PageSetup.printArea della classe PageSetup. Assegnare a questa proprietà un intervallo di celle che definisce l’area di stampa.
<!DOCTYPE html>
<html>
<head>
<title>Set Print Area Example</title>
</head>
<body>
<h1>Set Print Area 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 () => {
// Instantiate a new Workbook object
const workbook = new Workbook();
// Obtaining the reference of the PageSetup of the first worksheet
const pageSetup = workbook.worksheets.get(0).pageSetup;
// Specifying the cells range (from A1 cell to T35 cell) of the print area
pageSetup.printArea = "A1:T35";
// Save the workbook and prepare download link
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'SetPrintArea_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Print area set successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>
Impostare i titoli di stampa
Aspose.Cells consente di designare l’intestazione delle righe e delle colonne da ripetere su tutte le pagine di un foglio di lavoro stampato. Per farlo, utilizzare le proprietà PageSetup.printTitleColumns e PageSetup.printTitleRows della classe PageSetup.
Le righe o le colonne che verranno ripetute sono definite passando il loro numero di riga o colonna. Ad esempio, le righe sono definite come $1:$2 e le colonne sono definite come $A:$B.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Set Print Title</title>
</head>
<body>
<h1>Set Print Title Columns and Rows Example</h1>
<p>You may optionally select an existing Excel file to modify. If no file is selected, a new workbook will be created.</p>
<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');
let workbook;
if (fileInput.files.length) {
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
workbook = new Workbook(new Uint8Array(arrayBuffer));
} else {
workbook = new Workbook();
}
// Obtaining the reference of the PageSetup of the first worksheet
const pageSetup = workbook.worksheets.get(0).pageSetup;
// Defining column numbers A & B as title columns
pageSetup.printTitleColumns = "$A:$B";
// Defining row numbers 1 & 2 as title rows
pageSetup.printTitleRows = "$1:$2";
// Save the workbook and provide download link
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'SetPrintTitle_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Print title columns and rows set successfully. Click the download link to save the file.</p>';
});
</script>
</html>
Impostare altre opzioni di stampa
La classe PageSetup fornisce anche diverse altre proprietà per impostare le opzioni di stampa generali come segue:
- PageSetup.printGridlines: una proprietà booleana che definisce se stampare o meno le linee della griglia.
- PageSetup.printHeadings: una proprietà booleana che definisce se stampare o meno le intestazioni di riga e colonna.
- PageSetup.blackAndWhite: una proprietà booleana che definisce se stampare o meno il foglio in modalità bianco e nero.
- PageSetup.printComments: determina se visualizzare i commenti di stampa sul foglio o alla fine del foglio.
- PageSetup.printDraft: una proprietà booleana che definisce se stampare il foglio senza grafica.
- PageSetup.printErrors: definisce se stampare gli errori delle celle come visualizzati, vuoto, trattino o N/D.
Per impostare le proprietà PageSetup.printComments e PageSetup.printErrors, Aspose.Cells for JavaScript via C++ fornisce anche due enumerazioni, PrintCommentsType e PrintErrorsType che contengono valori predefiniti da assegnare rispettivamente alle proprietà PageSetup.printComments e PageSetup.printErrors.
I valori predefiniti dell’enumerazione PrintCommentsType sono elencati di seguito con le loro descrizioni.
| Tipi di Commenti di Stampa | Descrizione |
|---|---|
| PrintInPlace | Specifica di stampare i commenti come visualizzati sul foglio di lavoro. |
| PrintNoComments | Specifica di non stampare i commenti. |
| PrintSheetEnd | Specifica di stampare i commenti alla fine del foglio di lavoro. |
I valori predefiniti dell’enumerazione PrintErrorsType sono elencati di seguito con le loro descrizioni.
| Tipi di Errori di Stampa | Descrizione |
|---|---|
| PrintErrorsBlank | Specifica di non stampare gli errori. |
| PrintErrorsDash | Specifica di stampare gli errori come “–”. |
| PrintErrorsDisplayed | Specifica di stampare gli errori come visualizzato. |
| PrintErrorsNA | Specifica di stampare gli errori come “#N/A”. |
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Other Print Options</title>
</head>
<body>
<h1>Other Print Options 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, PrintCommentsType, PrintErrorsType } = 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 provided
workbook = new Workbook();
}
// Obtaining the reference of the PageSetup of the first worksheet
const pageSetup = workbook.worksheets.get(0).pageSetup;
// Allowing to print gridlines
pageSetup.printGridlines = true;
// Allowing to print row/column headings
pageSetup.printHeadings = true;
// Allowing to print worksheet in black & white mode
pageSetup.blackAndWhite = true;
// Allowing to print comments as displayed on worksheet
pageSetup.printComments = PrintCommentsType.PrintInPlace;
// Allowing to print worksheet with draft quality
pageSetup.printDraft = true;
// Allowing to print cell errors as N/A
pageSetup.printErrors = PrintErrorsType.PrintErrorsNA;
// Saving the modified workbook to 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 = 'OtherPrintOptions_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Modified 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>
Imposta l’Ordine delle Pagine
La classe PageSetup fornisce la proprietà PageSetup.order che viene utilizzata per ordinare le pagine multiple del tuo foglio di lavoro da stampare. Ci sono due possibilità per ordinare le pagine come segue.
- stampa tutte le pagine verso il basso prima di stampare qualsiasi pagina a destra.
- stampa le pagine da sinistra a destra prima di stampare le pagine sotto.
Aspose.Cells fornisce un’enumerazione, PrintOrderType che contiene tutti i tipi di ordinamento predefiniti.
I valori predefiniti dell’enumerazione PrintOrderType sono elencati di seguito.
| Tipi di Ordine di Stampa | Descrizione |
|---|---|
| DownThenOver | Rappresenta l’ordine di stampa come in basso e poi sopra. |
| OverThenDown | Rappresenta l’ordine di stampa come sopra e poi in basso. |
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Set Page Order Example</title>
</head>
<body>
<h1>Set Page Order 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, PrintOrderType } = 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 {
workbook = new Workbook();
}
const worksheet = workbook.worksheets.get(0);
const pageSetup = worksheet.pageSetup;
pageSetup.order = PrintOrderType.OverThenDown;
const outputData = workbook.save(SaveFormat.Excel97To2003);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'SetPageOrder_out.xls';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel File';
document.getElementById('result').innerHTML = '<p style="color: green;">Page order set successfully! Click the download link to get the modified file.</p>';
});
</script>
</html>