1<?php
2
3namespace PhpOffice\PhpSpreadsheet\Writer\Xls;
4
5use GdImage;
6use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
7use PhpOffice\PhpSpreadsheet\Cell\DataType;
8use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
9use PhpOffice\PhpSpreadsheet\RichText\RichText;
10use PhpOffice\PhpSpreadsheet\RichText\Run;
11use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
12use PhpOffice\PhpSpreadsheet\Shared\Xls;
13use PhpOffice\PhpSpreadsheet\Style\Border;
14use PhpOffice\PhpSpreadsheet\Style\Color;
15use PhpOffice\PhpSpreadsheet\Style\Conditional;
16use PhpOffice\PhpSpreadsheet\Style\Protection;
17use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
18use PhpOffice\PhpSpreadsheet\Worksheet\SheetView;
19use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
20
21// Original file header of PEAR::Spreadsheet_Excel_Writer_Worksheet (used as the base for this class):
22// -----------------------------------------------------------------------------------------
23// /*
24// *  Module written/ported by Xavier Noguer <xnoguer@rezebra.com>
25// *
26// *  The majority of this is _NOT_ my code.  I simply ported it from the
27// *  PERL Spreadsheet::WriteExcel module.
28// *
29// *  The author of the Spreadsheet::WriteExcel module is John McNamara
30// *  <jmcnamara@cpan.org>
31// *
32// *  I _DO_ maintain this code, and John McNamara has nothing to do with the
33// *  porting of this code to PHP.  Any questions directly related to this
34// *  class library should be directed to me.
35// *
36// *  License Information:
37// *
38// *    Spreadsheet_Excel_Writer:  A library for generating Excel Spreadsheets
39// *    Copyright (c) 2002-2003 Xavier Noguer xnoguer@rezebra.com
40// *
41// *    This library is free software; you can redistribute it and/or
42// *    modify it under the terms of the GNU Lesser General Public
43// *    License as published by the Free Software Foundation; either
44// *    version 2.1 of the License, or (at your option) any later version.
45// *
46// *    This library is distributed in the hope that it will be useful,
47// *    but WITHOUT ANY WARRANTY; without even the implied warranty of
48// *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
49// *    Lesser General Public License for more details.
50// *
51// *    You should have received a copy of the GNU Lesser General Public
52// *    License along with this library; if not, write to the Free Software
53// *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
54// */
55class Worksheet extends BIFFwriter
56{
57    /**
58     * Formula parser.
59     *
60     * @var \PhpOffice\PhpSpreadsheet\Writer\Xls\Parser
61     */
62    private $parser;
63
64    /**
65     * Maximum number of characters for a string (LABEL record in BIFF5).
66     *
67     * @var int
68     */
69    private $xlsStringMaxLength;
70
71    /**
72     * Array containing format information for columns.
73     *
74     * @var array
75     */
76    private $columnInfo;
77
78    /**
79     * Array containing the selected area for the worksheet.
80     *
81     * @var array
82     */
83    private $selection;
84
85    /**
86     * The active pane for the worksheet.
87     *
88     * @var int
89     */
90    private $activePane;
91
92    /**
93     * Whether to use outline.
94     *
95     * @var bool
96     */
97    private $outlineOn;
98
99    /**
100     * Auto outline styles.
101     *
102     * @var bool
103     */
104    private $outlineStyle;
105
106    /**
107     * Whether to have outline summary below.
108     *
109     * @var bool
110     */
111    private $outlineBelow;
112
113    /**
114     * Whether to have outline summary at the right.
115     *
116     * @var bool
117     */
118    private $outlineRight;
119
120    /**
121     * Reference to the total number of strings in the workbook.
122     *
123     * @var int
124     */
125    private $stringTotal;
126
127    /**
128     * Reference to the number of unique strings in the workbook.
129     *
130     * @var int
131     */
132    private $stringUnique;
133
134    /**
135     * Reference to the array containing all the unique strings in the workbook.
136     *
137     * @var array
138     */
139    private $stringTable;
140
141    /**
142     * Color cache.
143     */
144    private $colors;
145
146    /**
147     * Index of first used row (at least 0).
148     *
149     * @var int
150     */
151    private $firstRowIndex;
152
153    /**
154     * Index of last used row. (no used rows means -1).
155     *
156     * @var int
157     */
158    private $lastRowIndex;
159
160    /**
161     * Index of first used column (at least 0).
162     *
163     * @var int
164     */
165    private $firstColumnIndex;
166
167    /**
168     * Index of last used column (no used columns means -1).
169     *
170     * @var int
171     */
172    private $lastColumnIndex;
173
174    /**
175     * Sheet object.
176     *
177     * @var \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet
178     */
179    public $phpSheet;
180
181    /**
182     * Count cell style Xfs.
183     *
184     * @var int
185     */
186    private $countCellStyleXfs;
187
188    /**
189     * Escher object corresponding to MSODRAWING.
190     *
191     * @var \PhpOffice\PhpSpreadsheet\Shared\Escher
192     */
193    private $escher;
194
195    /**
196     * Array of font hashes associated to FONT records index.
197     *
198     * @var array
199     */
200    public $fontHashIndex;
201
202    /**
203     * @var bool
204     */
205    private $preCalculateFormulas;
206
207    /**
208     * @var int
209     */
210    private $printHeaders;
211
212    /**
213     * Constructor.
214     *
215     * @param int $str_total Total number of strings
216     * @param int $str_unique Total number of unique strings
217     * @param array $str_table String Table
218     * @param array $colors Colour Table
219     * @param Parser $parser The formula parser created for the Workbook
220     * @param bool $preCalculateFormulas Flag indicating whether formulas should be calculated or just written
221     * @param \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $phpSheet The worksheet to write
222     */
223    public function __construct(&$str_total, &$str_unique, &$str_table, &$colors, Parser $parser, $preCalculateFormulas, \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet $phpSheet)
224    {
225        // It needs to call its parent's constructor explicitly
226        parent::__construct();
227
228        $this->preCalculateFormulas = $preCalculateFormulas;
229        $this->stringTotal = &$str_total;
230        $this->stringUnique = &$str_unique;
231        $this->stringTable = &$str_table;
232        $this->colors = &$colors;
233        $this->parser = $parser;
234
235        $this->phpSheet = $phpSheet;
236
237        $this->xlsStringMaxLength = 255;
238        $this->columnInfo = [];
239        $this->selection = [0, 0, 0, 0];
240        $this->activePane = 3;
241
242        $this->printHeaders = 0;
243
244        $this->outlineStyle = false;
245        $this->outlineBelow = true;
246        $this->outlineRight = true;
247        $this->outlineOn = true;
248
249        $this->fontHashIndex = [];
250
251        // calculate values for DIMENSIONS record
252        $minR = 1;
253        $minC = 'A';
254
255        $maxR = $this->phpSheet->getHighestRow();
256        $maxC = $this->phpSheet->getHighestColumn();
257
258        // Determine lowest and highest column and row
259        $this->firstRowIndex = $minR;
260        $this->lastRowIndex = ($maxR > 65535) ? 65535 : $maxR;
261
262        $this->firstColumnIndex = Coordinate::columnIndexFromString($minC);
263        $this->lastColumnIndex = Coordinate::columnIndexFromString($maxC);
264
265        if ($this->lastColumnIndex > 255) {
266            $this->lastColumnIndex = 255;
267        }
268
269        $this->countCellStyleXfs = count($phpSheet->getParent()->getCellStyleXfCollection());
270    }
271
272    /**
273     * Add data to the beginning of the workbook (note the reverse order)
274     * and to the end of the workbook.
275     *
276     * @see \PhpOffice\PhpSpreadsheet\Writer\Xls\Workbook::storeWorkbook()
277     */
278    public function close(): void
279    {
280        $phpSheet = $this->phpSheet;
281
282        // Storing selected cells and active sheet because it changes while parsing cells with formulas.
283        $selectedCells = $this->phpSheet->getSelectedCells();
284        $activeSheetIndex = $this->phpSheet->getParent()->getActiveSheetIndex();
285
286        // Write BOF record
287        $this->storeBof(0x0010);
288
289        // Write PRINTHEADERS
290        $this->writePrintHeaders();
291
292        // Write PRINTGRIDLINES
293        $this->writePrintGridlines();
294
295        // Write GRIDSET
296        $this->writeGridset();
297
298        // Calculate column widths
299        $phpSheet->calculateColumnWidths();
300
301        // Column dimensions
302        if (($defaultWidth = $phpSheet->getDefaultColumnDimension()->getWidth()) < 0) {
303            $defaultWidth = \PhpOffice\PhpSpreadsheet\Shared\Font::getDefaultColumnWidthByFont($phpSheet->getParent()->getDefaultStyle()->getFont());
304        }
305
306        $columnDimensions = $phpSheet->getColumnDimensions();
307        $maxCol = $this->lastColumnIndex - 1;
308        for ($i = 0; $i <= $maxCol; ++$i) {
309            $hidden = 0;
310            $level = 0;
311            $xfIndex = 15; // there are 15 cell style Xfs
312
313            $width = $defaultWidth;
314
315            $columnLetter = Coordinate::stringFromColumnIndex($i + 1);
316            if (isset($columnDimensions[$columnLetter])) {
317                $columnDimension = $columnDimensions[$columnLetter];
318                if ($columnDimension->getWidth() >= 0) {
319                    $width = $columnDimension->getWidth();
320                }
321                $hidden = $columnDimension->getVisible() ? 0 : 1;
322                $level = $columnDimension->getOutlineLevel();
323                $xfIndex = $columnDimension->getXfIndex() + 15; // there are 15 cell style Xfs
324            }
325
326            // Components of columnInfo:
327            // $firstcol first column on the range
328            // $lastcol  last column on the range
329            // $width    width to set
330            // $xfIndex  The optional cell style Xf index to apply to the columns
331            // $hidden   The optional hidden atribute
332            // $level    The optional outline level
333            $this->columnInfo[] = [$i, $i, $width, $xfIndex, $hidden, $level];
334        }
335
336        // Write GUTS
337        $this->writeGuts();
338
339        // Write DEFAULTROWHEIGHT
340        $this->writeDefaultRowHeight();
341        // Write WSBOOL
342        $this->writeWsbool();
343        // Write horizontal and vertical page breaks
344        $this->writeBreaks();
345        // Write page header
346        $this->writeHeader();
347        // Write page footer
348        $this->writeFooter();
349        // Write page horizontal centering
350        $this->writeHcenter();
351        // Write page vertical centering
352        $this->writeVcenter();
353        // Write left margin
354        $this->writeMarginLeft();
355        // Write right margin
356        $this->writeMarginRight();
357        // Write top margin
358        $this->writeMarginTop();
359        // Write bottom margin
360        $this->writeMarginBottom();
361        // Write page setup
362        $this->writeSetup();
363        // Write sheet protection
364        $this->writeProtect();
365        // Write SCENPROTECT
366        $this->writeScenProtect();
367        // Write OBJECTPROTECT
368        $this->writeObjectProtect();
369        // Write sheet password
370        $this->writePassword();
371        // Write DEFCOLWIDTH record
372        $this->writeDefcol();
373
374        // Write the COLINFO records if they exist
375        if (!empty($this->columnInfo)) {
376            $colcount = count($this->columnInfo);
377            for ($i = 0; $i < $colcount; ++$i) {
378                $this->writeColinfo($this->columnInfo[$i]);
379            }
380        }
381        $autoFilterRange = $phpSheet->getAutoFilter()->getRange();
382        if (!empty($autoFilterRange)) {
383            // Write AUTOFILTERINFO
384            $this->writeAutoFilterInfo();
385        }
386
387        // Write sheet dimensions
388        $this->writeDimensions();
389
390        // Row dimensions
391        foreach ($phpSheet->getRowDimensions() as $rowDimension) {
392            $xfIndex = $rowDimension->getXfIndex() + 15; // there are 15 cellXfs
393            $this->writeRow(
394                $rowDimension->getRowIndex() - 1,
395                (int) $rowDimension->getRowHeight(),
396                $xfIndex,
397                !$rowDimension->getVisible(),
398                $rowDimension->getOutlineLevel()
399            );
400        }
401
402        // Write Cells
403        foreach ($phpSheet->getCoordinates() as $coordinate) {
404            $cell = $phpSheet->getCell($coordinate);
405            $row = $cell->getRow() - 1;
406            $column = Coordinate::columnIndexFromString($cell->getColumn()) - 1;
407
408            // Don't break Excel break the code!
409            if ($row > 65535 || $column > 255) {
410                throw new WriterException('Rows or columns overflow! Excel5 has limit to 65535 rows and 255 columns. Use XLSX instead.');
411            }
412
413            // Write cell value
414            $xfIndex = $cell->getXfIndex() + 15; // there are 15 cell style Xfs
415
416            $cVal = $cell->getValue();
417            if ($cVal instanceof RichText) {
418                $arrcRun = [];
419                $str_pos = 0;
420                $elements = $cVal->getRichTextElements();
421                foreach ($elements as $element) {
422                    // FONT Index
423                    if ($element instanceof Run) {
424                        $str_fontidx = $this->fontHashIndex[$element->getFont()->getHashCode()];
425                    } else {
426                        $str_fontidx = 0;
427                    }
428                    $arrcRun[] = ['strlen' => $str_pos, 'fontidx' => $str_fontidx];
429                    // Position FROM
430                    $str_pos += StringHelper::countCharacters($element->getText(), 'UTF-8');
431                }
432                $this->writeRichTextString($row, $column, $cVal->getPlainText(), $xfIndex, $arrcRun);
433            } else {
434                switch ($cell->getDatatype()) {
435                    case DataType::TYPE_STRING:
436                    case DataType::TYPE_NULL:
437                        if ($cVal === '' || $cVal === null) {
438                            $this->writeBlank($row, $column, $xfIndex);
439                        } else {
440                            $this->writeString($row, $column, $cVal, $xfIndex);
441                        }
442
443                        break;
444                    case DataType::TYPE_NUMERIC:
445                        $this->writeNumber($row, $column, $cVal, $xfIndex);
446
447                        break;
448                    case DataType::TYPE_FORMULA:
449                        $calculatedValue = $this->preCalculateFormulas ?
450                            $cell->getCalculatedValue() : null;
451                        if (self::WRITE_FORMULA_EXCEPTION == $this->writeFormula($row, $column, $cVal, $xfIndex, $calculatedValue)) {
452                            if ($calculatedValue === null) {
453                                $calculatedValue = $cell->getCalculatedValue();
454                            }
455                            $calctype = gettype($calculatedValue);
456                            switch ($calctype) {
457                                case 'integer':
458                                case 'double':
459                                    $this->writeNumber($row, $column, $calculatedValue, $xfIndex);
460
461                                    break;
462                                case 'string':
463                                    $this->writeString($row, $column, $calculatedValue, $xfIndex);
464
465                                    break;
466                                case 'boolean':
467                                    $this->writeBoolErr($row, $column, $calculatedValue, 0, $xfIndex);
468
469                                    break;
470                                default:
471                                    $this->writeString($row, $column, $cVal, $xfIndex);
472                            }
473                        }
474
475                        break;
476                    case DataType::TYPE_BOOL:
477                        $this->writeBoolErr($row, $column, $cVal, 0, $xfIndex);
478
479                        break;
480                    case DataType::TYPE_ERROR:
481                        $this->writeBoolErr($row, $column, ErrorCode::error($cVal), 1, $xfIndex);
482
483                        break;
484                }
485            }
486        }
487
488        // Append
489        $this->writeMsoDrawing();
490
491        // Restoring active sheet.
492        $this->phpSheet->getParent()->setActiveSheetIndex($activeSheetIndex);
493
494        // Write WINDOW2 record
495        $this->writeWindow2();
496
497        // Write PLV record
498        $this->writePageLayoutView();
499
500        // Write ZOOM record
501        $this->writeZoom();
502        if ($phpSheet->getFreezePane()) {
503            $this->writePanes();
504        }
505
506        // Restoring selected cells.
507        $this->phpSheet->setSelectedCells($selectedCells);
508
509        // Write SELECTION record
510        $this->writeSelection();
511
512        // Write MergedCellsTable Record
513        $this->writeMergedCells();
514
515        // Hyperlinks
516        foreach ($phpSheet->getHyperLinkCollection() as $coordinate => $hyperlink) {
517            [$column, $row] = Coordinate::indexesFromString($coordinate);
518
519            $url = $hyperlink->getUrl();
520
521            if (strpos($url, 'sheet://') !== false) {
522                // internal to current workbook
523                $url = str_replace('sheet://', 'internal:', $url);
524            } elseif (preg_match('/^(http:|https:|ftp:|mailto:)/', $url)) {
525                // URL
526            } else {
527                // external (local file)
528                $url = 'external:' . $url;
529            }
530
531            $this->writeUrl($row - 1, $column - 1, $url);
532        }
533
534        $this->writeDataValidity();
535        $this->writeSheetLayout();
536
537        // Write SHEETPROTECTION record
538        $this->writeSheetProtection();
539        $this->writeRangeProtection();
540
541        $arrConditionalStyles = $phpSheet->getConditionalStylesCollection();
542        if (!empty($arrConditionalStyles)) {
543            $arrConditional = [];
544
545            $cfHeaderWritten = false;
546            // Write ConditionalFormattingTable records
547            foreach ($arrConditionalStyles as $cellCoordinate => $conditionalStyles) {
548                foreach ($conditionalStyles as $conditional) {
549                    /** @var Conditional $conditional */
550                    if (
551                        $conditional->getConditionType() == Conditional::CONDITION_EXPRESSION ||
552                        $conditional->getConditionType() == Conditional::CONDITION_CELLIS
553                    ) {
554                        // Write CFHEADER record (only if there are Conditional Styles that we are able to write)
555                        if ($cfHeaderWritten === false) {
556                            $this->writeCFHeader();
557                            $cfHeaderWritten = true;
558                        }
559                        if (!isset($arrConditional[$conditional->getHashCode()])) {
560                            // This hash code has been handled
561                            $arrConditional[$conditional->getHashCode()] = true;
562
563                            // Write CFRULE record
564                            $this->writeCFRule($conditional);
565                        }
566                    }
567                }
568            }
569        }
570
571        $this->storeEof();
572    }
573
574    /**
575     * Write a cell range address in BIFF8
576     * always fixed range
577     * See section 2.5.14 in OpenOffice.org's Documentation of the Microsoft Excel File Format.
578     *
579     * @param string $range E.g. 'A1' or 'A1:B6'
580     *
581     * @return string Binary data
582     */
583    private function writeBIFF8CellRangeAddressFixed($range)
584    {
585        $explodes = explode(':', $range);
586
587        // extract first cell, e.g. 'A1'
588        $firstCell = $explodes[0];
589
590        // extract last cell, e.g. 'B6'
591        if (count($explodes) == 1) {
592            $lastCell = $firstCell;
593        } else {
594            $lastCell = $explodes[1];
595        }
596
597        $firstCellCoordinates = Coordinate::indexesFromString($firstCell); // e.g. [0, 1]
598        $lastCellCoordinates = Coordinate::indexesFromString($lastCell); // e.g. [1, 6]
599
600        return pack('vvvv', $firstCellCoordinates[1] - 1, $lastCellCoordinates[1] - 1, $firstCellCoordinates[0] - 1, $lastCellCoordinates[0] - 1);
601    }
602
603    /**
604     * Retrieves data from memory in one chunk, or from disk
605     * sized chunks.
606     *
607     * @return string The data
608     */
609    public function getData()
610    {
611        // Return data stored in memory
612        if (isset($this->_data)) {
613            $tmp = $this->_data;
614            $this->_data = null;
615
616            return $tmp;
617        }
618
619        // No data to return
620        return '';
621    }
622
623    /**
624     * Set the option to print the row and column headers on the printed page.
625     *
626     * @param int $print Whether to print the headers or not. Defaults to 1 (print).
627     */
628    public function printRowColHeaders($print = 1): void
629    {
630        $this->printHeaders = $print;
631    }
632
633    /**
634     * This method sets the properties for outlining and grouping. The defaults
635     * correspond to Excel's defaults.
636     *
637     * @param bool $visible
638     * @param bool $symbols_below
639     * @param bool $symbols_right
640     * @param bool $auto_style
641     */
642    public function setOutline($visible = true, $symbols_below = true, $symbols_right = true, $auto_style = false): void
643    {
644        $this->outlineOn = $visible;
645        $this->outlineBelow = $symbols_below;
646        $this->outlineRight = $symbols_right;
647        $this->outlineStyle = $auto_style;
648    }
649
650    /**
651     * Write a double to the specified row and column (zero indexed).
652     * An integer can be written as a double. Excel will display an
653     * integer. $format is optional.
654     *
655     * Returns  0 : normal termination
656     *         -2 : row or column out of range
657     *
658     * @param int $row Zero indexed row
659     * @param int $col Zero indexed column
660     * @param float $num The number to write
661     * @param mixed $xfIndex The optional XF format
662     *
663     * @return int
664     */
665    private function writeNumber($row, $col, $num, $xfIndex)
666    {
667        $record = 0x0203; // Record identifier
668        $length = 0x000E; // Number of bytes to follow
669
670        $header = pack('vv', $record, $length);
671        $data = pack('vvv', $row, $col, $xfIndex);
672        $xl_double = pack('d', $num);
673        if (self::getByteOrder()) { // if it's Big Endian
674            $xl_double = strrev($xl_double);
675        }
676
677        $this->append($header . $data . $xl_double);
678
679        return 0;
680    }
681
682    /**
683     * Write a LABELSST record or a LABEL record. Which one depends on BIFF version.
684     *
685     * @param int $row Row index (0-based)
686     * @param int $col Column index (0-based)
687     * @param string $str The string
688     * @param int $xfIndex Index to XF record
689     */
690    private function writeString($row, $col, $str, $xfIndex): void
691    {
692        $this->writeLabelSst($row, $col, $str, $xfIndex);
693    }
694
695    /**
696     * Write a LABELSST record or a LABEL record. Which one depends on BIFF version
697     * It differs from writeString by the writing of rich text strings.
698     *
699     * @param int $row Row index (0-based)
700     * @param int $col Column index (0-based)
701     * @param string $str The string
702     * @param int $xfIndex The XF format index for the cell
703     * @param array $arrcRun Index to Font record and characters beginning
704     */
705    private function writeRichTextString($row, $col, $str, $xfIndex, $arrcRun): void
706    {
707        $record = 0x00FD; // Record identifier
708        $length = 0x000A; // Bytes to follow
709        $str = StringHelper::UTF8toBIFF8UnicodeShort($str, $arrcRun);
710
711        // check if string is already present
712        if (!isset($this->stringTable[$str])) {
713            $this->stringTable[$str] = $this->stringUnique++;
714        }
715        ++$this->stringTotal;
716
717        $header = pack('vv', $record, $length);
718        $data = pack('vvvV', $row, $col, $xfIndex, $this->stringTable[$str]);
719        $this->append($header . $data);
720    }
721
722    /**
723     * Write a string to the specified row and column (zero indexed).
724     * This is the BIFF8 version (no 255 chars limit).
725     * $format is optional.
726     *
727     * @param int $row Zero indexed row
728     * @param int $col Zero indexed column
729     * @param string $str The string to write
730     * @param mixed $xfIndex The XF format index for the cell
731     */
732    private function writeLabelSst($row, $col, $str, $xfIndex): void
733    {
734        $record = 0x00FD; // Record identifier
735        $length = 0x000A; // Bytes to follow
736
737        $str = StringHelper::UTF8toBIFF8UnicodeLong($str);
738
739        // check if string is already present
740        if (!isset($this->stringTable[$str])) {
741            $this->stringTable[$str] = $this->stringUnique++;
742        }
743        ++$this->stringTotal;
744
745        $header = pack('vv', $record, $length);
746        $data = pack('vvvV', $row, $col, $xfIndex, $this->stringTable[$str]);
747        $this->append($header . $data);
748    }
749
750    /**
751     * Write a blank cell to the specified row and column (zero indexed).
752     * A blank cell is used to specify formatting without adding a string
753     * or a number.
754     *
755     * A blank cell without a format serves no purpose. Therefore, we don't write
756     * a BLANK record unless a format is specified.
757     *
758     * Returns  0 : normal termination (including no format)
759     *         -1 : insufficient number of arguments
760     *         -2 : row or column out of range
761     *
762     * @param int $row Zero indexed row
763     * @param int $col Zero indexed column
764     * @param mixed $xfIndex The XF format index
765     *
766     * @return int
767     */
768    public function writeBlank($row, $col, $xfIndex)
769    {
770        $record = 0x0201; // Record identifier
771        $length = 0x0006; // Number of bytes to follow
772
773        $header = pack('vv', $record, $length);
774        $data = pack('vvv', $row, $col, $xfIndex);
775        $this->append($header . $data);
776
777        return 0;
778    }
779
780    /**
781     * Write a boolean or an error type to the specified row and column (zero indexed).
782     *
783     * @param int $row Row index (0-based)
784     * @param int $col Column index (0-based)
785     * @param int $value
786     * @param bool $isError Error or Boolean?
787     * @param int $xfIndex
788     *
789     * @return int
790     */
791    private function writeBoolErr($row, $col, $value, $isError, $xfIndex)
792    {
793        $record = 0x0205;
794        $length = 8;
795
796        $header = pack('vv', $record, $length);
797        $data = pack('vvvCC', $row, $col, $xfIndex, $value, $isError);
798        $this->append($header . $data);
799
800        return 0;
801    }
802
803    const WRITE_FORMULA_NORMAL = 0;
804    const WRITE_FORMULA_ERRORS = -1;
805    const WRITE_FORMULA_RANGE = -2;
806    const WRITE_FORMULA_EXCEPTION = -3;
807
808    /**
809     * Write a formula to the specified row and column (zero indexed).
810     * The textual representation of the formula is passed to the parser in
811     * Parser.php which returns a packed binary string.
812     *
813     * Returns  0 : WRITE_FORMULA_NORMAL  normal termination
814     *         -1 : WRITE_FORMULA_ERRORS formula errors (bad formula)
815     *         -2 : WRITE_FORMULA_RANGE  row or column out of range
816     *         -3 : WRITE_FORMULA_EXCEPTION parse raised exception, probably due to definedname
817     *
818     * @param int $row Zero indexed row
819     * @param int $col Zero indexed column
820     * @param string $formula The formula text string
821     * @param mixed $xfIndex The XF format index
822     * @param mixed $calculatedValue Calculated value
823     *
824     * @return int
825     */
826    private function writeFormula($row, $col, $formula, $xfIndex, $calculatedValue)
827    {
828        $record = 0x0006; // Record identifier
829        // Initialize possible additional value for STRING record that should be written after the FORMULA record?
830        $stringValue = null;
831
832        // calculated value
833        if (isset($calculatedValue)) {
834            // Since we can't yet get the data type of the calculated value,
835            // we use best effort to determine data type
836            if (is_bool($calculatedValue)) {
837                // Boolean value
838                $num = pack('CCCvCv', 0x01, 0x00, (int) $calculatedValue, 0x00, 0x00, 0xFFFF);
839            } elseif (is_int($calculatedValue) || is_float($calculatedValue)) {
840                // Numeric value
841                $num = pack('d', $calculatedValue);
842            } elseif (is_string($calculatedValue)) {
843                $errorCodes = DataType::getErrorCodes();
844                if (isset($errorCodes[$calculatedValue])) {
845                    // Error value
846                    $num = pack('CCCvCv', 0x02, 0x00, ErrorCode::error($calculatedValue), 0x00, 0x00, 0xFFFF);
847                } elseif ($calculatedValue === '') {
848                    // Empty string (and BIFF8)
849                    $num = pack('CCCvCv', 0x03, 0x00, 0x00, 0x00, 0x00, 0xFFFF);
850                } else {
851                    // Non-empty string value (or empty string BIFF5)
852                    $stringValue = $calculatedValue;
853                    $num = pack('CCCvCv', 0x00, 0x00, 0x00, 0x00, 0x00, 0xFFFF);
854                }
855            } else {
856                // We are really not supposed to reach here
857                $num = pack('d', 0x00);
858            }
859        } else {
860            $num = pack('d', 0x00);
861        }
862
863        $grbit = 0x03; // Option flags
864        $unknown = 0x0000; // Must be zero
865
866        // Strip the '=' or '@' sign at the beginning of the formula string
867        if ($formula[0] == '=') {
868            $formula = substr($formula, 1);
869        } else {
870            // Error handling
871            $this->writeString($row, $col, 'Unrecognised character for formula', 0);
872
873            return self::WRITE_FORMULA_ERRORS;
874        }
875
876        // Parse the formula using the parser in Parser.php
877        try {
878            $this->parser->parse($formula);
879            $formula = $this->parser->toReversePolish();
880
881            $formlen = strlen($formula); // Length of the binary string
882            $length = 0x16 + $formlen; // Length of the record data
883
884            $header = pack('vv', $record, $length);
885
886            $data = pack('vvv', $row, $col, $xfIndex)
887                . $num
888                . pack('vVv', $grbit, $unknown, $formlen);
889            $this->append($header . $data . $formula);
890
891            // Append also a STRING record if necessary
892            if ($stringValue !== null) {
893                $this->writeStringRecord($stringValue);
894            }
895
896            return self::WRITE_FORMULA_NORMAL;
897        } catch (PhpSpreadsheetException $e) {
898            return self::WRITE_FORMULA_EXCEPTION;
899        }
900    }
901
902    /**
903     * Write a STRING record. This.
904     *
905     * @param string $stringValue
906     */
907    private function writeStringRecord($stringValue): void
908    {
909        $record = 0x0207; // Record identifier
910        $data = StringHelper::UTF8toBIFF8UnicodeLong($stringValue);
911
912        $length = strlen($data);
913        $header = pack('vv', $record, $length);
914
915        $this->append($header . $data);
916    }
917
918    /**
919     * Write a hyperlink.
920     * This is comprised of two elements: the visible label and
921     * the invisible link. The visible label is the same as the link unless an
922     * alternative string is specified. The label is written using the
923     * writeString() method. Therefore the 255 characters string limit applies.
924     * $string and $format are optional.
925     *
926     * The hyperlink can be to a http, ftp, mail, internal sheet (not yet), or external
927     * directory url.
928     *
929     * @param int $row Row
930     * @param int $col Column
931     * @param string $url URL string
932     */
933    private function writeUrl($row, $col, $url): void
934    {
935        // Add start row and col to arg list
936        $this->writeUrlRange($row, $col, $row, $col, $url);
937    }
938
939    /**
940     * This is the more general form of writeUrl(). It allows a hyperlink to be
941     * written to a range of cells. This function also decides the type of hyperlink
942     * to be written. These are either, Web (http, ftp, mailto), Internal
943     * (Sheet1!A1) or external ('c:\temp\foo.xls#Sheet1!A1').
944     *
945     * @param int $row1 Start row
946     * @param int $col1 Start column
947     * @param int $row2 End row
948     * @param int $col2 End column
949     * @param string $url URL string
950     *
951     * @see writeUrl()
952     */
953    private function writeUrlRange($row1, $col1, $row2, $col2, $url): void
954    {
955        // Check for internal/external sheet links or default to web link
956        if (preg_match('[^internal:]', $url)) {
957            $this->writeUrlInternal($row1, $col1, $row2, $col2, $url);
958        }
959        if (preg_match('[^external:]', $url)) {
960            $this->writeUrlExternal($row1, $col1, $row2, $col2, $url);
961        }
962
963        $this->writeUrlWeb($row1, $col1, $row2, $col2, $url);
964    }
965
966    /**
967     * Used to write http, ftp and mailto hyperlinks.
968     * The link type ($options) is 0x03 is the same as absolute dir ref without
969     * sheet. However it is differentiated by the $unknown2 data stream.
970     *
971     * @param int $row1 Start row
972     * @param int $col1 Start column
973     * @param int $row2 End row
974     * @param int $col2 End column
975     * @param string $url URL string
976     *
977     * @see writeUrl()
978     */
979    public function writeUrlWeb($row1, $col1, $row2, $col2, $url): void
980    {
981        $record = 0x01B8; // Record identifier
982
983        // Pack the undocumented parts of the hyperlink stream
984        $unknown1 = pack('H*', 'D0C9EA79F9BACE118C8200AA004BA90B02000000');
985        $unknown2 = pack('H*', 'E0C9EA79F9BACE118C8200AA004BA90B');
986
987        // Pack the option flags
988        $options = pack('V', 0x03);
989
990        // Convert URL to a null terminated wchar string
991        $url = implode("\0", preg_split("''", $url, -1, PREG_SPLIT_NO_EMPTY));
992        $url = $url . "\0\0\0";
993
994        // Pack the length of the URL
995        $url_len = pack('V', strlen($url));
996
997        // Calculate the data length
998        $length = 0x34 + strlen($url);
999
1000        // Pack the header data
1001        $header = pack('vv', $record, $length);
1002        $data = pack('vvvv', $row1, $row2, $col1, $col2);
1003
1004        // Write the packed data
1005        $this->append($header . $data . $unknown1 . $options . $unknown2 . $url_len . $url);
1006    }
1007
1008    /**
1009     * Used to write internal reference hyperlinks such as "Sheet1!A1".
1010     *
1011     * @param int $row1 Start row
1012     * @param int $col1 Start column
1013     * @param int $row2 End row
1014     * @param int $col2 End column
1015     * @param string $url URL string
1016     *
1017     * @see writeUrl()
1018     */
1019    private function writeUrlInternal($row1, $col1, $row2, $col2, $url): void
1020    {
1021        $record = 0x01B8; // Record identifier
1022
1023        // Strip URL type
1024        $url = preg_replace('/^internal:/', '', $url);
1025
1026        // Pack the undocumented parts of the hyperlink stream
1027        $unknown1 = pack('H*', 'D0C9EA79F9BACE118C8200AA004BA90B02000000');
1028
1029        // Pack the option flags
1030        $options = pack('V', 0x08);
1031
1032        // Convert the URL type and to a null terminated wchar string
1033        $url .= "\0";
1034
1035        // character count
1036        $url_len = StringHelper::countCharacters($url);
1037        $url_len = pack('V', $url_len);
1038
1039        $url = StringHelper::convertEncoding($url, 'UTF-16LE', 'UTF-8');
1040
1041        // Calculate the data length
1042        $length = 0x24 + strlen($url);
1043
1044        // Pack the header data
1045        $header = pack('vv', $record, $length);
1046        $data = pack('vvvv', $row1, $row2, $col1, $col2);
1047
1048        // Write the packed data
1049        $this->append($header . $data . $unknown1 . $options . $url_len . $url);
1050    }
1051
1052    /**
1053     * Write links to external directory names such as 'c:\foo.xls',
1054     * c:\foo.xls#Sheet1!A1', '../../foo.xls'. and '../../foo.xls#Sheet1!A1'.
1055     *
1056     * Note: Excel writes some relative links with the $dir_long string. We ignore
1057     * these cases for the sake of simpler code.
1058     *
1059     * @param int $row1 Start row
1060     * @param int $col1 Start column
1061     * @param int $row2 End row
1062     * @param int $col2 End column
1063     * @param string $url URL string
1064     *
1065     * @see writeUrl()
1066     */
1067    private function writeUrlExternal($row1, $col1, $row2, $col2, $url): void
1068    {
1069        // Network drives are different. We will handle them separately
1070        // MS/Novell network drives and shares start with \\
1071        if (preg_match('[^external:\\\\]', $url)) {
1072            return;
1073        }
1074
1075        $record = 0x01B8; // Record identifier
1076        $length = 0x00000; // Bytes to follow
1077
1078        // Strip URL type and change Unix dir separator to Dos style (if needed)
1079        //
1080        $url = preg_replace('/^external:/', '', $url);
1081        $url = preg_replace('/\//', '\\', $url);
1082
1083        // Determine if the link is relative or absolute:
1084        //   relative if link contains no dir separator, "somefile.xls"
1085        //   relative if link starts with up-dir, "..\..\somefile.xls"
1086        //   otherwise, absolute
1087
1088        $absolute = 0x00; // relative path
1089        if (preg_match('/^[A-Z]:/', $url)) {
1090            $absolute = 0x02; // absolute path on Windows, e.g. C:\...
1091        }
1092        $link_type = 0x01 | $absolute;
1093
1094        // Determine if the link contains a sheet reference and change some of the
1095        // parameters accordingly.
1096        // Split the dir name and sheet name (if it exists)
1097        $dir_long = $url;
1098        if (preg_match('/\\#/', $url)) {
1099            $link_type |= 0x08;
1100        }
1101
1102        // Pack the link type
1103        $link_type = pack('V', $link_type);
1104
1105        // Calculate the up-level dir count e.g.. (..\..\..\ == 3)
1106        $up_count = preg_match_all('/\\.\\.\\\\/', $dir_long, $useless);
1107        $up_count = pack('v', $up_count);
1108
1109        // Store the short dos dir name (null terminated)
1110        $dir_short = preg_replace('/\\.\\.\\\\/', '', $dir_long) . "\0";
1111
1112        // Store the long dir name as a wchar string (non-null terminated)
1113        $dir_long = $dir_long . "\0";
1114
1115        // Pack the lengths of the dir strings
1116        $dir_short_len = pack('V', strlen($dir_short));
1117        $dir_long_len = pack('V', strlen($dir_long));
1118        $stream_len = pack('V', 0); //strlen($dir_long) + 0x06);
1119
1120        // Pack the undocumented parts of the hyperlink stream
1121        $unknown1 = pack('H*', 'D0C9EA79F9BACE118C8200AA004BA90B02000000');
1122        $unknown2 = pack('H*', '0303000000000000C000000000000046');
1123        $unknown3 = pack('H*', 'FFFFADDE000000000000000000000000000000000000000');
1124        $unknown4 = pack('v', 0x03);
1125
1126        // Pack the main data stream
1127        $data = pack('vvvv', $row1, $row2, $col1, $col2) .
1128            $unknown1 .
1129            $link_type .
1130            $unknown2 .
1131            $up_count .
1132            $dir_short_len .
1133            $dir_short .
1134            $unknown3 .
1135            $stream_len; /*.
1136                          $dir_long_len .
1137                          $unknown4     .
1138                          $dir_long     .
1139                          $sheet_len    .
1140                          $sheet        ;*/
1141
1142        // Pack the header data
1143        $length = strlen($data);
1144        $header = pack('vv', $record, $length);
1145
1146        // Write the packed data
1147        $this->append($header . $data);
1148    }
1149
1150    /**
1151     * This method is used to set the height and format for a row.
1152     *
1153     * @param int $row The row to set
1154     * @param int $height Height we are giving to the row.
1155     *                        Use null to set XF without setting height
1156     * @param int $xfIndex The optional cell style Xf index to apply to the columns
1157     * @param bool $hidden The optional hidden attribute
1158     * @param int $level The optional outline level for row, in range [0,7]
1159     */
1160    private function writeRow($row, $height, $xfIndex, $hidden = false, $level = 0): void
1161    {
1162        $record = 0x0208; // Record identifier
1163        $length = 0x0010; // Number of bytes to follow
1164
1165        $colMic = 0x0000; // First defined column
1166        $colMac = 0x0000; // Last defined column
1167        $irwMac = 0x0000; // Used by Excel to optimise loading
1168        $reserved = 0x0000; // Reserved
1169        $grbit = 0x0000; // Option flags
1170        $ixfe = $xfIndex;
1171
1172        if ($height < 0) {
1173            $height = null;
1174        }
1175
1176        // Use writeRow($row, null, $XF) to set XF format without setting height
1177        if ($height !== null) {
1178            $miyRw = $height * 20; // row height
1179        } else {
1180            $miyRw = 0xff; // default row height is 256
1181        }
1182
1183        // Set the options flags. fUnsynced is used to show that the font and row
1184        // heights are not compatible. This is usually the case for WriteExcel.
1185        // The collapsed flag 0x10 doesn't seem to be used to indicate that a row
1186        // is collapsed. Instead it is used to indicate that the previous row is
1187        // collapsed. The zero height flag, 0x20, is used to collapse a row.
1188
1189        $grbit |= $level;
1190        if ($hidden === true) {
1191            $grbit |= 0x0030;
1192        }
1193        if ($height !== null) {
1194            $grbit |= 0x0040; // fUnsynced
1195        }
1196        if ($xfIndex !== 0xF) {
1197            $grbit |= 0x0080;
1198        }
1199        $grbit |= 0x0100;
1200
1201        $header = pack('vv', $record, $length);
1202        $data = pack('vvvvvvvv', $row, $colMic, $colMac, $miyRw, $irwMac, $reserved, $grbit, $ixfe);
1203        $this->append($header . $data);
1204    }
1205
1206    /**
1207     * Writes Excel DIMENSIONS to define the area in which there is data.
1208     */
1209    private function writeDimensions(): void
1210    {
1211        $record = 0x0200; // Record identifier
1212
1213        $length = 0x000E;
1214        $data = pack('VVvvv', $this->firstRowIndex, $this->lastRowIndex + 1, $this->firstColumnIndex, $this->lastColumnIndex + 1, 0x0000); // reserved
1215
1216        $header = pack('vv', $record, $length);
1217        $this->append($header . $data);
1218    }
1219
1220    /**
1221     * Write BIFF record Window2.
1222     */
1223    private function writeWindow2(): void
1224    {
1225        $record = 0x023E; // Record identifier
1226        $length = 0x0012;
1227
1228        $grbit = 0x00B6; // Option flags
1229        $rwTop = 0x0000; // Top row visible in window
1230        $colLeft = 0x0000; // Leftmost column visible in window
1231
1232        // The options flags that comprise $grbit
1233        $fDspFmla = 0; // 0 - bit
1234        $fDspGrid = $this->phpSheet->getShowGridlines() ? 1 : 0; // 1
1235        $fDspRwCol = $this->phpSheet->getShowRowColHeaders() ? 1 : 0; // 2
1236        $fFrozen = $this->phpSheet->getFreezePane() ? 1 : 0; // 3
1237        $fDspZeros = 1; // 4
1238        $fDefaultHdr = 1; // 5
1239        $fArabic = $this->phpSheet->getRightToLeft() ? 1 : 0; // 6
1240        $fDspGuts = $this->outlineOn; // 7
1241        $fFrozenNoSplit = 0; // 0 - bit
1242        // no support in PhpSpreadsheet for selected sheet, therefore sheet is only selected if it is the active sheet
1243        $fSelected = ($this->phpSheet === $this->phpSheet->getParent()->getActiveSheet()) ? 1 : 0;
1244        $fPageBreakPreview = $this->phpSheet->getSheetView()->getView() === SheetView::SHEETVIEW_PAGE_BREAK_PREVIEW;
1245
1246        $grbit = $fDspFmla;
1247        $grbit |= $fDspGrid << 1;
1248        $grbit |= $fDspRwCol << 2;
1249        $grbit |= $fFrozen << 3;
1250        $grbit |= $fDspZeros << 4;
1251        $grbit |= $fDefaultHdr << 5;
1252        $grbit |= $fArabic << 6;
1253        $grbit |= $fDspGuts << 7;
1254        $grbit |= $fFrozenNoSplit << 8;
1255        $grbit |= $fSelected << 9; // Selected sheets.
1256        $grbit |= $fSelected << 10; // Active sheet.
1257        $grbit |= $fPageBreakPreview << 11;
1258
1259        $header = pack('vv', $record, $length);
1260        $data = pack('vvv', $grbit, $rwTop, $colLeft);
1261
1262        // FIXME !!!
1263        $rgbHdr = 0x0040; // Row/column heading and gridline color index
1264        $zoom_factor_page_break = ($fPageBreakPreview ? $this->phpSheet->getSheetView()->getZoomScale() : 0x0000);
1265        $zoom_factor_normal = $this->phpSheet->getSheetView()->getZoomScaleNormal();
1266
1267        $data .= pack('vvvvV', $rgbHdr, 0x0000, $zoom_factor_page_break, $zoom_factor_normal, 0x00000000);
1268
1269        $this->append($header . $data);
1270    }
1271
1272    /**
1273     * Write BIFF record DEFAULTROWHEIGHT.
1274     */
1275    private function writeDefaultRowHeight(): void
1276    {
1277        $defaultRowHeight = $this->phpSheet->getDefaultRowDimension()->getRowHeight();
1278
1279        if ($defaultRowHeight < 0) {
1280            return;
1281        }
1282
1283        // convert to twips
1284        $defaultRowHeight = (int) 20 * $defaultRowHeight;
1285
1286        $record = 0x0225; // Record identifier
1287        $length = 0x0004; // Number of bytes to follow
1288
1289        $header = pack('vv', $record, $length);
1290        $data = pack('vv', 1, $defaultRowHeight);
1291        $this->append($header . $data);
1292    }
1293
1294    /**
1295     * Write BIFF record DEFCOLWIDTH if COLINFO records are in use.
1296     */
1297    private function writeDefcol(): void
1298    {
1299        $defaultColWidth = 8;
1300
1301        $record = 0x0055; // Record identifier
1302        $length = 0x0002; // Number of bytes to follow
1303
1304        $header = pack('vv', $record, $length);
1305        $data = pack('v', $defaultColWidth);
1306        $this->append($header . $data);
1307    }
1308
1309    /**
1310     * Write BIFF record COLINFO to define column widths.
1311     *
1312     * Note: The SDK says the record length is 0x0B but Excel writes a 0x0C
1313     * length record.
1314     *
1315     * @param array $col_array This is the only parameter received and is composed of the following:
1316     *                0 => First formatted column,
1317     *                1 => Last formatted column,
1318     *                2 => Col width (8.43 is Excel default),
1319     *                3 => The optional XF format of the column,
1320     *                4 => Option flags.
1321     *                5 => Optional outline level
1322     */
1323    private function writeColinfo($col_array): void
1324    {
1325        $colFirst = $col_array[0] ?? null;
1326        $colLast = $col_array[1] ?? null;
1327        $coldx = $col_array[2] ?? 8.43;
1328        $xfIndex = $col_array[3] ?? 15;
1329        $grbit = $col_array[4] ?? 0;
1330        $level = $col_array[5] ?? 0;
1331
1332        $record = 0x007D; // Record identifier
1333        $length = 0x000C; // Number of bytes to follow
1334
1335        $coldx *= 256; // Convert to units of 1/256 of a char
1336
1337        $ixfe = $xfIndex;
1338        $reserved = 0x0000; // Reserved
1339
1340        $level = max(0, min($level, 7));
1341        $grbit |= $level << 8;
1342
1343        $header = pack('vv', $record, $length);
1344        $data = pack('vvvvvv', $colFirst, $colLast, $coldx, $ixfe, $grbit, $reserved);
1345        $this->append($header . $data);
1346    }
1347
1348    /**
1349     * Write BIFF record SELECTION.
1350     */
1351    private function writeSelection(): void
1352    {
1353        // look up the selected cell range
1354        $selectedCells = Coordinate::splitRange($this->phpSheet->getSelectedCells());
1355        $selectedCells = $selectedCells[0];
1356        if (count($selectedCells) == 2) {
1357            [$first, $last] = $selectedCells;
1358        } else {
1359            $first = $selectedCells[0];
1360            $last = $selectedCells[0];
1361        }
1362
1363        [$colFirst, $rwFirst] = Coordinate::coordinateFromString($first);
1364        $colFirst = Coordinate::columnIndexFromString($colFirst) - 1; // base 0 column index
1365        --$rwFirst; // base 0 row index
1366
1367        [$colLast, $rwLast] = Coordinate::coordinateFromString($last);
1368        $colLast = Coordinate::columnIndexFromString($colLast) - 1; // base 0 column index
1369        --$rwLast; // base 0 row index
1370
1371        // make sure we are not out of bounds
1372        $colFirst = min($colFirst, 255);
1373        $colLast = min($colLast, 255);
1374
1375        $rwFirst = min($rwFirst, 65535);
1376        $rwLast = min($rwLast, 65535);
1377
1378        $record = 0x001D; // Record identifier
1379        $length = 0x000F; // Number of bytes to follow
1380
1381        $pnn = $this->activePane; // Pane position
1382        $rwAct = $rwFirst; // Active row
1383        $colAct = $colFirst; // Active column
1384        $irefAct = 0; // Active cell ref
1385        $cref = 1; // Number of refs
1386
1387        // Swap last row/col for first row/col as necessary
1388        if ($rwFirst > $rwLast) {
1389            [$rwFirst, $rwLast] = [$rwLast, $rwFirst];
1390        }
1391
1392        if ($colFirst > $colLast) {
1393            [$colFirst, $colLast] = [$colLast, $colFirst];
1394        }
1395
1396        $header = pack('vv', $record, $length);
1397        $data = pack('CvvvvvvCC', $pnn, $rwAct, $colAct, $irefAct, $cref, $rwFirst, $rwLast, $colFirst, $colLast);
1398        $this->append($header . $data);
1399    }
1400
1401    /**
1402     * Store the MERGEDCELLS records for all ranges of merged cells.
1403     */
1404    private function writeMergedCells(): void
1405    {
1406        $mergeCells = $this->phpSheet->getMergeCells();
1407        $countMergeCells = count($mergeCells);
1408
1409        if ($countMergeCells == 0) {
1410            return;
1411        }
1412
1413        // maximum allowed number of merged cells per record
1414        $maxCountMergeCellsPerRecord = 1027;
1415
1416        // record identifier
1417        $record = 0x00E5;
1418
1419        // counter for total number of merged cells treated so far by the writer
1420        $i = 0;
1421
1422        // counter for number of merged cells written in record currently being written
1423        $j = 0;
1424
1425        // initialize record data
1426        $recordData = '';
1427
1428        // loop through the merged cells
1429        foreach ($mergeCells as $mergeCell) {
1430            ++$i;
1431            ++$j;
1432
1433            // extract the row and column indexes
1434            $range = Coordinate::splitRange($mergeCell);
1435            [$first, $last] = $range[0];
1436            [$firstColumn, $firstRow] = Coordinate::indexesFromString($first);
1437            [$lastColumn, $lastRow] = Coordinate::indexesFromString($last);
1438
1439            $recordData .= pack('vvvv', $firstRow - 1, $lastRow - 1, $firstColumn - 1, $lastColumn - 1);
1440
1441            // flush record if we have reached limit for number of merged cells, or reached final merged cell
1442            if ($j == $maxCountMergeCellsPerRecord || $i == $countMergeCells) {
1443                $recordData = pack('v', $j) . $recordData;
1444                $length = strlen($recordData);
1445                $header = pack('vv', $record, $length);
1446                $this->append($header . $recordData);
1447
1448                // initialize for next record, if any
1449                $recordData = '';
1450                $j = 0;
1451            }
1452        }
1453    }
1454
1455    /**
1456     * Write SHEETLAYOUT record.
1457     */
1458    private function writeSheetLayout(): void
1459    {
1460        if (!$this->phpSheet->isTabColorSet()) {
1461            return;
1462        }
1463
1464        $recordData = pack(
1465            'vvVVVvv',
1466            0x0862,
1467            0x0000, // unused
1468            0x00000000, // unused
1469            0x00000000, // unused
1470            0x00000014, // size of record data
1471            $this->colors[$this->phpSheet->getTabColor()->getRGB()], // color index
1472            0x0000        // unused
1473        );
1474
1475        $length = strlen($recordData);
1476
1477        $record = 0x0862; // Record identifier
1478        $header = pack('vv', $record, $length);
1479        $this->append($header . $recordData);
1480    }
1481
1482    /**
1483     * Write SHEETPROTECTION.
1484     */
1485    private function writeSheetProtection(): void
1486    {
1487        // record identifier
1488        $record = 0x0867;
1489
1490        // prepare options
1491        $options = (int) !$this->phpSheet->getProtection()->getObjects()
1492            | (int) !$this->phpSheet->getProtection()->getScenarios() << 1
1493            | (int) !$this->phpSheet->getProtection()->getFormatCells() << 2
1494            | (int) !$this->phpSheet->getProtection()->getFormatColumns() << 3
1495            | (int) !$this->phpSheet->getProtection()->getFormatRows() << 4
1496            | (int) !$this->phpSheet->getProtection()->getInsertColumns() << 5
1497            | (int) !$this->phpSheet->getProtection()->getInsertRows() << 6
1498            | (int) !$this->phpSheet->getProtection()->getInsertHyperlinks() << 7
1499            | (int) !$this->phpSheet->getProtection()->getDeleteColumns() << 8
1500            | (int) !$this->phpSheet->getProtection()->getDeleteRows() << 9
1501            | (int) !$this->phpSheet->getProtection()->getSelectLockedCells() << 10
1502            | (int) !$this->phpSheet->getProtection()->getSort() << 11
1503            | (int) !$this->phpSheet->getProtection()->getAutoFilter() << 12
1504            | (int) !$this->phpSheet->getProtection()->getPivotTables() << 13
1505            | (int) !$this->phpSheet->getProtection()->getSelectUnlockedCells() << 14;
1506
1507        // record data
1508        $recordData = pack(
1509            'vVVCVVvv',
1510            0x0867, // repeated record identifier
1511            0x0000, // not used
1512            0x0000, // not used
1513            0x00, // not used
1514            0x01000200, // unknown data
1515            0xFFFFFFFF, // unknown data
1516            $options, // options
1517            0x0000 // not used
1518        );
1519
1520        $length = strlen($recordData);
1521        $header = pack('vv', $record, $length);
1522
1523        $this->append($header . $recordData);
1524    }
1525
1526    /**
1527     * Write BIFF record RANGEPROTECTION.
1528     *
1529     * Openoffice.org's Documentation of the Microsoft Excel File Format uses term RANGEPROTECTION for these records
1530     * Microsoft Office Excel 97-2007 Binary File Format Specification uses term FEAT for these records
1531     */
1532    private function writeRangeProtection(): void
1533    {
1534        foreach ($this->phpSheet->getProtectedCells() as $range => $password) {
1535            // number of ranges, e.g. 'A1:B3 C20:D25'
1536            $cellRanges = explode(' ', $range);
1537            $cref = count($cellRanges);
1538
1539            $recordData = pack(
1540                'vvVVvCVvVv',
1541                0x0868,
1542                0x00,
1543                0x0000,
1544                0x0000,
1545                0x02,
1546                0x0,
1547                0x0000,
1548                $cref,
1549                0x0000,
1550                0x00
1551            );
1552
1553            foreach ($cellRanges as $cellRange) {
1554                $recordData .= $this->writeBIFF8CellRangeAddressFixed($cellRange);
1555            }
1556
1557            // the rgbFeat structure
1558            $recordData .= pack(
1559                'VV',
1560                0x0000,
1561                hexdec($password)
1562            );
1563
1564            $recordData .= StringHelper::UTF8toBIFF8UnicodeLong('p' . md5($recordData));
1565
1566            $length = strlen($recordData);
1567
1568            $record = 0x0868; // Record identifier
1569            $header = pack('vv', $record, $length);
1570            $this->append($header . $recordData);
1571        }
1572    }
1573
1574    /**
1575     * Writes the Excel BIFF PANE record.
1576     * The panes can either be frozen or thawed (unfrozen).
1577     * Frozen panes are specified in terms of an integer number of rows and columns.
1578     * Thawed panes are specified in terms of Excel's units for rows and columns.
1579     */
1580    private function writePanes(): void
1581    {
1582        if (!$this->phpSheet->getFreezePane()) {
1583            // thaw panes
1584            return;
1585        }
1586
1587        [$column, $row] = Coordinate::indexesFromString($this->phpSheet->getFreezePane());
1588        $x = $column - 1;
1589        $y = $row - 1;
1590
1591        [$leftMostColumn, $topRow] = Coordinate::indexesFromString($this->phpSheet->getTopLeftCell());
1592        //Coordinates are zero-based in xls files
1593        $rwTop = $topRow - 1;
1594        $colLeft = $leftMostColumn - 1;
1595
1596        $record = 0x0041; // Record identifier
1597        $length = 0x000A; // Number of bytes to follow
1598
1599        // Determine which pane should be active. There is also the undocumented
1600        // option to override this should it be necessary: may be removed later.
1601        $pnnAct = null;
1602        if ($x != 0 && $y != 0) {
1603            $pnnAct = 0; // Bottom right
1604        }
1605        if ($x != 0 && $y == 0) {
1606            $pnnAct = 1; // Top right
1607        }
1608        if ($x == 0 && $y != 0) {
1609            $pnnAct = 2; // Bottom left
1610        }
1611        if ($x == 0 && $y == 0) {
1612            $pnnAct = 3; // Top left
1613        }
1614
1615        $this->activePane = $pnnAct; // Used in writeSelection
1616
1617        $header = pack('vv', $record, $length);
1618        $data = pack('vvvvv', $x, $y, $rwTop, $colLeft, $pnnAct);
1619        $this->append($header . $data);
1620    }
1621
1622    /**
1623     * Store the page setup SETUP BIFF record.
1624     */
1625    private function writeSetup(): void
1626    {
1627        $record = 0x00A1; // Record identifier
1628        $length = 0x0022; // Number of bytes to follow
1629
1630        $iPaperSize = $this->phpSheet->getPageSetup()->getPaperSize(); // Paper size
1631        $iScale = $this->phpSheet->getPageSetup()->getScale() ?: 100; // Print scaling factor
1632
1633        $iPageStart = 0x01; // Starting page number
1634        $iFitWidth = (int) $this->phpSheet->getPageSetup()->getFitToWidth(); // Fit to number of pages wide
1635        $iFitHeight = (int) $this->phpSheet->getPageSetup()->getFitToHeight(); // Fit to number of pages high
1636        $grbit = 0x00; // Option flags
1637        $iRes = 0x0258; // Print resolution
1638        $iVRes = 0x0258; // Vertical print resolution
1639
1640        $numHdr = $this->phpSheet->getPageMargins()->getHeader(); // Header Margin
1641
1642        $numFtr = $this->phpSheet->getPageMargins()->getFooter(); // Footer Margin
1643        $iCopies = 0x01; // Number of copies
1644
1645        // Order of printing pages
1646        $fLeftToRight = $this->phpSheet->getPageSetup()->getPageOrder() === PageSetup::PAGEORDER_DOWN_THEN_OVER
1647            ? 0x1 : 0x0;
1648        // Page orientation
1649        $fLandscape = ($this->phpSheet->getPageSetup()->getOrientation() == PageSetup::ORIENTATION_LANDSCAPE)
1650            ? 0x0 : 0x1;
1651
1652        $fNoPls = 0x0; // Setup not read from printer
1653        $fNoColor = 0x0; // Print black and white
1654        $fDraft = 0x0; // Print draft quality
1655        $fNotes = 0x0; // Print notes
1656        $fNoOrient = 0x0; // Orientation not set
1657        $fUsePage = 0x0; // Use custom starting page
1658
1659        $grbit = $fLeftToRight;
1660        $grbit |= $fLandscape << 1;
1661        $grbit |= $fNoPls << 2;
1662        $grbit |= $fNoColor << 3;
1663        $grbit |= $fDraft << 4;
1664        $grbit |= $fNotes << 5;
1665        $grbit |= $fNoOrient << 6;
1666        $grbit |= $fUsePage << 7;
1667
1668        $numHdr = pack('d', $numHdr);
1669        $numFtr = pack('d', $numFtr);
1670        if (self::getByteOrder()) { // if it's Big Endian
1671            $numHdr = strrev($numHdr);
1672            $numFtr = strrev($numFtr);
1673        }
1674
1675        $header = pack('vv', $record, $length);
1676        $data1 = pack('vvvvvvvv', $iPaperSize, $iScale, $iPageStart, $iFitWidth, $iFitHeight, $grbit, $iRes, $iVRes);
1677        $data2 = $numHdr . $numFtr;
1678        $data3 = pack('v', $iCopies);
1679        $this->append($header . $data1 . $data2 . $data3);
1680    }
1681
1682    /**
1683     * Store the header caption BIFF record.
1684     */
1685    private function writeHeader(): void
1686    {
1687        $record = 0x0014; // Record identifier
1688
1689        /* removing for now
1690        // need to fix character count (multibyte!)
1691        if (strlen($this->phpSheet->getHeaderFooter()->getOddHeader()) <= 255) {
1692            $str      = $this->phpSheet->getHeaderFooter()->getOddHeader();       // header string
1693        } else {
1694            $str = '';
1695        }
1696        */
1697
1698        $recordData = StringHelper::UTF8toBIFF8UnicodeLong($this->phpSheet->getHeaderFooter()->getOddHeader());
1699        $length = strlen($recordData);
1700
1701        $header = pack('vv', $record, $length);
1702
1703        $this->append($header . $recordData);
1704    }
1705
1706    /**
1707     * Store the footer caption BIFF record.
1708     */
1709    private function writeFooter(): void
1710    {
1711        $record = 0x0015; // Record identifier
1712
1713        /* removing for now
1714        // need to fix character count (multibyte!)
1715        if (strlen($this->phpSheet->getHeaderFooter()->getOddFooter()) <= 255) {
1716            $str = $this->phpSheet->getHeaderFooter()->getOddFooter();
1717        } else {
1718            $str = '';
1719        }
1720        */
1721
1722        $recordData = StringHelper::UTF8toBIFF8UnicodeLong($this->phpSheet->getHeaderFooter()->getOddFooter());
1723        $length = strlen($recordData);
1724
1725        $header = pack('vv', $record, $length);
1726
1727        $this->append($header . $recordData);
1728    }
1729
1730    /**
1731     * Store the horizontal centering HCENTER BIFF record.
1732     */
1733    private function writeHcenter(): void
1734    {
1735        $record = 0x0083; // Record identifier
1736        $length = 0x0002; // Bytes to follow
1737
1738        $fHCenter = $this->phpSheet->getPageSetup()->getHorizontalCentered() ? 1 : 0; // Horizontal centering
1739
1740        $header = pack('vv', $record, $length);
1741        $data = pack('v', $fHCenter);
1742
1743        $this->append($header . $data);
1744    }
1745
1746    /**
1747     * Store the vertical centering VCENTER BIFF record.
1748     */
1749    private function writeVcenter(): void
1750    {
1751        $record = 0x0084; // Record identifier
1752        $length = 0x0002; // Bytes to follow
1753
1754        $fVCenter = $this->phpSheet->getPageSetup()->getVerticalCentered() ? 1 : 0; // Horizontal centering
1755
1756        $header = pack('vv', $record, $length);
1757        $data = pack('v', $fVCenter);
1758        $this->append($header . $data);
1759    }
1760
1761    /**
1762     * Store the LEFTMARGIN BIFF record.
1763     */
1764    private function writeMarginLeft(): void
1765    {
1766        $record = 0x0026; // Record identifier
1767        $length = 0x0008; // Bytes to follow
1768
1769        $margin = $this->phpSheet->getPageMargins()->getLeft(); // Margin in inches
1770
1771        $header = pack('vv', $record, $length);
1772        $data = pack('d', $margin);
1773        if (self::getByteOrder()) { // if it's Big Endian
1774            $data = strrev($data);
1775        }
1776
1777        $this->append($header . $data);
1778    }
1779
1780    /**
1781     * Store the RIGHTMARGIN BIFF record.
1782     */
1783    private function writeMarginRight(): void
1784    {
1785        $record = 0x0027; // Record identifier
1786        $length = 0x0008; // Bytes to follow
1787
1788        $margin = $this->phpSheet->getPageMargins()->getRight(); // Margin in inches
1789
1790        $header = pack('vv', $record, $length);
1791        $data = pack('d', $margin);
1792        if (self::getByteOrder()) { // if it's Big Endian
1793            $data = strrev($data);
1794        }
1795
1796        $this->append($header . $data);
1797    }
1798
1799    /**
1800     * Store the TOPMARGIN BIFF record.
1801     */
1802    private function writeMarginTop(): void
1803    {
1804        $record = 0x0028; // Record identifier
1805        $length = 0x0008; // Bytes to follow
1806
1807        $margin = $this->phpSheet->getPageMargins()->getTop(); // Margin in inches
1808
1809        $header = pack('vv', $record, $length);
1810        $data = pack('d', $margin);
1811        if (self::getByteOrder()) { // if it's Big Endian
1812            $data = strrev($data);
1813        }
1814
1815        $this->append($header . $data);
1816    }
1817
1818    /**
1819     * Store the BOTTOMMARGIN BIFF record.
1820     */
1821    private function writeMarginBottom(): void
1822    {
1823        $record = 0x0029; // Record identifier
1824        $length = 0x0008; // Bytes to follow
1825
1826        $margin = $this->phpSheet->getPageMargins()->getBottom(); // Margin in inches
1827
1828        $header = pack('vv', $record, $length);
1829        $data = pack('d', $margin);
1830        if (self::getByteOrder()) { // if it's Big Endian
1831            $data = strrev($data);
1832        }
1833
1834        $this->append($header . $data);
1835    }
1836
1837    /**
1838     * Write the PRINTHEADERS BIFF record.
1839     */
1840    private function writePrintHeaders(): void
1841    {
1842        $record = 0x002a; // Record identifier
1843        $length = 0x0002; // Bytes to follow
1844
1845        $fPrintRwCol = $this->printHeaders; // Boolean flag
1846
1847        $header = pack('vv', $record, $length);
1848        $data = pack('v', $fPrintRwCol);
1849        $this->append($header . $data);
1850    }
1851
1852    /**
1853     * Write the PRINTGRIDLINES BIFF record. Must be used in conjunction with the
1854     * GRIDSET record.
1855     */
1856    private function writePrintGridlines(): void
1857    {
1858        $record = 0x002b; // Record identifier
1859        $length = 0x0002; // Bytes to follow
1860
1861        $fPrintGrid = $this->phpSheet->getPrintGridlines() ? 1 : 0; // Boolean flag
1862
1863        $header = pack('vv', $record, $length);
1864        $data = pack('v', $fPrintGrid);
1865        $this->append($header . $data);
1866    }
1867
1868    /**
1869     * Write the GRIDSET BIFF record. Must be used in conjunction with the
1870     * PRINTGRIDLINES record.
1871     */
1872    private function writeGridset(): void
1873    {
1874        $record = 0x0082; // Record identifier
1875        $length = 0x0002; // Bytes to follow
1876
1877        $fGridSet = !$this->phpSheet->getPrintGridlines(); // Boolean flag
1878
1879        $header = pack('vv', $record, $length);
1880        $data = pack('v', $fGridSet);
1881        $this->append($header . $data);
1882    }
1883
1884    /**
1885     * Write the AUTOFILTERINFO BIFF record. This is used to configure the number of autofilter select used in the sheet.
1886     */
1887    private function writeAutoFilterInfo(): void
1888    {
1889        $record = 0x009D; // Record identifier
1890        $length = 0x0002; // Bytes to follow
1891
1892        $rangeBounds = Coordinate::rangeBoundaries($this->phpSheet->getAutoFilter()->getRange());
1893        $iNumFilters = 1 + $rangeBounds[1][0] - $rangeBounds[0][0];
1894
1895        $header = pack('vv', $record, $length);
1896        $data = pack('v', $iNumFilters);
1897        $this->append($header . $data);
1898    }
1899
1900    /**
1901     * Write the GUTS BIFF record. This is used to configure the gutter margins
1902     * where Excel outline symbols are displayed. The visibility of the gutters is
1903     * controlled by a flag in WSBOOL.
1904     *
1905     * @see writeWsbool()
1906     */
1907    private function writeGuts(): void
1908    {
1909        $record = 0x0080; // Record identifier
1910        $length = 0x0008; // Bytes to follow
1911
1912        $dxRwGut = 0x0000; // Size of row gutter
1913        $dxColGut = 0x0000; // Size of col gutter
1914
1915        // determine maximum row outline level
1916        $maxRowOutlineLevel = 0;
1917        foreach ($this->phpSheet->getRowDimensions() as $rowDimension) {
1918            $maxRowOutlineLevel = max($maxRowOutlineLevel, $rowDimension->getOutlineLevel());
1919        }
1920
1921        $col_level = 0;
1922
1923        // Calculate the maximum column outline level. The equivalent calculation
1924        // for the row outline level is carried out in writeRow().
1925        $colcount = count($this->columnInfo);
1926        for ($i = 0; $i < $colcount; ++$i) {
1927            $col_level = max($this->columnInfo[$i][5], $col_level);
1928        }
1929
1930        // Set the limits for the outline levels (0 <= x <= 7).
1931        $col_level = max(0, min($col_level, 7));
1932
1933        // The displayed level is one greater than the max outline levels
1934        if ($maxRowOutlineLevel) {
1935            ++$maxRowOutlineLevel;
1936        }
1937        if ($col_level) {
1938            ++$col_level;
1939        }
1940
1941        $header = pack('vv', $record, $length);
1942        $data = pack('vvvv', $dxRwGut, $dxColGut, $maxRowOutlineLevel, $col_level);
1943
1944        $this->append($header . $data);
1945    }
1946
1947    /**
1948     * Write the WSBOOL BIFF record, mainly for fit-to-page. Used in conjunction
1949     * with the SETUP record.
1950     */
1951    private function writeWsbool(): void
1952    {
1953        $record = 0x0081; // Record identifier
1954        $length = 0x0002; // Bytes to follow
1955        $grbit = 0x0000;
1956
1957        // The only option that is of interest is the flag for fit to page. So we
1958        // set all the options in one go.
1959        //
1960        // Set the option flags
1961        $grbit |= 0x0001; // Auto page breaks visible
1962        if ($this->outlineStyle) {
1963            $grbit |= 0x0020; // Auto outline styles
1964        }
1965        if ($this->phpSheet->getShowSummaryBelow()) {
1966            $grbit |= 0x0040; // Outline summary below
1967        }
1968        if ($this->phpSheet->getShowSummaryRight()) {
1969            $grbit |= 0x0080; // Outline summary right
1970        }
1971        if ($this->phpSheet->getPageSetup()->getFitToPage()) {
1972            $grbit |= 0x0100; // Page setup fit to page
1973        }
1974        if ($this->outlineOn) {
1975            $grbit |= 0x0400; // Outline symbols displayed
1976        }
1977
1978        $header = pack('vv', $record, $length);
1979        $data = pack('v', $grbit);
1980        $this->append($header . $data);
1981    }
1982
1983    /**
1984     * Write the HORIZONTALPAGEBREAKS and VERTICALPAGEBREAKS BIFF records.
1985     */
1986    private function writeBreaks(): void
1987    {
1988        // initialize
1989        $vbreaks = [];
1990        $hbreaks = [];
1991
1992        foreach ($this->phpSheet->getBreaks() as $cell => $breakType) {
1993            // Fetch coordinates
1994            $coordinates = Coordinate::coordinateFromString($cell);
1995
1996            // Decide what to do by the type of break
1997            switch ($breakType) {
1998                case \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::BREAK_COLUMN:
1999                    // Add to list of vertical breaks
2000                    $vbreaks[] = Coordinate::columnIndexFromString($coordinates[0]) - 1;
2001
2002                    break;
2003                case \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::BREAK_ROW:
2004                    // Add to list of horizontal breaks
2005                    $hbreaks[] = $coordinates[1];
2006
2007                    break;
2008                case \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::BREAK_NONE:
2009                default:
2010                    // Nothing to do
2011                    break;
2012            }
2013        }
2014
2015        //horizontal page breaks
2016        if (!empty($hbreaks)) {
2017            // Sort and filter array of page breaks
2018            sort($hbreaks, SORT_NUMERIC);
2019            if ($hbreaks[0] == 0) { // don't use first break if it's 0
2020                array_shift($hbreaks);
2021            }
2022
2023            $record = 0x001b; // Record identifier
2024            $cbrk = count($hbreaks); // Number of page breaks
2025            $length = 2 + 6 * $cbrk; // Bytes to follow
2026
2027            $header = pack('vv', $record, $length);
2028            $data = pack('v', $cbrk);
2029
2030            // Append each page break
2031            foreach ($hbreaks as $hbreak) {
2032                $data .= pack('vvv', $hbreak, 0x0000, 0x00ff);
2033            }
2034
2035            $this->append($header . $data);
2036        }
2037
2038        // vertical page breaks
2039        if (!empty($vbreaks)) {
2040            // 1000 vertical pagebreaks appears to be an internal Excel 5 limit.
2041            // It is slightly higher in Excel 97/200, approx. 1026
2042            $vbreaks = array_slice($vbreaks, 0, 1000);
2043
2044            // Sort and filter array of page breaks
2045            sort($vbreaks, SORT_NUMERIC);
2046            if ($vbreaks[0] == 0) { // don't use first break if it's 0
2047                array_shift($vbreaks);
2048            }
2049
2050            $record = 0x001a; // Record identifier
2051            $cbrk = count($vbreaks); // Number of page breaks
2052            $length = 2 + 6 * $cbrk; // Bytes to follow
2053
2054            $header = pack('vv', $record, $length);
2055            $data = pack('v', $cbrk);
2056
2057            // Append each page break
2058            foreach ($vbreaks as $vbreak) {
2059                $data .= pack('vvv', $vbreak, 0x0000, 0xffff);
2060            }
2061
2062            $this->append($header . $data);
2063        }
2064    }
2065
2066    /**
2067     * Set the Biff PROTECT record to indicate that the worksheet is protected.
2068     */
2069    private function writeProtect(): void
2070    {
2071        // Exit unless sheet protection has been specified
2072        if (!$this->phpSheet->getProtection()->getSheet()) {
2073            return;
2074        }
2075
2076        $record = 0x0012; // Record identifier
2077        $length = 0x0002; // Bytes to follow
2078
2079        $fLock = 1; // Worksheet is protected
2080
2081        $header = pack('vv', $record, $length);
2082        $data = pack('v', $fLock);
2083
2084        $this->append($header . $data);
2085    }
2086
2087    /**
2088     * Write SCENPROTECT.
2089     */
2090    private function writeScenProtect(): void
2091    {
2092        // Exit if sheet protection is not active
2093        if (!$this->phpSheet->getProtection()->getSheet()) {
2094            return;
2095        }
2096
2097        // Exit if scenarios are not protected
2098        if (!$this->phpSheet->getProtection()->getScenarios()) {
2099            return;
2100        }
2101
2102        $record = 0x00DD; // Record identifier
2103        $length = 0x0002; // Bytes to follow
2104
2105        $header = pack('vv', $record, $length);
2106        $data = pack('v', 1);
2107
2108        $this->append($header . $data);
2109    }
2110
2111    /**
2112     * Write OBJECTPROTECT.
2113     */
2114    private function writeObjectProtect(): void
2115    {
2116        // Exit if sheet protection is not active
2117        if (!$this->phpSheet->getProtection()->getSheet()) {
2118            return;
2119        }
2120
2121        // Exit if objects are not protected
2122        if (!$this->phpSheet->getProtection()->getObjects()) {
2123            return;
2124        }
2125
2126        $record = 0x0063; // Record identifier
2127        $length = 0x0002; // Bytes to follow
2128
2129        $header = pack('vv', $record, $length);
2130        $data = pack('v', 1);
2131
2132        $this->append($header . $data);
2133    }
2134
2135    /**
2136     * Write the worksheet PASSWORD record.
2137     */
2138    private function writePassword(): void
2139    {
2140        // Exit unless sheet protection and password have been specified
2141        if (!$this->phpSheet->getProtection()->getSheet() || !$this->phpSheet->getProtection()->getPassword()) {
2142            return;
2143        }
2144
2145        $record = 0x0013; // Record identifier
2146        $length = 0x0002; // Bytes to follow
2147
2148        $wPassword = hexdec($this->phpSheet->getProtection()->getPassword()); // Encoded password
2149
2150        $header = pack('vv', $record, $length);
2151        $data = pack('v', $wPassword);
2152
2153        $this->append($header . $data);
2154    }
2155
2156    /**
2157     * Insert a 24bit bitmap image in a worksheet.
2158     *
2159     * @param int $row The row we are going to insert the bitmap into
2160     * @param int $col The column we are going to insert the bitmap into
2161     * @param mixed $bitmap The bitmap filename or GD-image resource
2162     * @param int $x the horizontal position (offset) of the image inside the cell
2163     * @param int $y the vertical position (offset) of the image inside the cell
2164     * @param float $scale_x The horizontal scale
2165     * @param float $scale_y The vertical scale
2166     */
2167    public function insertBitmap($row, $col, $bitmap, $x = 0, $y = 0, $scale_x = 1, $scale_y = 1): void
2168    {
2169        $bitmap_array = (is_resource($bitmap) || $bitmap instanceof GdImage
2170            ? $this->processBitmapGd($bitmap)
2171            : $this->processBitmap($bitmap));
2172        [$width, $height, $size, $data] = $bitmap_array;
2173
2174        // Scale the frame of the image.
2175        $width *= $scale_x;
2176        $height *= $scale_y;
2177
2178        // Calculate the vertices of the image and write the OBJ record
2179        $this->positionImage($col, $row, $x, $y, $width, $height);
2180
2181        // Write the IMDATA record to store the bitmap data
2182        $record = 0x007f;
2183        $length = 8 + $size;
2184        $cf = 0x09;
2185        $env = 0x01;
2186        $lcb = $size;
2187
2188        $header = pack('vvvvV', $record, $length, $cf, $env, $lcb);
2189        $this->append($header . $data);
2190    }
2191
2192    /**
2193     * Calculate the vertices that define the position of the image as required by
2194     * the OBJ record.
2195     *
2196     *         +------------+------------+
2197     *         |     A      |      B     |
2198     *   +-----+------------+------------+
2199     *   |     |(x1,y1)     |            |
2200     *   |  1  |(A1)._______|______      |
2201     *   |     |    |              |     |
2202     *   |     |    |              |     |
2203     *   +-----+----|    BITMAP    |-----+
2204     *   |     |    |              |     |
2205     *   |  2  |    |______________.     |
2206     *   |     |            |        (B2)|
2207     *   |     |            |     (x2,y2)|
2208     *   +---- +------------+------------+
2209     *
2210     * Example of a bitmap that covers some of the area from cell A1 to cell B2.
2211     *
2212     * Based on the width and height of the bitmap we need to calculate 8 vars:
2213     *     $col_start, $row_start, $col_end, $row_end, $x1, $y1, $x2, $y2.
2214     * The width and height of the cells are also variable and have to be taken into
2215     * account.
2216     * The values of $col_start and $row_start are passed in from the calling
2217     * function. The values of $col_end and $row_end are calculated by subtracting
2218     * the width and height of the bitmap from the width and height of the
2219     * underlying cells.
2220     * The vertices are expressed as a percentage of the underlying cell width as
2221     * follows (rhs values are in pixels):
2222     *
2223     *       x1 = X / W *1024
2224     *       y1 = Y / H *256
2225     *       x2 = (X-1) / W *1024
2226     *       y2 = (Y-1) / H *256
2227     *
2228     *       Where:  X is distance from the left side of the underlying cell
2229     *               Y is distance from the top of the underlying cell
2230     *               W is the width of the cell
2231     *               H is the height of the cell
2232     * The SDK incorrectly states that the height should be expressed as a
2233     *        percentage of 1024.
2234     *
2235     * @param int $col_start Col containing upper left corner of object
2236     * @param int $row_start Row containing top left corner of object
2237     * @param int $x1 Distance to left side of object
2238     * @param int $y1 Distance to top of object
2239     * @param int $width Width of image frame
2240     * @param int $height Height of image frame
2241     */
2242    public function positionImage($col_start, $row_start, $x1, $y1, $width, $height): void
2243    {
2244        // Initialise end cell to the same as the start cell
2245        $col_end = $col_start; // Col containing lower right corner of object
2246        $row_end = $row_start; // Row containing bottom right corner of object
2247
2248        // Zero the specified offset if greater than the cell dimensions
2249        if ($x1 >= Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_start + 1))) {
2250            $x1 = 0;
2251        }
2252        if ($y1 >= Xls::sizeRow($this->phpSheet, $row_start + 1)) {
2253            $y1 = 0;
2254        }
2255
2256        $width = $width + $x1 - 1;
2257        $height = $height + $y1 - 1;
2258
2259        // Subtract the underlying cell widths to find the end cell of the image
2260        while ($width >= Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_end + 1))) {
2261            $width -= Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_end + 1));
2262            ++$col_end;
2263        }
2264
2265        // Subtract the underlying cell heights to find the end cell of the image
2266        while ($height >= Xls::sizeRow($this->phpSheet, $row_end + 1)) {
2267            $height -= Xls::sizeRow($this->phpSheet, $row_end + 1);
2268            ++$row_end;
2269        }
2270
2271        // Bitmap isn't allowed to start or finish in a hidden cell, i.e. a cell
2272        // with zero eight or width.
2273        //
2274        if (Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_start + 1)) == 0) {
2275            return;
2276        }
2277        if (Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_end + 1)) == 0) {
2278            return;
2279        }
2280        if (Xls::sizeRow($this->phpSheet, $row_start + 1) == 0) {
2281            return;
2282        }
2283        if (Xls::sizeRow($this->phpSheet, $row_end + 1) == 0) {
2284            return;
2285        }
2286
2287        // Convert the pixel values to the percentage value expected by Excel
2288        $x1 = $x1 / Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_start + 1)) * 1024;
2289        $y1 = $y1 / Xls::sizeRow($this->phpSheet, $row_start + 1) * 256;
2290        $x2 = $width / Xls::sizeCol($this->phpSheet, Coordinate::stringFromColumnIndex($col_end + 1)) * 1024; // Distance to right side of object
2291        $y2 = $height / Xls::sizeRow($this->phpSheet, $row_end + 1) * 256; // Distance to bottom of object
2292
2293        $this->writeObjPicture($col_start, $x1, $row_start, $y1, $col_end, $x2, $row_end, $y2);
2294    }
2295
2296    /**
2297     * Store the OBJ record that precedes an IMDATA record. This could be generalise
2298     * to support other Excel objects.
2299     *
2300     * @param int $colL Column containing upper left corner of object
2301     * @param int $dxL Distance from left side of cell
2302     * @param int $rwT Row containing top left corner of object
2303     * @param int $dyT Distance from top of cell
2304     * @param int $colR Column containing lower right corner of object
2305     * @param int $dxR Distance from right of cell
2306     * @param int $rwB Row containing bottom right corner of object
2307     * @param int $dyB Distance from bottom of cell
2308     */
2309    private function writeObjPicture($colL, $dxL, $rwT, $dyT, $colR, $dxR, $rwB, $dyB): void
2310    {
2311        $record = 0x005d; // Record identifier
2312        $length = 0x003c; // Bytes to follow
2313
2314        $cObj = 0x0001; // Count of objects in file (set to 1)
2315        $OT = 0x0008; // Object type. 8 = Picture
2316        $id = 0x0001; // Object ID
2317        $grbit = 0x0614; // Option flags
2318
2319        $cbMacro = 0x0000; // Length of FMLA structure
2320        $Reserved1 = 0x0000; // Reserved
2321        $Reserved2 = 0x0000; // Reserved
2322
2323        $icvBack = 0x09; // Background colour
2324        $icvFore = 0x09; // Foreground colour
2325        $fls = 0x00; // Fill pattern
2326        $fAuto = 0x00; // Automatic fill
2327        $icv = 0x08; // Line colour
2328        $lns = 0xff; // Line style
2329        $lnw = 0x01; // Line weight
2330        $fAutoB = 0x00; // Automatic border
2331        $frs = 0x0000; // Frame style
2332        $cf = 0x0009; // Image format, 9 = bitmap
2333        $Reserved3 = 0x0000; // Reserved
2334        $cbPictFmla = 0x0000; // Length of FMLA structure
2335        $Reserved4 = 0x0000; // Reserved
2336        $grbit2 = 0x0001; // Option flags
2337        $Reserved5 = 0x0000; // Reserved
2338
2339        $header = pack('vv', $record, $length);
2340        $data = pack('V', $cObj);
2341        $data .= pack('v', $OT);
2342        $data .= pack('v', $id);
2343        $data .= pack('v', $grbit);
2344        $data .= pack('v', $colL);
2345        $data .= pack('v', $dxL);
2346        $data .= pack('v', $rwT);
2347        $data .= pack('v', $dyT);
2348        $data .= pack('v', $colR);
2349        $data .= pack('v', $dxR);
2350        $data .= pack('v', $rwB);
2351        $data .= pack('v', $dyB);
2352        $data .= pack('v', $cbMacro);
2353        $data .= pack('V', $Reserved1);
2354        $data .= pack('v', $Reserved2);
2355        $data .= pack('C', $icvBack);
2356        $data .= pack('C', $icvFore);
2357        $data .= pack('C', $fls);
2358        $data .= pack('C', $fAuto);
2359        $data .= pack('C', $icv);
2360        $data .= pack('C', $lns);
2361        $data .= pack('C', $lnw);
2362        $data .= pack('C', $fAutoB);
2363        $data .= pack('v', $frs);
2364        $data .= pack('V', $cf);
2365        $data .= pack('v', $Reserved3);
2366        $data .= pack('v', $cbPictFmla);
2367        $data .= pack('v', $Reserved4);
2368        $data .= pack('v', $grbit2);
2369        $data .= pack('V', $Reserved5);
2370
2371        $this->append($header . $data);
2372    }
2373
2374    /**
2375     * Convert a GD-image into the internal format.
2376     *
2377     * @param GdImage|resource $image The image to process
2378     *
2379     * @return array Array with data and properties of the bitmap
2380     */
2381    public function processBitmapGd($image)
2382    {
2383        $width = imagesx($image);
2384        $height = imagesy($image);
2385
2386        $data = pack('Vvvvv', 0x000c, $width, $height, 0x01, 0x18);
2387        for ($j = $height; --$j;) {
2388            for ($i = 0; $i < $width; ++$i) {
2389                $color = imagecolorsforindex($image, imagecolorat($image, $i, $j));
2390                foreach (['red', 'green', 'blue'] as $key) {
2391                    $color[$key] = $color[$key] + round((255 - $color[$key]) * $color['alpha'] / 127);
2392                }
2393                $data .= chr($color['blue']) . chr($color['green']) . chr($color['red']);
2394            }
2395            if (3 * $width % 4) {
2396                $data .= str_repeat("\x00", 4 - 3 * $width % 4);
2397            }
2398        }
2399
2400        return [$width, $height, strlen($data), $data];
2401    }
2402
2403    /**
2404     * Convert a 24 bit bitmap into the modified internal format used by Windows.
2405     * This is described in BITMAPCOREHEADER and BITMAPCOREINFO structures in the
2406     * MSDN library.
2407     *
2408     * @param string $bitmap The bitmap to process
2409     *
2410     * @return array Array with data and properties of the bitmap
2411     */
2412    public function processBitmap($bitmap)
2413    {
2414        // Open file.
2415        $bmp_fd = @fopen($bitmap, 'rb');
2416        if (!$bmp_fd) {
2417            throw new WriterException("Couldn't import $bitmap");
2418        }
2419
2420        // Slurp the file into a string.
2421        $data = fread($bmp_fd, filesize($bitmap));
2422
2423        // Check that the file is big enough to be a bitmap.
2424        if (strlen($data) <= 0x36) {
2425            throw new WriterException("$bitmap doesn't contain enough data.\n");
2426        }
2427
2428        // The first 2 bytes are used to identify the bitmap.
2429        $identity = unpack('A2ident', $data);
2430        if ($identity['ident'] != 'BM') {
2431            throw new WriterException("$bitmap doesn't appear to be a valid bitmap image.\n");
2432        }
2433
2434        // Remove bitmap data: ID.
2435        $data = substr($data, 2);
2436
2437        // Read and remove the bitmap size. This is more reliable than reading
2438        // the data size at offset 0x22.
2439        //
2440        $size_array = unpack('Vsa', substr($data, 0, 4));
2441        $size = $size_array['sa'];
2442        $data = substr($data, 4);
2443        $size -= 0x36; // Subtract size of bitmap header.
2444        $size += 0x0C; // Add size of BIFF header.
2445
2446        // Remove bitmap data: reserved, offset, header length.
2447        $data = substr($data, 12);
2448
2449        // Read and remove the bitmap width and height. Verify the sizes.
2450        $width_and_height = unpack('V2', substr($data, 0, 8));
2451        $width = $width_and_height[1];
2452        $height = $width_and_height[2];
2453        $data = substr($data, 8);
2454        if ($width > 0xFFFF) {
2455            throw new WriterException("$bitmap: largest image width supported is 65k.\n");
2456        }
2457        if ($height > 0xFFFF) {
2458            throw new WriterException("$bitmap: largest image height supported is 65k.\n");
2459        }
2460
2461        // Read and remove the bitmap planes and bpp data. Verify them.
2462        $planes_and_bitcount = unpack('v2', substr($data, 0, 4));
2463        $data = substr($data, 4);
2464        if ($planes_and_bitcount[2] != 24) { // Bitcount
2465            throw new WriterException("$bitmap isn't a 24bit true color bitmap.\n");
2466        }
2467        if ($planes_and_bitcount[1] != 1) {
2468            throw new WriterException("$bitmap: only 1 plane supported in bitmap image.\n");
2469        }
2470
2471        // Read and remove the bitmap compression. Verify compression.
2472        $compression = unpack('Vcomp', substr($data, 0, 4));
2473        $data = substr($data, 4);
2474
2475        if ($compression['comp'] != 0) {
2476            throw new WriterException("$bitmap: compression not supported in bitmap image.\n");
2477        }
2478
2479        // Remove bitmap data: data size, hres, vres, colours, imp. colours.
2480        $data = substr($data, 20);
2481
2482        // Add the BITMAPCOREHEADER data
2483        $header = pack('Vvvvv', 0x000c, $width, $height, 0x01, 0x18);
2484        $data = $header . $data;
2485
2486        return [$width, $height, $size, $data];
2487    }
2488
2489    /**
2490     * Store the window zoom factor. This should be a reduced fraction but for
2491     * simplicity we will store all fractions with a numerator of 100.
2492     */
2493    private function writeZoom(): void
2494    {
2495        // If scale is 100 we don't need to write a record
2496        if ($this->phpSheet->getSheetView()->getZoomScale() == 100) {
2497            return;
2498        }
2499
2500        $record = 0x00A0; // Record identifier
2501        $length = 0x0004; // Bytes to follow
2502
2503        $header = pack('vv', $record, $length);
2504        $data = pack('vv', $this->phpSheet->getSheetView()->getZoomScale(), 100);
2505        $this->append($header . $data);
2506    }
2507
2508    /**
2509     * Get Escher object.
2510     *
2511     * @return \PhpOffice\PhpSpreadsheet\Shared\Escher
2512     */
2513    public function getEscher()
2514    {
2515        return $this->escher;
2516    }
2517
2518    /**
2519     * Set Escher object.
2520     *
2521     * @param \PhpOffice\PhpSpreadsheet\Shared\Escher $pValue
2522     */
2523    public function setEscher(?\PhpOffice\PhpSpreadsheet\Shared\Escher $pValue = null): void
2524    {
2525        $this->escher = $pValue;
2526    }
2527
2528    /**
2529     * Write MSODRAWING record.
2530     */
2531    private function writeMsoDrawing(): void
2532    {
2533        // write the Escher stream if necessary
2534        if (isset($this->escher)) {
2535            $writer = new Escher($this->escher);
2536            $data = $writer->close();
2537            $spOffsets = $writer->getSpOffsets();
2538            $spTypes = $writer->getSpTypes();
2539            // write the neccesary MSODRAWING, OBJ records
2540
2541            // split the Escher stream
2542            $spOffsets[0] = 0;
2543            $nm = count($spOffsets) - 1; // number of shapes excluding first shape
2544            for ($i = 1; $i <= $nm; ++$i) {
2545                // MSODRAWING record
2546                $record = 0x00EC; // Record identifier
2547
2548                // chunk of Escher stream for one shape
2549                $dataChunk = substr($data, $spOffsets[$i - 1], $spOffsets[$i] - $spOffsets[$i - 1]);
2550
2551                $length = strlen($dataChunk);
2552                $header = pack('vv', $record, $length);
2553
2554                $this->append($header . $dataChunk);
2555
2556                // OBJ record
2557                $record = 0x005D; // record identifier
2558                $objData = '';
2559
2560                // ftCmo
2561                if ($spTypes[$i] == 0x00C9) {
2562                    // Add ftCmo (common object data) subobject
2563                    $objData .=
2564                        pack(
2565                            'vvvvvVVV',
2566                            0x0015, // 0x0015 = ftCmo
2567                            0x0012, // length of ftCmo data
2568                            0x0014, // object type, 0x0014 = filter
2569                            $i, // object id number, Excel seems to use 1-based index, local for the sheet
2570                            0x2101, // option flags, 0x2001 is what OpenOffice.org uses
2571                            0, // reserved
2572                            0, // reserved
2573                            0  // reserved
2574                        );
2575
2576                    // Add ftSbs Scroll bar subobject
2577                    $objData .= pack('vv', 0x00C, 0x0014);
2578                    $objData .= pack('H*', '0000000000000000640001000A00000010000100');
2579                    // Add ftLbsData (List box data) subobject
2580                    $objData .= pack('vv', 0x0013, 0x1FEE);
2581                    $objData .= pack('H*', '00000000010001030000020008005700');
2582                } else {
2583                    // Add ftCmo (common object data) subobject
2584                    $objData .=
2585                        pack(
2586                            'vvvvvVVV',
2587                            0x0015, // 0x0015 = ftCmo
2588                            0x0012, // length of ftCmo data
2589                            0x0008, // object type, 0x0008 = picture
2590                            $i, // object id number, Excel seems to use 1-based index, local for the sheet
2591                            0x6011, // option flags, 0x6011 is what OpenOffice.org uses
2592                            0, // reserved
2593                            0, // reserved
2594                            0  // reserved
2595                        );
2596                }
2597
2598                // ftEnd
2599                $objData .=
2600                    pack(
2601                        'vv',
2602                        0x0000, // 0x0000 = ftEnd
2603                        0x0000  // length of ftEnd data
2604                    );
2605
2606                $length = strlen($objData);
2607                $header = pack('vv', $record, $length);
2608                $this->append($header . $objData);
2609            }
2610        }
2611    }
2612
2613    /**
2614     * Store the DATAVALIDATIONS and DATAVALIDATION records.
2615     */
2616    private function writeDataValidity(): void
2617    {
2618        // Datavalidation collection
2619        $dataValidationCollection = $this->phpSheet->getDataValidationCollection();
2620
2621        // Write data validations?
2622        if (!empty($dataValidationCollection)) {
2623            // DATAVALIDATIONS record
2624            $record = 0x01B2; // Record identifier
2625            $length = 0x0012; // Bytes to follow
2626
2627            $grbit = 0x0000; // Prompt box at cell, no cached validity data at DV records
2628            $horPos = 0x00000000; // Horizontal position of prompt box, if fixed position
2629            $verPos = 0x00000000; // Vertical position of prompt box, if fixed position
2630            $objId = 0xFFFFFFFF; // Object identifier of drop down arrow object, or -1 if not visible
2631
2632            $header = pack('vv', $record, $length);
2633            $data = pack('vVVVV', $grbit, $horPos, $verPos, $objId, count($dataValidationCollection));
2634            $this->append($header . $data);
2635
2636            // DATAVALIDATION records
2637            $record = 0x01BE; // Record identifier
2638
2639            foreach ($dataValidationCollection as $cellCoordinate => $dataValidation) {
2640                // options
2641                $options = 0x00000000;
2642
2643                // data type
2644                $type = CellDataValidation::type($dataValidation);
2645
2646                $options |= $type << 0;
2647
2648                // error style
2649                $errorStyle = CellDataValidation::errorStyle($dataValidation);
2650
2651                $options |= $errorStyle << 4;
2652
2653                // explicit formula?
2654                if ($type == 0x03 && preg_match('/^\".*\"$/', $dataValidation->getFormula1())) {
2655                    $options |= 0x01 << 7;
2656                }
2657
2658                // empty cells allowed
2659                $options |= $dataValidation->getAllowBlank() << 8;
2660
2661                // show drop down
2662                $options |= (!$dataValidation->getShowDropDown()) << 9;
2663
2664                // show input message
2665                $options |= $dataValidation->getShowInputMessage() << 18;
2666
2667                // show error message
2668                $options |= $dataValidation->getShowErrorMessage() << 19;
2669
2670                // condition operator
2671                $operator = CellDataValidation::operator($dataValidation);
2672
2673                $options |= $operator << 20;
2674
2675                $data = pack('V', $options);
2676
2677                // prompt title
2678                $promptTitle = $dataValidation->getPromptTitle() !== '' ?
2679                    $dataValidation->getPromptTitle() : chr(0);
2680                $data .= StringHelper::UTF8toBIFF8UnicodeLong($promptTitle);
2681
2682                // error title
2683                $errorTitle = $dataValidation->getErrorTitle() !== '' ?
2684                    $dataValidation->getErrorTitle() : chr(0);
2685                $data .= StringHelper::UTF8toBIFF8UnicodeLong($errorTitle);
2686
2687                // prompt text
2688                $prompt = $dataValidation->getPrompt() !== '' ?
2689                    $dataValidation->getPrompt() : chr(0);
2690                $data .= StringHelper::UTF8toBIFF8UnicodeLong($prompt);
2691
2692                // error text
2693                $error = $dataValidation->getError() !== '' ?
2694                    $dataValidation->getError() : chr(0);
2695                $data .= StringHelper::UTF8toBIFF8UnicodeLong($error);
2696
2697                // formula 1
2698                try {
2699                    $formula1 = $dataValidation->getFormula1();
2700                    if ($type == 0x03) { // list type
2701                        $formula1 = str_replace(',', chr(0), $formula1);
2702                    }
2703                    $this->parser->parse($formula1);
2704                    $formula1 = $this->parser->toReversePolish();
2705                    $sz1 = strlen($formula1);
2706                } catch (PhpSpreadsheetException $e) {
2707                    $sz1 = 0;
2708                    $formula1 = '';
2709                }
2710                $data .= pack('vv', $sz1, 0x0000);
2711                $data .= $formula1;
2712
2713                // formula 2
2714                try {
2715                    $formula2 = $dataValidation->getFormula2();
2716                    if ($formula2 === '') {
2717                        throw new WriterException('No formula2');
2718                    }
2719                    $this->parser->parse($formula2);
2720                    $formula2 = $this->parser->toReversePolish();
2721                    $sz2 = strlen($formula2);
2722                } catch (PhpSpreadsheetException $e) {
2723                    $sz2 = 0;
2724                    $formula2 = '';
2725                }
2726                $data .= pack('vv', $sz2, 0x0000);
2727                $data .= $formula2;
2728
2729                // cell range address list
2730                $data .= pack('v', 0x0001);
2731                $data .= $this->writeBIFF8CellRangeAddressFixed($cellCoordinate);
2732
2733                $length = strlen($data);
2734                $header = pack('vv', $record, $length);
2735
2736                $this->append($header . $data);
2737            }
2738        }
2739    }
2740
2741    /**
2742     * Write PLV Record.
2743     */
2744    private function writePageLayoutView(): void
2745    {
2746        $record = 0x088B; // Record identifier
2747        $length = 0x0010; // Bytes to follow
2748
2749        $rt = 0x088B; // 2
2750        $grbitFrt = 0x0000; // 2
2751        $reserved = 0x0000000000000000; // 8
2752        $wScalvePLV = $this->phpSheet->getSheetView()->getZoomScale(); // 2
2753
2754        // The options flags that comprise $grbit
2755        if ($this->phpSheet->getSheetView()->getView() == SheetView::SHEETVIEW_PAGE_LAYOUT) {
2756            $fPageLayoutView = 1;
2757        } else {
2758            $fPageLayoutView = 0;
2759        }
2760        $fRulerVisible = 0;
2761        $fWhitespaceHidden = 0;
2762
2763        $grbit = $fPageLayoutView; // 2
2764        $grbit |= $fRulerVisible << 1;
2765        $grbit |= $fWhitespaceHidden << 3;
2766
2767        $header = pack('vv', $record, $length);
2768        $data = pack('vvVVvv', $rt, $grbitFrt, 0x00000000, 0x00000000, $wScalvePLV, $grbit);
2769        $this->append($header . $data);
2770    }
2771
2772    /**
2773     * Write CFRule Record.
2774     */
2775    private function writeCFRule(Conditional $conditional): void
2776    {
2777        $record = 0x01B1; // Record identifier
2778        $type = null;  //  Type of the CF
2779        $operatorType = null;   //  Comparison operator
2780
2781        if ($conditional->getConditionType() == Conditional::CONDITION_EXPRESSION) {
2782            $type = 0x02;
2783            $operatorType = 0x00;
2784        } elseif ($conditional->getConditionType() == Conditional::CONDITION_CELLIS) {
2785            $type = 0x01;
2786
2787            switch ($conditional->getOperatorType()) {
2788                case Conditional::OPERATOR_NONE:
2789                    $operatorType = 0x00;
2790
2791                    break;
2792                case Conditional::OPERATOR_EQUAL:
2793                    $operatorType = 0x03;
2794
2795                    break;
2796                case Conditional::OPERATOR_GREATERTHAN:
2797                    $operatorType = 0x05;
2798
2799                    break;
2800                case Conditional::OPERATOR_GREATERTHANOREQUAL:
2801                    $operatorType = 0x07;
2802
2803                    break;
2804                case Conditional::OPERATOR_LESSTHAN:
2805                    $operatorType = 0x06;
2806
2807                    break;
2808                case Conditional::OPERATOR_LESSTHANOREQUAL:
2809                    $operatorType = 0x08;
2810
2811                    break;
2812                case Conditional::OPERATOR_NOTEQUAL:
2813                    $operatorType = 0x04;
2814
2815                    break;
2816                case Conditional::OPERATOR_BETWEEN:
2817                    $operatorType = 0x01;
2818
2819                    break;
2820                // not OPERATOR_NOTBETWEEN 0x02
2821            }
2822        }
2823
2824        // $szValue1 : size of the formula data for first value or formula
2825        // $szValue2 : size of the formula data for second value or formula
2826        $arrConditions = $conditional->getConditions();
2827        $numConditions = count($arrConditions);
2828        if ($numConditions == 1) {
2829            $szValue1 = ($arrConditions[0] <= 65535 ? 3 : 0x0000);
2830            $szValue2 = 0x0000;
2831            $operand1 = pack('Cv', 0x1E, $arrConditions[0]);
2832            $operand2 = null;
2833        } elseif ($numConditions == 2 && ($conditional->getOperatorType() == Conditional::OPERATOR_BETWEEN)) {
2834            $szValue1 = ($arrConditions[0] <= 65535 ? 3 : 0x0000);
2835            $szValue2 = ($arrConditions[1] <= 65535 ? 3 : 0x0000);
2836            $operand1 = pack('Cv', 0x1E, $arrConditions[0]);
2837            $operand2 = pack('Cv', 0x1E, $arrConditions[1]);
2838        } else {
2839            $szValue1 = 0x0000;
2840            $szValue2 = 0x0000;
2841            $operand1 = null;
2842            $operand2 = null;
2843        }
2844
2845        // $flags : Option flags
2846        // Alignment
2847        $bAlignHz = ($conditional->getStyle()->getAlignment()->getHorizontal() === null ? 1 : 0);
2848        $bAlignVt = ($conditional->getStyle()->getAlignment()->getVertical() === null ? 1 : 0);
2849        $bAlignWrapTx = ($conditional->getStyle()->getAlignment()->getWrapText() === false ? 1 : 0);
2850        $bTxRotation = ($conditional->getStyle()->getAlignment()->getTextRotation() === null ? 1 : 0);
2851        $bIndent = ($conditional->getStyle()->getAlignment()->getIndent() === 0 ? 1 : 0);
2852        $bShrinkToFit = ($conditional->getStyle()->getAlignment()->getShrinkToFit() === false ? 1 : 0);
2853        if ($bAlignHz == 0 || $bAlignVt == 0 || $bAlignWrapTx == 0 || $bTxRotation == 0 || $bIndent == 0 || $bShrinkToFit == 0) {
2854            $bFormatAlign = 1;
2855        } else {
2856            $bFormatAlign = 0;
2857        }
2858        // Protection
2859        $bProtLocked = ($conditional->getStyle()->getProtection()->getLocked() == null ? 1 : 0);
2860        $bProtHidden = ($conditional->getStyle()->getProtection()->getHidden() == null ? 1 : 0);
2861        if ($bProtLocked == 0 || $bProtHidden == 0) {
2862            $bFormatProt = 1;
2863        } else {
2864            $bFormatProt = 0;
2865        }
2866        // Border
2867        $bBorderLeft = ($conditional->getStyle()->getBorders()->getLeft()->getColor()->getARGB() == Color::COLOR_BLACK
2868        && $conditional->getStyle()->getBorders()->getLeft()->getBorderStyle() == Border::BORDER_NONE ? 1 : 0);
2869        $bBorderRight = ($conditional->getStyle()->getBorders()->getRight()->getColor()->getARGB() == Color::COLOR_BLACK
2870        && $conditional->getStyle()->getBorders()->getRight()->getBorderStyle() == Border::BORDER_NONE ? 1 : 0);
2871        $bBorderTop = ($conditional->getStyle()->getBorders()->getTop()->getColor()->getARGB() == Color::COLOR_BLACK
2872        && $conditional->getStyle()->getBorders()->getTop()->getBorderStyle() == Border::BORDER_NONE ? 1 : 0);
2873        $bBorderBottom = ($conditional->getStyle()->getBorders()->getBottom()->getColor()->getARGB() == Color::COLOR_BLACK
2874        && $conditional->getStyle()->getBorders()->getBottom()->getBorderStyle() == Border::BORDER_NONE ? 1 : 0);
2875        if ($bBorderLeft == 0 || $bBorderRight == 0 || $bBorderTop == 0 || $bBorderBottom == 0) {
2876            $bFormatBorder = 1;
2877        } else {
2878            $bFormatBorder = 0;
2879        }
2880        // Pattern
2881        $bFillStyle = ($conditional->getStyle()->getFill()->getFillType() === null ? 0 : 1);
2882        $bFillColor = ($conditional->getStyle()->getFill()->getStartColor()->getARGB() == null ? 0 : 1);
2883        $bFillColorBg = ($conditional->getStyle()->getFill()->getEndColor()->getARGB() == null ? 0 : 1);
2884        if ($bFillStyle == 0 || $bFillColor == 0 || $bFillColorBg == 0) {
2885            $bFormatFill = 1;
2886        } else {
2887            $bFormatFill = 0;
2888        }
2889        // Font
2890        if (
2891            $conditional->getStyle()->getFont()->getName() !== null
2892            || $conditional->getStyle()->getFont()->getSize() !== null
2893            || $conditional->getStyle()->getFont()->getBold() !== null
2894            || $conditional->getStyle()->getFont()->getItalic() !== null
2895            || $conditional->getStyle()->getFont()->getSuperscript() !== null
2896            || $conditional->getStyle()->getFont()->getSubscript() !== null
2897            || $conditional->getStyle()->getFont()->getUnderline() !== null
2898            || $conditional->getStyle()->getFont()->getStrikethrough() !== null
2899            || $conditional->getStyle()->getFont()->getColor()->getARGB() != null
2900        ) {
2901            $bFormatFont = 1;
2902        } else {
2903            $bFormatFont = 0;
2904        }
2905        // Alignment
2906        $flags = 0;
2907        $flags |= (1 == $bAlignHz ? 0x00000001 : 0);
2908        $flags |= (1 == $bAlignVt ? 0x00000002 : 0);
2909        $flags |= (1 == $bAlignWrapTx ? 0x00000004 : 0);
2910        $flags |= (1 == $bTxRotation ? 0x00000008 : 0);
2911        // Justify last line flag
2912        $flags |= (1 == 1 ? 0x00000010 : 0);
2913        $flags |= (1 == $bIndent ? 0x00000020 : 0);
2914        $flags |= (1 == $bShrinkToFit ? 0x00000040 : 0);
2915        // Default
2916        $flags |= (1 == 1 ? 0x00000080 : 0);
2917        // Protection
2918        $flags |= (1 == $bProtLocked ? 0x00000100 : 0);
2919        $flags |= (1 == $bProtHidden ? 0x00000200 : 0);
2920        // Border
2921        $flags |= (1 == $bBorderLeft ? 0x00000400 : 0);
2922        $flags |= (1 == $bBorderRight ? 0x00000800 : 0);
2923        $flags |= (1 == $bBorderTop ? 0x00001000 : 0);
2924        $flags |= (1 == $bBorderBottom ? 0x00002000 : 0);
2925        $flags |= (1 == 1 ? 0x00004000 : 0); // Top left to Bottom right border
2926        $flags |= (1 == 1 ? 0x00008000 : 0); // Bottom left to Top right border
2927        // Pattern
2928        $flags |= (1 == $bFillStyle ? 0x00010000 : 0);
2929        $flags |= (1 == $bFillColor ? 0x00020000 : 0);
2930        $flags |= (1 == $bFillColorBg ? 0x00040000 : 0);
2931        $flags |= (1 == 1 ? 0x00380000 : 0);
2932        // Font
2933        $flags |= (1 == $bFormatFont ? 0x04000000 : 0);
2934        // Alignment:
2935        $flags |= (1 == $bFormatAlign ? 0x08000000 : 0);
2936        // Border
2937        $flags |= (1 == $bFormatBorder ? 0x10000000 : 0);
2938        // Pattern
2939        $flags |= (1 == $bFormatFill ? 0x20000000 : 0);
2940        // Protection
2941        $flags |= (1 == $bFormatProt ? 0x40000000 : 0);
2942        // Text direction
2943        $flags |= (1 == 0 ? 0x80000000 : 0);
2944
2945        $dataBlockFont = null;
2946        $dataBlockAlign = null;
2947        $dataBlockBorder = null;
2948        $dataBlockFill = null;
2949
2950        // Data Blocks
2951        if ($bFormatFont == 1) {
2952            // Font Name
2953            if ($conditional->getStyle()->getFont()->getName() === null) {
2954                $dataBlockFont = pack('VVVVVVVV', 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000);
2955                $dataBlockFont .= pack('VVVVVVVV', 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000);
2956            } else {
2957                $dataBlockFont = StringHelper::UTF8toBIFF8UnicodeLong($conditional->getStyle()->getFont()->getName());
2958            }
2959            // Font Size
2960            if ($conditional->getStyle()->getFont()->getSize() === null) {
2961                $dataBlockFont .= pack('V', 20 * 11);
2962            } else {
2963                $dataBlockFont .= pack('V', 20 * $conditional->getStyle()->getFont()->getSize());
2964            }
2965            // Font Options
2966            $dataBlockFont .= pack('V', 0);
2967            // Font weight
2968            if ($conditional->getStyle()->getFont()->getBold() === true) {
2969                $dataBlockFont .= pack('v', 0x02BC);
2970            } else {
2971                $dataBlockFont .= pack('v', 0x0190);
2972            }
2973            // Escapement type
2974            if ($conditional->getStyle()->getFont()->getSubscript() === true) {
2975                $dataBlockFont .= pack('v', 0x02);
2976                $fontEscapement = 0;
2977            } elseif ($conditional->getStyle()->getFont()->getSuperscript() === true) {
2978                $dataBlockFont .= pack('v', 0x01);
2979                $fontEscapement = 0;
2980            } else {
2981                $dataBlockFont .= pack('v', 0x00);
2982                $fontEscapement = 1;
2983            }
2984            // Underline type
2985            switch ($conditional->getStyle()->getFont()->getUnderline()) {
2986                case \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_NONE:
2987                    $dataBlockFont .= pack('C', 0x00);
2988                    $fontUnderline = 0;
2989
2990                    break;
2991                case \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLE:
2992                    $dataBlockFont .= pack('C', 0x02);
2993                    $fontUnderline = 0;
2994
2995                    break;
2996                case \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_DOUBLEACCOUNTING:
2997                    $dataBlockFont .= pack('C', 0x22);
2998                    $fontUnderline = 0;
2999
3000                    break;
3001                case \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE:
3002                    $dataBlockFont .= pack('C', 0x01);
3003                    $fontUnderline = 0;
3004
3005                    break;
3006                case \PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLEACCOUNTING:
3007                    $dataBlockFont .= pack('C', 0x21);
3008                    $fontUnderline = 0;
3009
3010                    break;
3011                default:
3012                    $dataBlockFont .= pack('C', 0x00);
3013                    $fontUnderline = 1;
3014
3015                    break;
3016            }
3017            // Not used (3)
3018            $dataBlockFont .= pack('vC', 0x0000, 0x00);
3019            // Font color index
3020            $colorIdx = Style\ColorMap::lookup($conditional->getStyle()->getFont()->getColor(), 0x00);
3021
3022            $dataBlockFont .= pack('V', $colorIdx);
3023            // Not used (4)
3024            $dataBlockFont .= pack('V', 0x00000000);
3025            // Options flags for modified font attributes
3026            $optionsFlags = 0;
3027            $optionsFlagsBold = ($conditional->getStyle()->getFont()->getBold() === null ? 1 : 0);
3028            $optionsFlags |= (1 == $optionsFlagsBold ? 0x00000002 : 0);
3029            $optionsFlags |= (1 == 1 ? 0x00000008 : 0);
3030            $optionsFlags |= (1 == 1 ? 0x00000010 : 0);
3031            $optionsFlags |= (1 == 0 ? 0x00000020 : 0);
3032            $optionsFlags |= (1 == 1 ? 0x00000080 : 0);
3033            $dataBlockFont .= pack('V', $optionsFlags);
3034            // Escapement type
3035            $dataBlockFont .= pack('V', $fontEscapement);
3036            // Underline type
3037            $dataBlockFont .= pack('V', $fontUnderline);
3038            // Always
3039            $dataBlockFont .= pack('V', 0x00000000);
3040            // Always
3041            $dataBlockFont .= pack('V', 0x00000000);
3042            // Not used (8)
3043            $dataBlockFont .= pack('VV', 0x00000000, 0x00000000);
3044            // Always
3045            $dataBlockFont .= pack('v', 0x0001);
3046        }
3047        if ($bFormatAlign === 1) {
3048            // Alignment and text break
3049            $blockAlign = Style\CellAlignment::horizontal($conditional->getStyle()->getAlignment());
3050            $blockAlign |= Style\CellAlignment::wrap($conditional->getStyle()->getAlignment()) << 3;
3051            $blockAlign |= Style\CellAlignment::vertical($conditional->getStyle()->getAlignment()) << 4;
3052            $blockAlign |= 0 << 7;
3053
3054            // Text rotation angle
3055            $blockRotation = $conditional->getStyle()->getAlignment()->getTextRotation();
3056
3057            // Indentation
3058            $blockIndent = $conditional->getStyle()->getAlignment()->getIndent();
3059            if ($conditional->getStyle()->getAlignment()->getShrinkToFit() === true) {
3060                $blockIndent |= 1 << 4;
3061            } else {
3062                $blockIndent |= 0 << 4;
3063            }
3064            $blockIndent |= 0 << 6;
3065
3066            // Relative indentation
3067            $blockIndentRelative = 255;
3068
3069            $dataBlockAlign = pack('CCvvv', $blockAlign, $blockRotation, $blockIndent, $blockIndentRelative, 0x0000);
3070        }
3071        if ($bFormatBorder === 1) {
3072            $blockLineStyle = Style\CellBorder::style($conditional->getStyle()->getBorders()->getLeft());
3073            $blockLineStyle |= Style\CellBorder::style($conditional->getStyle()->getBorders()->getRight()) << 4;
3074            $blockLineStyle |= Style\CellBorder::style($conditional->getStyle()->getBorders()->getTop()) << 8;
3075            $blockLineStyle |= Style\CellBorder::style($conditional->getStyle()->getBorders()->getBottom()) << 12;
3076
3077            // TODO writeCFRule() => $blockLineStyle => Index Color for left line
3078            // TODO writeCFRule() => $blockLineStyle => Index Color for right line
3079            // TODO writeCFRule() => $blockLineStyle => Top-left to bottom-right on/off
3080            // TODO writeCFRule() => $blockLineStyle => Bottom-left to top-right on/off
3081            $blockColor = 0;
3082            // TODO writeCFRule() => $blockColor => Index Color for top line
3083            // TODO writeCFRule() => $blockColor => Index Color for bottom line
3084            // TODO writeCFRule() => $blockColor => Index Color for diagonal line
3085            $blockColor |= Style\CellBorder::style($conditional->getStyle()->getBorders()->getDiagonal()) << 21;
3086            $dataBlockBorder = pack('vv', $blockLineStyle, $blockColor);
3087        }
3088        if ($bFormatFill === 1) {
3089            // Fill Pattern Style
3090            $blockFillPatternStyle = Style\CellFill::style($conditional->getStyle()->getFill());
3091            // Background Color
3092            $colorIdxBg = Style\ColorMap::lookup($conditional->getStyle()->getFill()->getStartColor(), 0x41);
3093            // Foreground Color
3094            $colorIdxFg = Style\ColorMap::lookup($conditional->getStyle()->getFill()->getEndColor(), 0x40);
3095
3096            $dataBlockFill = pack('v', $blockFillPatternStyle);
3097            $dataBlockFill .= pack('v', $colorIdxFg | ($colorIdxBg << 7));
3098        }
3099
3100        $data = pack('CCvvVv', $type, $operatorType, $szValue1, $szValue2, $flags, 0x0000);
3101        if ($bFormatFont === 1) { // Block Formatting : OK
3102            $data .= $dataBlockFont;
3103        }
3104        if ($bFormatAlign === 1) {
3105            $data .= $dataBlockAlign;
3106        }
3107        if ($bFormatBorder === 1) {
3108            $data .= $dataBlockBorder;
3109        }
3110        if ($bFormatFill === 1) { // Block Formatting : OK
3111            $data .= $dataBlockFill;
3112        }
3113        if ($bFormatProt == 1) {
3114            $data .= $this->getDataBlockProtection($conditional);
3115        }
3116        if ($operand1 !== null) {
3117            $data .= $operand1;
3118        }
3119        if ($operand2 !== null) {
3120            $data .= $operand2;
3121        }
3122        $header = pack('vv', $record, strlen($data));
3123        $this->append($header . $data);
3124    }
3125
3126    /**
3127     * Write CFHeader record.
3128     */
3129    private function writeCFHeader(): void
3130    {
3131        $record = 0x01B0; // Record identifier
3132        $length = 0x0016; // Bytes to follow
3133
3134        $numColumnMin = null;
3135        $numColumnMax = null;
3136        $numRowMin = null;
3137        $numRowMax = null;
3138        $arrConditional = [];
3139        foreach ($this->phpSheet->getConditionalStylesCollection() as $cellCoordinate => $conditionalStyles) {
3140            foreach ($conditionalStyles as $conditional) {
3141                if (
3142                    $conditional->getConditionType() == Conditional::CONDITION_EXPRESSION ||
3143                    $conditional->getConditionType() == Conditional::CONDITION_CELLIS
3144                ) {
3145                    if (!in_array($conditional->getHashCode(), $arrConditional)) {
3146                        $arrConditional[] = $conditional->getHashCode();
3147                    }
3148                    // Cells
3149                    $rangeCoordinates = Coordinate::rangeBoundaries($cellCoordinate);
3150                    if ($numColumnMin === null || ($numColumnMin > $rangeCoordinates[0][0])) {
3151                        $numColumnMin = $rangeCoordinates[0][0];
3152                    }
3153                    if ($numColumnMax === null || ($numColumnMax < $rangeCoordinates[1][0])) {
3154                        $numColumnMax = $rangeCoordinates[1][0];
3155                    }
3156                    if ($numRowMin === null || ($numRowMin > $rangeCoordinates[0][1])) {
3157                        $numRowMin = (int) $rangeCoordinates[0][1];
3158                    }
3159                    if ($numRowMax === null || ($numRowMax < $rangeCoordinates[1][1])) {
3160                        $numRowMax = (int) $rangeCoordinates[1][1];
3161                    }
3162                }
3163            }
3164        }
3165        $needRedraw = 1;
3166        $cellRange = pack('vvvv', $numRowMin - 1, $numRowMax - 1, $numColumnMin - 1, $numColumnMax - 1);
3167
3168        $header = pack('vv', $record, $length);
3169        $data = pack('vv', count($arrConditional), $needRedraw);
3170        $data .= $cellRange;
3171        $data .= pack('v', 0x0001);
3172        $data .= $cellRange;
3173        $this->append($header . $data);
3174    }
3175
3176    private function getDataBlockProtection(Conditional $conditional): int
3177    {
3178        $dataBlockProtection = 0;
3179        if ($conditional->getStyle()->getProtection()->getLocked() == Protection::PROTECTION_PROTECTED) {
3180            $dataBlockProtection = 1;
3181        }
3182        if ($conditional->getStyle()->getProtection()->getHidden() == Protection::PROTECTION_PROTECTED) {
3183            $dataBlockProtection = 1 << 1;
3184        }
3185
3186        return $dataBlockProtection;
3187    }
3188}
3189