henkmahendra / php-excel-reader

Automatically exported from code.google.com/p/php-excel-reader
0 stars 0 forks source link

Boolean and String Formulas are not supported #4

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Put a formula in spreadsheet with boolean or string result

What is the expected output? What do you see instead?
Desired output is the result of the formula in the cells array. Instead, it
is as if the cell is empty.

What version of the product are you using? On what operating system?
NA

Please provide any additional information below.
I have a private version of the code in which I've made the necessary
changes to support boolean (see mmpbool) and string (see mmptext) strings.
Please note, this is based on an older version of the code when the
constants were not all uppercase. In any case, here is the code in case you
would like to incorporate it into the next release:

                case Spreadsheet_Excel_Reader_Type_FORMULA:
                case Spreadsheet_Excel_Reader_Type_FORMULA2:
                    $row    = ord($this->data[$spos]) |
ord($this->data[$spos+1])<<8;
                    $column = ord($this->data[$spos+2]) |
ord($this->data[$spos+3])<<8;
                    if ((ord($this->data[$spos+6])==0) &&
(ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
                        //String formula. Result follows in a STRING record
//mmptext begin                        
                        echo "FORMULA $row $column Formula with a
string<br>\n";
                        $SAVErow = $row; $SAVEcolumn = $column;
                        // the SAVE values are used in the STRING record
//mmptext end                        
                    } elseif ((ord($this->data[$spos+6])==1) &&
(ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
                        //Boolean formula. Result is in +2; 0=false,1=true
//mmpbool begin                       
                        if (ord($this->data[$spos+8])==1) {
                            $this->addcell($row, $column, "TRUE", 1);
                        } else {
                            $this->addcell($row, $column, "FALSE", 0);
                        }
//mmpbool end                       
                    } elseif ((ord($this->data[$spos+6])==2) &&
(ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
                        //Error formula. Error code is in +2;
                    } elseif ((ord($this->data[$spos+6])==3) &&
(ord($this->data[$spos+12])==255) && (ord($this->data[$spos+13])==255)) {
                        //Formula result is a null string.
//mmptext begin                        
                        $this->addcell($row, $column, '');
//mmptext end                        
                    } else {
                        // result is a number, so first 14 bytes are just
like a _NUMBER record
                        $tmp = unpack("ddouble", substr($this->data, $spos
+ 6, 8)); // It machine machine dependent
                        if ($this->isDate($spos)) {
                            list($string, $raw) =
$this->createDate($tmp['double']);
                         //   $this->addcell(DateRecord($r, 1));
                        }else{
                            //$raw = $tmp[''];
                            if (isset($this->_columnsFormat[$column + 1])){
                                    $this->curformat =
$this->_columnsFormat[$column + 1];
                            }
                            $raw = $this->createNumber($spos);
                            $string = sprintf($this->curformat, $raw *
$this->multiplier);

                         //   $this->addcell(NumberRecord($r));
                        }
                        $this->addcell($row, $column, $string, $raw);
                        //echo "Number $row $column $string\n";
                    }
                    break;                    
//mmptext begin                    
                case Spreadsheet_Excel_Reader_Type_STRING:
            echo "Found a STRING\n<br>";
            if ($version == Spreadsheet_Excel_Reader_BIFF8){
                // Unicode 16 string, like an SST record
            echo "In STRING section, BIFF8\n<br>";
                        $xpos = $spos;
                                                $numChars =
ord($this->data[$xpos]) | (ord($this->data[$xpos+1]) << 8);
                                                $xpos += 2;
                                                $optionFlags =
ord($this->data[$xpos]);
                                                $xpos++;
                                        $asciiEncoding = (($optionFlags &
0x01) == 0) ;
                                                $extendedString = (
($optionFlags & 0x04) != 0);

                                                // See if string contains
formatting information
                                                $richString = (
($optionFlags & 0x08) != 0);

                                                if ($richString) {
                                        // Read in the crun
                                                        $formattingRuns =
ord($this->data[$xpos]) | (ord($this->data[$xpos+1]) << 8);
                                                        $xpos += 2;
                                                }

                                                if ($extendedString) {
                                                  // Read in cchExtRst
                                                  $extendedRunLength =
$this->_GetInt4d($this->data, $xpos);
                                                  $xpos += 4;
                                                }

                                                $len = ($asciiEncoding)?
$numChars : $numChars*2;
                                                $retstr =
substr($this->data, $xpos, $len);
                                                $xpos += $len;

                                                $retstr = ($asciiEncoding)
? $retstr : $this->_encodeUTF16($retstr);
                                                echo "Str = $retstr\n<br>";
                                                //$this->sst[]=$retstr;
                    }elseif ($version == Spreadsheet_Excel_Reader_BIFF7){
                // Simple byte string
                echo "In STRING section, BIFF7\n<br>";
                        $xpos = $spos;
                                                $numChars =
ord($this->data[$xpos]) | (ord($this->data[$xpos+1]) << 8);
                                                $xpos += 2;
                                                $retstr =
substr($this->data, $xpos, $numChars);
                                                echo "Str = $retstr\n<br>";
                    }
                    $this->addcell($SAVErow, $SAVEcolumn, $retstr);
            break;
//mmptext end            
                case Spreadsheet_Excel_Reader_Type_BOOLERR:

Original issue reported on code.google.com by mar...@gmail.com on 19 Dec 2008 at 9:34

GoogleCodeExporter commented 9 years ago
This change will be released in version 2.2

Original comment by matthew....@gmail.com on 25 Mar 2009 at 2:30

GoogleCodeExporter commented 9 years ago
This is great!! .. How, then, can the Formula STRING be retrieved for the cell. 
What 
I am expecting to retrieve is something like " =SUM(CB2:CS2)". 
Thanks
David McBride 

Original comment by dsm1...@gmail.com on 24 Sep 2009 at 4:44

GoogleCodeExporter commented 9 years ago
From a quick inspection of the Excel File Format document at
http://sc.openoffice.org/excelfileformat.pdf it appears that it is rather 
difficult
to extract the formula string. In particular, the formula string is not stored 
in the
file. Rather, a tokenized version of the formula is stored. The code required to
reconstruct the string formula would be of similar complexity to the current 
entire
phpexcelreader program.

Original comment by mar...@gmail.com on 7 Oct 2009 at 9:13