Novis\Spreadsheet\Sheet::setPrintOptions
$sheet->setPrintOptions(PrintOptions $options): Sheet$sheet->setPrintOptions( PrintOptions $options, ): Sheet
Sets how the sheet is printed, and returns the sheet.
orientation is "portrait" or "landscape". fitToPage sets how many pages wide (width) and tall (height) the sheet is printed on, and 0 means as many as needed. margins are in inches. header and footer are the text at the top and the bottom of each page. repeatRows, such as "1:1", prints those rows at the top of every page. area is the range to print.
Good to know: set fitToPage before the first row of the sheet. After that, it throws a LogicError. Give all the options in one call. A second setPrintOptions call on the same sheet throws a LogicError. Any other orientation throws a LogicError. A CSV file has no page setup, so setPrintOptions does nothing there.
The examples below print a wide report on one page, and repeat the header row on every page. The last one shows the error for a fitToPage set after the first row.
Parameters
$optionsPrintOptions
Return value
Returns Sheet.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each Novis\Spreadsheet\Sheet and Workbook method that adds to the file says what it adds, for example addRow(), addChart() and addSheet(). setPrintOptions() replaces print(). Chart->withSeries() and combinedWith() return a new chart. |
Examples
A wide report on one page
<?nvs// Shows how to print a wide report across one page, turned sideways.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Sales');
// `fitToPage` is set before the first row. `height: 0` means as many pages tall as needed.$sheet->setPrintOptions({ orientation: 'landscape', fitToPage: { width: 1, height: 0 }, footer: 'Shop sales report' });$sheet->addRow(['shop' => 'North', 'january' => 120, 'february' => 140, 'march' => 135]);$sheet->addRow(['shop' => 'South', 'january' => 80, 'february' => 95, 'march' => 90]);bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the sheet. This line checks the page setup.var $xml = Core\Zip::read($file, 'xl/worksheets/sheet1.xml') as string;echo Core\Str::contains($xml, '<pageSetup orientation="landscape" fitToWidth="1" fitToHeight="0"/>') ? 'landscape, one page wide' : 'other setup', "\n";landscape, one page wideThe header row on every page
<?nvs// Shows how to print the header row at the top of every page of a long list.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Orders');int $order = 1000;while ($order < 1200) { $sheet->addRow(['order' => $order, 'total' => 9.5]); $order = $order + 1;}
// Row 1 is printed on every page. Only the cells A1 to B201 are printed.$sheet->setPrintOptions({ repeatRows: '1:1', area: 'A1:B201', header: 'Orders' });bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the workbook. These lines check the two settings.var $xml = Core\Zip::read($file, 'xl/workbook.xml') as string;echo Core\Str::contains($xml, "'Orders'!\$1:\$1") ? 'row 1 repeats' : 'no row repeats', "\n";echo Core\Str::contains($xml, "'Orders'!\$A\$1:\$B\$201") ? 'A1:B201 is printed' : 'the whole sheet is printed', "\n";row 1 repeatsA1:B201 is printedA fit set after the first row
<?nvs// Shows the error for `fitToPage` after the first row, and for an unknown orientation.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Sales');$sheet->addRow(['shop' => 'North', 'sales' => 340]);
// The first row is already written, so `fitToPage` throws a `LogicError`.try { $sheet->setPrintOptions({ fitToPage: { width: 1 } });} catch (LogicError $e) { echo $e->message, "\n";}
try { $sheet->setPrintOptions({ orientation: 'sideways' });} catch (LogicError $e) { echo $e->message, "\n";}The sheet `Sales` cannot be fitted to pages after its first row. Set the fit before you add rows.The print option `orientation` is `'portrait'` or `'landscape'`. It has `'sideways'`.