Printing
The worksheet renders only the cells in the viewport, so the page itself cannot be printed as a whole. getPrintable creates a printable version of the worksheet: plain tables with every row, ready to be appended to the page and printed by the browser.
What's new with Version 13
getPrintable: a printable version of the worksheet with every row, built detached so appending it costs one layout and one paint- Large worksheets: the rows go in blocks the browser only lays out and paints when they are on screen or printed, so 100,000 rows are ready in a few seconds
- Faithful: formatted values, merges, cell styles, column renderers and the row group outline are kept
Documentation
Methods
| Method | Description |
|---|---|
getPrintable |
Create a printable version of the worksheet. Returns a detached element: append it where it should be shown or printed.getPrintable(options?: { rows?: number[], columns?: number[], render?: function }) => HTMLElement |
Options
| Option | Description |
|---|---|
rows?: number[] |
The rows to print, in this order. Default: the rows shown, so filters, hidden rows and closed row groups apply |
columns?: number[] |
The columns to print. Default: the visible columns |
render?: function |
Called for each printed cell after its content is set, to customize it.render(td: HTMLTableCellElement, x: number, y: number, value: any) => void |
What is printed
- Values: the formatted value of each cell, as the worksheet shows it, masks included. Checkbox columns print a checked or unchecked box
- Column renderers: a column with a
renderfunction owns the presentation of its cells, exactly as in the worksheet - Merges: the anchor spans its columns and rows, the covered cells are not printed
- Styles: the style of each cell and the alignment of its column
- Row groups: the index column is indented by the outline level, and the rows that own a group show its state,
-open and+closed. The rows inside a closed group are not printed unless given inrows - Headers: the column titles, in a table of their own
Performance
The printable version has no virtualization, editors or cell records: it is plain HTML built outside the document, so appending it costs a single layout and paint. A single table with hundreds of thousands of cells is expensive for any browser to lay out, so the rows go in blocks of about two hundred rows, each block a table with the same column widths. A block never ends inside a rowspan. The blocks use content-visibility: auto: the browser only lays out and paints the blocks on screen, while every row is in the document and all of them are rendered when the page is printed.
Styling
The printable version carries the class jss_printable, each block jss_printable_block, and the rows of a group the same classes as in the worksheet: jss_group_header for the rows that own a group and jss_group_level_1 to jss_group_level_8 for the outline level. The default style is a minimal grid that can be overridden with CSS.
Examples
Print the worksheet
Create the printable version, print it, and remove it afterwards.
<html>
<script src="https://jspreadsheet.com/v13/jspreadsheet.js"></script>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jspreadsheet.com/v13/jspreadsheet.css" type="text/css" />
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />
<style>
#printable { display: none; }
@media print {
body > *:not(#printable) { display: none; }
#printable { display: block; }
}
</style>
<div id="spreadsheet"></div>
<div id="printable"></div>
<p><input type="button" value="Print" id="print" /></p>
<script>
// You can use the following license for quick testing on localhost, StackBlitz, or CodeSandbox.
// The license is valid for one day, after which the spreadsheet will become read-only.
// For a longer trial period, you can create a free account and generate a demo license with an extended expiration date.
jspreadsheet.setLicense('OWJmMzVjMTM5ODZlNDQ2NjM1MmQ0YjA3ZmZkYTg2MmExZWU2OWQxYTY4Yzc2OGMyZTM2MTZmYTgwMzU3YTU2MjdiZmE5YzY2NDk1MWYzY2FkZGY4MGQ4MDNkY2M1ZmU4MmIxZTFlNjY3ZGVjYjlkZDA1NDllYzdjYjhhOWI0YzIsZXlKamJHbGxiblJKWkNJNklpSXNJbTVoYldVaU9pSktjM0J5WldGa2MyaGxaWFFpTENKa1lYUmxJam94TnprME9UQTFNelk1TENKa2IyMWhhVzRpT2xzaWFuTndjbVZoWkhOb1pXVjBMbU52YlNJc0ltTnZaR1Z6WVc1a1ltOTRMbWx2SWl3aWFuTm9aV3hzTG01bGRDSXNJbU56WWk1aGNIQWlMQ0p6ZEdGamEySnNhWFI2TG1sdklpd2lkMlZpWTI5dWRHRnBibVZ5TG1sdklpd2liRzlqWVd4b2IzTjBJbDBzSW5Cc1lXNGlPaUl6TkNJc0luTmpiM0JsSWpwYkluWTNJaXdpZGpnaUxDSjJPU0lzSW5ZeE1DSXNJbll4TVNJc0luWXhNaUlzSW5ZeE15SXNJbU5vWVhKMGN5SXNJbVp2Y20xeklpd2labTl5YlhWc1lTSXNJbkJoY25ObGNpSXNJbkpsYm1SbGNpSXNJbU52YlcxbGJuUnpJaXdpYVcxd2IzSjBaWElpTENKaVlYSWlMQ0oyWVd4cFpHRjBhVzl1Y3lJc0luTmxZWEpqYUNJc0luQnlhVzUwSWl3aWMyaGxaWFJ6SWl3aVkyeHBaVzUwSWl3aWMyVnlkbVZ5SWl3aWMyaGhjR1Z6SWl3aVptOXliV0YwSWl3aWNHbDJiM1FpWFN3aVpHVnRieUk2ZEhKMVpYMD0=');
// Some data
const data = [];
for (let i = 0; i < 1000; i++) {
data.push(['Product ' + i, Math.round(Math.random() * 1000), i % 3 === 0]);
}
// Create the spreadsheet
const worksheets = jspreadsheet(document.getElementById('spreadsheet'), {
worksheets: [{
data: data,
tableOverflow: true,
tableHeight: '300px',
columns: [
{ type: 'text', title: 'Product', width: 200 },
{ type: 'number', title: 'Price', width: 100, mask: '$ #,##0.00' },
{ type: 'checkbox', title: 'Available', width: 100 },
],
mergeCells: { A1: [2, 1] },
}],
});
document.getElementById('print').onclick = function() {
const target = document.getElementById('printable');
// Every row, appended once
target.appendChild(worksheets[0].getPrintable());
window.print();
target.replaceChildren();
}
</script>
</html>
Choose the rows and customize the cells
Print the selected rows only, with negative prices in red.
const printable = worksheet.getPrintable({
rows: worksheet.getSelectedRows(true),
render: function(td, x, y, value) {
if (x === 1 && value < 0) {
td.style.color = 'red';
}
},
});
document.getElementById('printable').appendChild(printable);
See Also
- Merged Cells - Merges in the printable version
- Group Rows - The row group outline
- Format - Masks of the printed values