Пример #1
0
 /**
  * Check a string that it satisfies Excel requirements
  *
  * @param mixed Value to sanitize to an Excel string
  * @return mixed Sanitized value
  */
 public static function checkString($pValue = null)
 {
     if ($pValue instanceof RichText) {
         // TODO: Sanitize Rich-Text string (max. character count is 32,767)
         return $pValue;
     }
     // string must never be longer than 32,767 characters, truncate if necessary
     $pValue = Shared_String::Substring($pValue, 0, 32767);
     // we require that newline is represented as "\n" in core, not as "\r\n" or "\r"
     $pValue = str_replace(array("\r\n", "\r"), "\n", $pValue);
     return $pValue;
 }
Пример #2
0
 /**
  * Read LABELSST record
  * This record represents a cell that contains a string. It
  * replaces the LABEL record and RSTRING record used in
  * BIFF2-BIFF5.
  *
  * --	"OpenOffice.org's Documentation of the Microsoft
  * 		Excel File Format"
  */
 private function _readLabelSst()
 {
     $length = $this->_GetInt2d($this->_data, $this->_pos + 2);
     $recordData = substr($this->_data, $this->_pos + 4, $length);
     // move stream pointer to next record
     $this->_pos += 4 + $length;
     // offset: 0; size: 2; index to row
     $row = $this->_GetInt2d($recordData, 0);
     // offset: 2; size: 2; index to column
     $column = $this->_GetInt2d($recordData, 2);
     $columnString = Cell::stringFromColumnIndex($column);
     // Read cell?
     if (!is_null($this->getReadFilter()) && $this->getReadFilter()->readCell($columnString, $row + 1, $this->_phpSheet->getTitle())) {
         // offset: 4; size: 2; index to XF record
         $xfIndex = $this->_GetInt2d($recordData, 4);
         // offset: 6; size: 4; index to SST record
         $index = $this->_GetInt4d($recordData, 6);
         // add cell
         if (($fmtRuns = $this->_sst[$index]['fmtRuns']) && !$this->_readDataOnly) {
             // then we should treat as rich text
             $richText = new RichText();
             $charPos = 0;
             for ($i = 0; $i <= count($this->_sst[$index]['fmtRuns']); ++$i) {
                 if (isset($fmtRuns[$i])) {
                     $text = Shared_String::Substring($this->_sst[$index]['value'], $charPos, $fmtRuns[$i]['charPos'] - $charPos);
                     $charPos = $fmtRuns[$i]['charPos'];
                 } else {
                     $text = Shared_String::Substring($this->_sst[$index]['value'], $charPos, Shared_String::CountCharacters($this->_sst[$index]['value']));
                 }
                 if (Shared_String::CountCharacters($text) > 0) {
                     if ($i == 0) {
                         // first text run, no style
                         $richText->createText($text);
                     } else {
                         $textRun = $richText->createTextRun($text);
                         if (isset($fmtRuns[$i - 1])) {
                             if ($fmtRuns[$i - 1]['fontIndex'] < 4) {
                                 $fontIndex = $fmtRuns[$i - 1]['fontIndex'];
                             } else {
                                 // this has to do with that index 4 is omitted in all BIFF versions for some strange reason
                                 // check the OpenOffice documentation of the FONT record
                                 $fontIndex = $fmtRuns[$i - 1]['fontIndex'] - 1;
                             }
                             $textRun->setFont(clone $this->_objFonts[$fontIndex]);
                         }
                     }
                 }
             }
             $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
             $cell->setValueExplicit($richText, Cell_DataType::TYPE_STRING);
         } else {
             $cell = $this->_phpSheet->getCell($columnString . ($row + 1));
             $cell->setValueExplicit($this->_sst[$index]['value'], Cell_DataType::TYPE_STRING);
         }
         if (!$this->_readDataOnly) {
             // add style information
             $cell->setXfIndex($this->_mapCellXfIndex[$xfIndex]);
         }
     }
 }