Vendor/phpspreadsheet/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xls/Worksheet.php
private function writeFormula($row, $col, $formula, $xfIndex, $calculatedValue)
    {
        $record = 0x0006; // Record identifier

        // Initialize possible additional value for STRING record that should be written after the FORMULA record?
        $stringValue = null;

        // calculated value
        if (isset($calculatedValue)) {
            // Since we can't yet get the data type of the calculated value,
            // we use best effort to determine data type
            if (is_bool($calculatedValue)) {
                // Boolean value
                $num = pack('CCCvCv', 0x01, 0x00, (int) $calculatedValue, 0x00, 0x00, 0xFFFF);
            } elseif (is_int($calculatedValue) || is_float($calculatedValue)) {
                // Numeric value
                $num = pack('d', $calculatedValue);
            } elseif (is_string($calculatedValue)) {
                $errorCodes = DataType::getErrorCodes();
                if (isset($errorCodes[$calculatedValue])) {
                    // Error value
                    $num = pack('CCCvCv', 0x02, 0x00, self::mapErrorCode($calculatedValue), 0x00, 0x00, 0xFFFF);
                } elseif ($calculatedValue === '') {
                    // Empty string (and BIFF8)
                    $num = pack('CCCvCv', 0x03, 0x00, 0x00, 0x00, 0x00, 0xFFFF);
                } else {
                    // Non-empty string value (or empty string BIFF5)
                    $stringValue = $calculatedValue;
                    $num = pack('CCCvCv', 0x00, 0x00, 0x00, 0x00, 0x00, 0xFFFF);
                }
            } else {
                // We are really not supposed to reach here
                $num = pack('d', 0x00);
            }
        } else {
            $num = pack('d', 0x00);
        }

        $grbit = 0x03; // Option flags
        $unknown = 0x0000; // Must be zero

        // Strip the '=' or '@' sign at the beginning of the formula string
        if ($formula[0] == '=') {
            $formula = substr($formula, 1);
        } else {
            // Error handling
            $this->writeString($row, $col, 'Unrecognised character for formula');

            return -1;
        }

        // Parse the formula using the parser in Parser.php
        try {
            $error = $this->parser->parse($formula);
            $formula = $this->parser->toReversePolish();

            $formlen = strlen($formula); // Length of the binary string
            $length = 0x16 + $formlen; // Length of the record data

            $header = pack('vv', $record, $length);

            $data = pack('vvv', $row, $col, $xfIndex)
                        . $num
                        . pack('vVv', $grbit, $unknown, $formlen);
            $this->append($header . $data . $formula);

            // Append also a STRING record if necessary
            if ($stringValue !== null) {
                $this->writeStringRecord($stringValue);
            }

            return 0;
        } catch (PhpSpreadsheetException $e) {
            // do nothing
        }
    }
private function writeFormula($row, $col, $formula, $xfIndex, $calculatedValue)
    {
        $record = 0x0006; // Record identifier

        // Initialize possible additional value for STRING record that should be written after the FORMULA record?
        $stringValue = null;

        // calculated value
        if (isset($calculatedValue)) {
            // Since we can't yet get the data type of the calculated value,
            // we use best effort to determine data type
            if (is_bool($calculatedValue)) {
                // Boolean value
                $num = pack('CCCvCv', 0x01, 0x00, (int) $calculatedValue, 0x00, 0x00, 0xFFFF);
            } elseif (is_int($calculatedValue) || is_float($calculatedValue)) {
                // Numeric value
                $num = pack('d', $calculatedValue);
            } elseif (is_string($calculatedValue)) {
                $errorCodes = DataType::getErrorCodes();
                if (isset($errorCodes[$calculatedValue])) {
                    // Error value
                    $num = pack('CCCvCv', 0x02, 0x00, self::mapErrorCode($calculatedValue), 0x00, 0x00, 0xFFFF);
                } elseif ($calculatedValue === '') {
                    // Empty string (and BIFF8)
                    $num = pack('CCCvCv', 0x03, 0x00, 0x00, 0x00, 0x00, 0xFFFF);
                } else {
                    // Non-empty string value (or empty string BIFF5)
                    $stringValue = $calculatedValue;
                    $num = pack('CCCvCv', 0x00, 0x00, 0x00, 0x00, 0x00, 0xFFFF);
                }
            } else {
                // We are really not supposed to reach here
                $num = pack('d', 0x00);
            }
        } else {
            $num = pack('d', 0x00);
        }

        $grbit = 0x03; // Option flags
        $unknown = 0x0000; // Must be zero

        // Strip the '=' or '@' sign at the beginning of the formula string
        if ($formula[0] == '=') {
            $formula = substr($formula, 1);
        } else {
            // Error handling
            $this->writeString($row, $col, 'Unrecognised character for formula');

            return -1;
        }

        // Parse the formula using the parser in Parser.php
        try {
            $error = $this->parser->parse($formula);
            $formula = $this->parser->toReversePolish();

            $formlen = strlen($formula); // Length of the binary string
            $length = 0x16 + $formlen; // Length of the record data

            $header = pack('vv', $record, $length);

            $data = pack('vvv', $row, $col, $xfIndex)
                        . $num
                        . pack('vVv', $grbit, $unknown, $formlen);
            $this->append($header . $data . $formula);

            // Append also a STRING record if necessary
            if ($stringValue !== null) {
                $this->writeStringRecord($stringValue);
            }

            return 0;
        } catch (PhpSpreadsheetException $e) {
            // do nothing
        }
    }
private function writeFormula ( $ row , $ col , $ formula , $ xfIndex , $ calculatedValue ) { $ record = 0x0006 ; // Record identifier // Initialize possible additional value for STRING record that should be written after the FORMULA record? $ stringValue = null ; // calculated value if ( isset ( $ calculatedValue ) ) { // Since we can't yet get the data type of the calculated value, // we use best effort to determine data type if ( is_bool ( $ calculatedValue ) ) { // Boolean value $ num = pack ( 'CCCvCv' , 0x01 , 0x00 , ( int ) $ calculatedValue , 0x00 , 0x00 , 0xFFFF ) ; } elseif ( is_int ( $ calculatedValue ) || is_float ( $ calculatedValue ) ) { // Numeric value $ num = pack ( 'd' , $ calculatedValue ) ; } elseif ( is_string ( $ calculatedValue ) ) { $ errorCodes = DataType :: getErrorCodes ( ) ; if ( isset ( $ errorCodes [ $ calculatedValue ] ) ) { // Error value $ num = pack ( 'CCCvCv' , 0x02 , 0x00 , self :: mapErrorCode ( $ calculatedValue ) , 0x00 , 0x00 , 0xFFFF ) ; } elseif ( $ calculatedValue === '' ) { // Empty string (and BIFF8) $ num = pack ( 'CCCvCv' , 0x03 , 0x00 , 0x00 , 0x00 , 0x00 , 0xFFFF ) ; } else { // Non-empty string value (or empty string BIFF5) $ stringValue = $ calculatedValue ; $ num = pack ( 'CCCvCv' , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xFFFF ) ; } } else { // We are really not supposed to reach here $ num = pack ( 'd' , 0x00 ) ; } } else { $ num = pack ( 'd' , 0x00 ) ; } $ grbit = 0x03 ; // Option flags $ unknown = 0x0000 ; // Must be zero // Strip the '=' or '@' sign at the beginning of the formula string if ( $ formula [ 0 ] == '=' ) { $ formula = substr ( $ formula , 1 ) ; } else { // Error handling $ this -> writeString ( $ row , $ col , 'Unrecognised character for formula' ) ; return - 1 ; } // Parse the formula using the parser in Parser.php try { $ error = $ this -> parser -> parse ( $ formula ) ; $ formula = $ this -> parser -> toReversePolish ( ) ; $ formlen = strlen ( $ formula ) ; // Length of the binary string $ length = 0x16 + $ formlen ; // Length of the record data $ header = pack ( 'vv' , $ record , $ length ) ; $ data = pack ( 'vvv' , $ row , $ col , $ xfIndex ) . $ num . pack ( 'vVv' , $ grbit , $ unknown , $ formlen ) ; $ this -> append ( $ header . $ data . $ formula ) ; // Append also a STRING record if necessary if ( $ stringValue !== null ) { $ this -> writeStringRecord ( $ stringValue ) ; } return 0 ; } catch ( PhpSpreadsheetException $ e ) { // do nothing } }
Write a formula to the specified row and column (zero indexed).
The textual representation of the formula is passed to the parser in
Parser.php which returns a packed binary string.

Returns  0 : normal termination
-1 : formula errors (bad formula)
-2 : row or column out of range

@param int $row Zero indexed row
@param int $col Zero indexed column
@param string $formula The formula text string
@param mixed $xfIndex The XF format index
@param mixed $calculatedValue Calculated value

@return int
Write a formula to the specified row and column ( zero indexed ) . The textual representation of the formula is passed to the parser in Parser . php which returns a packed binary string .