diff --git a/samples/templates/SylkTest.slk b/samples/templates/SylkTest.slk index d5dd5bbe..95770d04 100644 --- a/samples/templates/SylkTest.slk +++ b/samples/templates/SylkTest.slk @@ -52,7 +52,7 @@ P;EArial;M200 P;EArial;M200;SI P;EArial;M200;SBI P;EArial;M200;SBU -P;EArial;M200;SBIU +P;EArial;M220;SBIU P;EArial;M200 P;EArial;M200;SI F;P0;DG0G8;M255 @@ -115,6 +115,7 @@ F;P19;FG0G;X4 C;Y7;X2;K2.34 C;X3;KFALSE C;Y8;X2;K3.45 +C;Y9;X2;K2.34;EMEDIAN(R[-3]C:R[-1]C) F;Y9;X1 F;X2 F;X3 diff --git a/src/PhpSpreadsheet/Reader/Slk.php b/src/PhpSpreadsheet/Reader/Slk.php index f40eba74..0e147376 100644 --- a/src/PhpSpreadsheet/Reader/Slk.php +++ b/src/PhpSpreadsheet/Reader/Slk.php @@ -2,8 +2,10 @@ namespace PhpOffice\PhpSpreadsheet\Reader; +use InvalidArgumentException; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; +use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\Border; @@ -38,6 +40,20 @@ class Slk extends BaseReader */ private $format = 0; + /** + * Fonts. + * + * @var array + */ + private $fonts = []; + + /** + * Font Count. + * + * @var int + */ + private $fontcount = 0; + /** * Create a new SYLK Reader instance. */ @@ -55,10 +71,9 @@ class Slk extends BaseReader */ public function canRead($pFilename) { - // Check if file exists try { $this->openFile($pFilename); - } catch (Exception $e) { + } catch (InvalidArgumentException $e) { return false; } @@ -78,12 +93,24 @@ class Slk extends BaseReader return $hasDelimiter && $hasId; } + private function canReadOrBust(string $pFilename): void + { + if (!$this->canRead($pFilename)) { + throw new ReaderException($pFilename . ' is an Invalid SYLK file.'); + } + $this->openFile($pFilename); + } + /** * Set input encoding. * + * @deprecated no use is made of this property + * * @param string $pValue Input encoding, eg: 'ANSI' * * @return $this + * + * @codeCoverageIgnore */ public function setInputEncoding($pValue) { @@ -95,7 +122,11 @@ class Slk extends BaseReader /** * Get input encoding. * + * @deprecated no use is made of this property + * * @return string + * + * @codeCoverageIgnore */ public function getInputEncoding() { @@ -112,22 +143,16 @@ class Slk extends BaseReader public function listWorksheetInfo($pFilename) { // Open file - if (!$this->canRead($pFilename)) { - throw new Exception($pFilename . ' is an Invalid Spreadsheet file.'); - } - $this->openFile($pFilename); + $this->canReadOrBust($pFilename); $fileHandle = $this->fileHandle; rewind($fileHandle); $worksheetInfo = []; - $worksheetInfo[0]['worksheetName'] = 'Worksheet'; - $worksheetInfo[0]['lastColumnLetter'] = 'A'; - $worksheetInfo[0]['lastColumnIndex'] = 0; - $worksheetInfo[0]['totalRows'] = 0; - $worksheetInfo[0]['totalColumns'] = 0; + $worksheetInfo[0]['worksheetName'] = basename($pFilename, '.slk'); // loop through one row (line) at a time in the file $rowIndex = 0; + $columnIndex = 0; while (($rowData = fgets($fileHandle)) !== false) { $columnIndex = 0; @@ -139,28 +164,26 @@ class Slk extends BaseReader $rowData = explode("\t", str_replace('¤', ';', str_replace(';', "\t", str_replace(';;', '¤', rtrim($rowData))))); $dataType = array_shift($rowData); - if ($dataType == 'C') { - // Read cell value data + if ($dataType == 'B') { foreach ($rowData as $rowDatum) { switch ($rowDatum[0]) { - case 'C': case 'X': $columnIndex = substr($rowDatum, 1) - 1; break; - case 'R': case 'Y': $rowIndex = substr($rowDatum, 1); break; } - - $worksheetInfo[0]['totalRows'] = max($worksheetInfo[0]['totalRows'], $rowIndex); - $worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], $columnIndex); } + + break; } } + $worksheetInfo[0]['lastColumnIndex'] = $columnIndex; + $worksheetInfo[0]['totalRows'] = $rowIndex; $worksheetInfo[0]['lastColumnLetter'] = Coordinate::stringFromColumnIndex($worksheetInfo[0]['lastColumnIndex'] + 1); $worksheetInfo[0]['totalColumns'] = $worksheetInfo[0]['lastColumnIndex'] + 1; @@ -186,6 +209,294 @@ class Slk extends BaseReader return $this->loadIntoExisting($pFilename, $spreadsheet); } + private $colorArray = [ + 'FF00FFFF', // 0 - cyan + 'FF000000', // 1 - black + 'FFFFFFFF', // 2 - white + 'FFFF0000', // 3 - red + 'FF00FF00', // 4 - green + 'FF0000FF', // 5 - blue + 'FFFFFF00', // 6 - yellow + 'FFFF00FF', // 7 - magenta + ]; + + private $fontStyleMappings = [ + 'B' => 'bold', + 'I' => 'italic', + 'U' => 'underline', + ]; + + private function processFormula(string $rowDatum, bool &$hasCalculatedValue, string &$cellDataFormula, string $row, string $column): void + { + $cellDataFormula = '=' . substr($rowDatum, 1); + // Convert R1C1 style references to A1 style references (but only when not quoted) + $temp = explode('"', $cellDataFormula); + $key = false; + foreach ($temp as &$value) { + // Only count/replace in alternate array entries + if ($key = !$key) { + preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE); + // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way + // through the formula from left to right. Reversing means that we work right to left.through + // the formula + $cellReferences = array_reverse($cellReferences); + // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent, + // then modify the formula to use that new reference + foreach ($cellReferences as $cellReference) { + $rowReference = $cellReference[2][0]; + // Empty R reference is the current row + if ($rowReference == '') { + $rowReference = $row; + } + // Bracketed R references are relative to the current row + if ($rowReference[0] == '[') { + $rowReference = $row + trim($rowReference, '[]'); + } + $columnReference = $cellReference[4][0]; + // Empty C reference is the current column + if ($columnReference == '') { + $columnReference = $column; + } + // Bracketed C references are relative to the current column + if ($columnReference[0] == '[') { + $columnReference = $column + trim($columnReference, '[]'); + } + $A1CellReference = Coordinate::stringFromColumnIndex($columnReference) . $rowReference; + + $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0])); + } + } + } + unset($value); + // Then rebuild the formula string + $cellDataFormula = implode('"', $temp); + $hasCalculatedValue = true; + } + + private function processCRecord(array $rowData, Spreadsheet &$spreadsheet, string &$row, string &$column): void + { + // Read cell value data + $hasCalculatedValue = false; + $cellDataFormula = $cellData = ''; + foreach ($rowData as $rowDatum) { + switch ($rowDatum[0]) { + case 'C': + case 'X': + $column = substr($rowDatum, 1); + + break; + case 'R': + case 'Y': + $row = substr($rowDatum, 1); + + break; + case 'K': + $cellData = substr($rowDatum, 1); + + break; + case 'E': + $this->processFormula($rowDatum, $hasCalculatedValue, $cellDataFormula, $row, $column); + + break; + } + } + $columnLetter = Coordinate::stringFromColumnIndex((int) $column); + $cellData = Calculation::unwrapResult($cellData); + + // Set cell value + $this->processCFinal($spreadsheet, $hasCalculatedValue, $cellDataFormula, $cellData, "$columnLetter$row"); + } + + private function processCFinal(Spreadsheet &$spreadsheet, bool $hasCalculatedValue, string $cellDataFormula, string $cellData, string $coordinate): void + { + // Set cell value + $spreadsheet->getActiveSheet()->getCell($coordinate)->setValue(($hasCalculatedValue) ? $cellDataFormula : $cellData); + if ($hasCalculatedValue) { + $cellData = Calculation::unwrapResult($cellData); + $spreadsheet->getActiveSheet()->getCell($coordinate)->setCalculatedValue($cellData); + } + } + + private function processFRecord(array $rowData, Spreadsheet &$spreadsheet, string &$row, string &$column): void + { + // Read cell formatting + $formatStyle = $columnWidth = ''; + $startCol = $endCol = ''; + $fontStyle = ''; + $styleData = []; + foreach ($rowData as $rowDatum) { + switch ($rowDatum[0]) { + case 'C': + case 'X': + $column = substr($rowDatum, 1); + + break; + case 'R': + case 'Y': + $row = substr($rowDatum, 1); + + break; + case 'P': + $formatStyle = $rowDatum; + + break; + case 'W': + [$startCol, $endCol, $columnWidth] = explode(' ', substr($rowDatum, 1)); + + break; + case 'S': + $this->styleSettings($rowDatum, $styleData, $fontStyle); + + break; + } + } + $this->addFormats($spreadsheet, $formatStyle, $row, $column); + $this->addFonts($spreadsheet, $fontStyle, $row, $column); + $this->addStyle($spreadsheet, $styleData, $row, $column); + $this->addWidth($spreadsheet, $columnWidth, $startCol, $endCol); + } + + private $styleSettingsFont = ['D' => 'bold', 'I' => 'italic']; + + private $styleSettingsBorder = [ + 'B' => 'bottom', + 'L' => 'left', + 'R' => 'right', + 'T' => 'top', + ]; + + private function styleSettings(string $rowDatum, array &$styleData, string &$fontStyle): void + { + $styleSettings = substr($rowDatum, 1); + $iMax = strlen($styleSettings); + for ($i = 0; $i < $iMax; ++$i) { + $char = $styleSettings[$i]; + if (array_key_exists($char, $this->styleSettingsFont)) { + $styleData['font'][$this->styleSettingsFont[$char]] = true; + } elseif (array_key_exists($char, $this->styleSettingsBorder)) { + $styleData['borders'][$this->styleSettingsBorder[$char]]['borderStyle'] = Border::BORDER_THIN; + } elseif ($char == 'S') { + $styleData['fill']['fillType'] = \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_PATTERN_GRAY125; + } elseif ($char == 'M') { + if (preg_match('/M([1-9]\\d*)/', $styleSettings, $matches)) { + $fontStyle = $matches[1]; + } + } + } + } + + private function addFormats(Spreadsheet &$spreadsheet, string $formatStyle, string $row, string $column): void + { + if ($formatStyle && $column > '' && $row > '') { + $columnLetter = Coordinate::stringFromColumnIndex((int) $column); + if (isset($this->formats[$formatStyle])) { + $spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($this->formats[$formatStyle]); + } + } + } + + private function addFonts(Spreadsheet &$spreadsheet, string $fontStyle, string $row, string $column): void + { + if ($fontStyle && $column > '' && $row > '') { + $columnLetter = Coordinate::stringFromColumnIndex((int) $column); + if (isset($this->fonts[$fontStyle])) { + $spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($this->fonts[$fontStyle]); + } + } + } + + private function addStyle(Spreadsheet &$spreadsheet, array $styleData, string $row, string $column): void + { + if ((!empty($styleData)) && $column > '' && $row > '') { + $columnLetter = Coordinate::stringFromColumnIndex($column); + $spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($styleData); + } + } + + private function addWidth(Spreadsheet $spreadsheet, string $columnWidth, string $startCol, string $endCol): void + { + if ($columnWidth > '') { + if ($startCol == $endCol) { + $startCol = Coordinate::stringFromColumnIndex((int) $startCol); + $spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth); + } else { + $startCol = Coordinate::stringFromColumnIndex($startCol); + $endCol = Coordinate::stringFromColumnIndex($endCol); + $spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth((float) $columnWidth); + do { + $spreadsheet->getActiveSheet()->getColumnDimension(++$startCol)->setWidth($columnWidth); + } while ($startCol != $endCol); + } + } + } + + private function processPRecord(array $rowData, Spreadsheet &$spreadsheet): void + { + // Read shared styles + $formatArray = []; + $fromFormats = ['\-', '\ ']; + $toFormats = ['-', ' ']; + foreach ($rowData as $rowDatum) { + switch ($rowDatum[0]) { + case 'P': + $formatArray['numberFormat']['formatCode'] = str_replace($fromFormats, $toFormats, substr($rowDatum, 1)); + + break; + case 'E': + case 'F': + $formatArray['font']['name'] = substr($rowDatum, 1); + + break; + case 'M': + $formatArray['font']['size'] = substr($rowDatum, 1) / 20; + + break; + case 'L': + $this->processPColors($rowDatum, $formatArray); + + break; + case 'S': + $this->processPFontStyles($rowDatum, $formatArray); + + break; + } + } + $this->processPFinal($spreadsheet, $formatArray); + } + + private function processPColors(string $rowDatum, array &$formatArray): void + { + if (preg_match('/L([1-9]\\d*)/', $rowDatum, $matches)) { + $fontColor = $matches[1] % 8; + $formatArray['font']['color']['argb'] = $this->colorArray[$fontColor]; + } + } + + private function processPFontStyles(string $rowDatum, array &$formatArray): void + { + $styleSettings = substr($rowDatum, 1); + $iMax = strlen($styleSettings); + for ($i = 0; $i < $iMax; ++$i) { + if (array_key_exists($styleSettings[$i], $this->fontStyleMappings)) { + $formatArray['font'][$this->fontStyleMappings[$styleSettings[$i]]] = true; + } + } + } + + private function processPFinal(Spreadsheet &$spreadsheet, array $formatArray): void + { + if (array_key_exists('numberFormat', $formatArray)) { + $this->formats['P' . $this->format] = $formatArray; + ++$this->format; + } elseif (array_key_exists('font', $formatArray)) { + ++$this->fontcount; + $this->fonts[$this->fontcount] = $formatArray; + if ($this->fontcount === 1) { + $spreadsheet->getDefaultStyle()->applyFromArray($formatArray); + } + } + } + /** * Loads PhpSpreadsheet from file into PhpSpreadsheet instance. * @@ -196,10 +507,7 @@ class Slk extends BaseReader public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet) { // Open file - if (!$this->canRead($pFilename)) { - throw new Exception($pFilename . ' is an Invalid Spreadsheet file.'); - } - $this->openFile($pFilename); + $this->canReadOrBust($pFilename); $fileHandle = $this->fileHandle; rewind($fileHandle); @@ -208,251 +516,32 @@ class Slk extends BaseReader $spreadsheet->createSheet(); } $spreadsheet->setActiveSheetIndex($this->sheetIndex); - - $fromFormats = ['\-', '\ ']; - $toFormats = ['-', ' ']; + $spreadsheet->getActiveSheet()->setTitle(basename($pFilename, '.slk')); // Loop through file $column = $row = ''; // loop through one row (line) at a time in the file - while (($rowData = fgets($fileHandle)) !== false) { + while (($rowDataTxt = fgets($fileHandle)) !== false) { // convert SYLK encoded $rowData to UTF-8 - $rowData = StringHelper::SYLKtoUTF8($rowData); + $rowDataTxt = StringHelper::SYLKtoUTF8($rowDataTxt); // explode each row at semicolons while taking into account that literal semicolon (;) // is escaped like this (;;) - $rowData = explode("\t", str_replace('¤', ';', str_replace(';', "\t", str_replace(';;', '¤', rtrim($rowData))))); + $rowData = explode("\t", str_replace('¤', ';', str_replace(';', "\t", str_replace(';;', '¤', rtrim($rowDataTxt))))); $dataType = array_shift($rowData); - // Read shared styles if ($dataType == 'P') { - $formatArray = []; - foreach ($rowData as $rowDatum) { - switch ($rowDatum[0]) { - case 'P': - $formatArray['numberFormat']['formatCode'] = str_replace($fromFormats, $toFormats, substr($rowDatum, 1)); - - break; - case 'E': - case 'F': - $formatArray['font']['name'] = substr($rowDatum, 1); - - break; - case 'L': - $formatArray['font']['size'] = substr($rowDatum, 1); - - break; - case 'S': - $styleSettings = substr($rowDatum, 1); - $iMax = strlen($styleSettings); - for ($i = 0; $i < $iMax; ++$i) { - switch ($styleSettings[$i]) { - case 'I': - $formatArray['font']['italic'] = true; - - break; - case 'D': - $formatArray['font']['bold'] = true; - - break; - case 'T': - $formatArray['borders']['top']['borderStyle'] = Border::BORDER_THIN; - - break; - case 'B': - $formatArray['borders']['bottom']['borderStyle'] = Border::BORDER_THIN; - - break; - case 'L': - $formatArray['borders']['left']['borderStyle'] = Border::BORDER_THIN; - - break; - case 'R': - $formatArray['borders']['right']['borderStyle'] = Border::BORDER_THIN; - - break; - } - } - - break; - } - } - $this->formats['P' . $this->format++] = $formatArray; - // Read cell value data + // Read shared styles + $this->processPRecord($rowData, $spreadsheet); } elseif ($dataType == 'C') { - $hasCalculatedValue = false; - $cellData = $cellDataFormula = ''; - foreach ($rowData as $rowDatum) { - switch ($rowDatum[0]) { - case 'C': - case 'X': - $column = substr($rowDatum, 1); - - break; - case 'R': - case 'Y': - $row = substr($rowDatum, 1); - - break; - case 'K': - $cellData = substr($rowDatum, 1); - - break; - case 'E': - $cellDataFormula = '=' . substr($rowDatum, 1); - // Convert R1C1 style references to A1 style references (but only when not quoted) - $temp = explode('"', $cellDataFormula); - $key = false; - foreach ($temp as &$value) { - // Only count/replace in alternate array entries - if ($key = !$key) { - preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE); - // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way - // through the formula from left to right. Reversing means that we work right to left.through - // the formula - $cellReferences = array_reverse($cellReferences); - // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent, - // then modify the formula to use that new reference - foreach ($cellReferences as $cellReference) { - $rowReference = $cellReference[2][0]; - // Empty R reference is the current row - if ($rowReference == '') { - $rowReference = $row; - } - // Bracketed R references are relative to the current row - if ($rowReference[0] == '[') { - $rowReference = $row + trim($rowReference, '[]'); - } - $columnReference = $cellReference[4][0]; - // Empty C reference is the current column - if ($columnReference == '') { - $columnReference = $column; - } - // Bracketed C references are relative to the current column - if ($columnReference[0] == '[') { - $columnReference = $column + trim($columnReference, '[]'); - } - $A1CellReference = Coordinate::stringFromColumnIndex($columnReference) . $rowReference; - - $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0])); - } - } - } - unset($value); - // Then rebuild the formula string - $cellDataFormula = implode('"', $temp); - $hasCalculatedValue = true; - - break; - } - } - $columnLetter = Coordinate::stringFromColumnIndex($column); - $cellData = Calculation::unwrapResult($cellData); - - // Set cell value - $spreadsheet->getActiveSheet()->getCell($columnLetter . $row)->setValue(($hasCalculatedValue) ? $cellDataFormula : $cellData); - if ($hasCalculatedValue) { - $cellData = Calculation::unwrapResult($cellData); - $spreadsheet->getActiveSheet()->getCell($columnLetter . $row)->setCalculatedValue($cellData); - } - // Read cell formatting + // Read cell value data + $this->processCRecord($rowData, $spreadsheet, $row, $column); } elseif ($dataType == 'F') { - $formatStyle = $columnWidth = $styleSettings = ''; - $styleData = []; - foreach ($rowData as $rowDatum) { - switch ($rowDatum[0]) { - case 'C': - case 'X': - $column = substr($rowDatum, 1); - - break; - case 'R': - case 'Y': - $row = substr($rowDatum, 1); - - break; - case 'P': - $formatStyle = $rowDatum; - - break; - case 'W': - [$startCol, $endCol, $columnWidth] = explode(' ', substr($rowDatum, 1)); - - break; - case 'S': - $styleSettings = substr($rowDatum, 1); - $iMax = strlen($styleSettings); - for ($i = 0; $i < $iMax; ++$i) { - switch ($styleSettings[$i]) { - case 'I': - $styleData['font']['italic'] = true; - - break; - case 'D': - $styleData['font']['bold'] = true; - - break; - case 'T': - $styleData['borders']['top']['borderStyle'] = Border::BORDER_THIN; - - break; - case 'B': - $styleData['borders']['bottom']['borderStyle'] = Border::BORDER_THIN; - - break; - case 'L': - $styleData['borders']['left']['borderStyle'] = Border::BORDER_THIN; - - break; - case 'R': - $styleData['borders']['right']['borderStyle'] = Border::BORDER_THIN; - - break; - } - } - - break; - } - } - if (($formatStyle > '') && ($column > '') && ($row > '')) { - $columnLetter = Coordinate::stringFromColumnIndex($column); - if (isset($this->formats[$formatStyle])) { - $spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($this->formats[$formatStyle]); - } - } - if ((!empty($styleData)) && ($column > '') && ($row > '')) { - $columnLetter = Coordinate::stringFromColumnIndex($column); - $spreadsheet->getActiveSheet()->getStyle($columnLetter . $row)->applyFromArray($styleData); - } - if ($columnWidth > '') { - if ($startCol == $endCol) { - $startCol = Coordinate::stringFromColumnIndex($startCol); - $spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth); - } else { - $startCol = Coordinate::stringFromColumnIndex($startCol); - $endCol = Coordinate::stringFromColumnIndex($endCol); - $spreadsheet->getActiveSheet()->getColumnDimension($startCol)->setWidth($columnWidth); - do { - $spreadsheet->getActiveSheet()->getColumnDimension(++$startCol)->setWidth($columnWidth); - } while ($startCol != $endCol); - } - } + // Read cell formatting + $this->processFRecord($rowData, $spreadsheet, $row, $column); } else { - foreach ($rowData as $rowDatum) { - switch ($rowDatum[0]) { - case 'C': - case 'X': - $column = substr($rowDatum, 1); - - break; - case 'R': - case 'Y': - $row = substr($rowDatum, 1); - - break; - } - } + $this->columnRowFromRowData($rowData, $column, $row); } } @@ -463,6 +552,18 @@ class Slk extends BaseReader return $spreadsheet; } + private function columnRowFromRowData(array $rowData, string &$column, string &$row): void + { + foreach ($rowData as $rowDatum) { + $char0 = $rowDatum[0]; + if ($char0 === 'X' || $char0 == 'C') { + $column = substr($rowDatum, 1); + } elseif ($char0 === 'Y' || $char0 == 'R') { + $row = substr($rowDatum, 1); + } + } + } + /** * Get sheet index. * diff --git a/tests/PhpSpreadsheetTests/Reader/CsvTest.php b/tests/PhpSpreadsheetTests/Reader/CsvTest.php index e4ccd931..e11e0ff7 100644 --- a/tests/PhpSpreadsheetTests/Reader/CsvTest.php +++ b/tests/PhpSpreadsheetTests/Reader/CsvTest.php @@ -255,6 +255,10 @@ EOF; self::assertEquals('\'', $reader->getEnclosure()); $reader->setEnclosure(''); self::assertEquals('"', $reader->getEnclosure()); + // following tests from BaseReader + self::assertTrue($reader->getReadEmptyCells()); + self::assertFalse($reader->getIncludeCharts()); + self::assertNull($reader->getLoadSheetsOnly()); } public function testReadEmptyFileName(): void diff --git a/tests/PhpSpreadsheetTests/Reader/SlkTest.php b/tests/PhpSpreadsheetTests/Reader/SlkTest.php new file mode 100644 index 00000000..4c7cc513 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/SlkTest.php @@ -0,0 +1,134 @@ +listWorkSheetInfo(self::$testbook); + $info0 = $workSheetInfo[0]; + self::assertEquals('SylkTest', $info0['worksheetName']); + self::assertEquals('J', $info0['lastColumnLetter']); + self::assertEquals(9, $info0['lastColumnIndex']); + self::assertEquals(18, $info0['totalRows']); + self::assertEquals(10, $info0['totalColumns']); + } + + public function testBadFileName(): void + { + $this->expectException(ReaderException::class); + $reader = new Slk(); + self::assertNull($reader->setLoadSheetsOnly(null)->getLoadSheetsOnly()); + $reader->listWorkSheetInfo(self::$testbook . 'xxx'); + } + + public function testBadFileName2(): void + { + $reader = new Slk(); + self::assertFalse($reader->canRead(self::$testbook . 'xxx')); + } + + public function testNotSylkFile(): void + { + $this->expectException(ReaderException::class); + $reader = new Slk(); + $reader->listWorkSheetInfo(__FILE__); + } + + public function testLoadSlk(): void + { + $reader = new Slk(); + $spreadsheet = $reader->load(self::$testbook); + $sheet = $spreadsheet->getActiveSheet(); + self::assertEquals('SylkTest', $sheet->getTitle()); + + self::assertEquals('FFFF0000', $sheet->getCell('A1')->getStyle()->getFont()->getColor()->getARGB()); + self::assertEquals(Fill::FILL_PATTERN_GRAY125, $sheet->getCell('A2')->getStyle()->getFill()->getFillType()); + self::assertEquals(Font::UNDERLINE_SINGLE, $sheet->getCell('A4')->getStyle()->getFont()->getUnderline()); + self::assertEquals('Test with (;) in string', $sheet->getCell('A4')->getValue()); + + self::assertEquals(22269, $sheet->getCell('A10')->getValue()); + self::assertEquals('dd/mm/yyyy', $sheet->getCell('A10')->getStyle()->getNumberFormat()->getFormatCode()); + self::assertEquals('19/12/1960', $sheet->getCell('A10')->getFormattedValue()); + self::assertEquals(1.5, $sheet->getCell('A11')->getValue()); + self::assertEquals('# ?/?', $sheet->getCell('A11')->getStyle()->getNumberFormat()->getFormatCode()); + self::assertEquals('1 1/2', $sheet->getCell('A11')->getFormattedValue()); + + self::assertEquals('=B1+C1', $sheet->getCell('H1')->getValue()); + self::assertEquals('=E2&F2', $sheet->getCell('J2')->getValue()); + self::assertEquals('=SUM(C1:C4)', $sheet->getCell('I5')->getValue()); + self::assertEquals('=MEDIAN(B6:B8)', $sheet->getCell('B9')->getValue()); + + self::assertEquals(11, $sheet->getCell('E1')->getStyle()->getFont()->getSize()); + self::assertTrue($sheet->getCell('E1')->getStyle()->getFont()->getBold()); + self::assertTrue($sheet->getCell('E1')->getStyle()->getFont()->getItalic()); + self::assertEquals(Font::UNDERLINE_SINGLE, $sheet->getCell('E1')->getStyle()->getFont()->getUnderline()); + self::assertFalse($sheet->getCell('E2')->getStyle()->getFont()->getBold()); + self::assertFalse($sheet->getCell('E2')->getStyle()->getFont()->getItalic()); + self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('E2')->getStyle()->getFont()->getUnderline()); + self::assertTrue($sheet->getCell('E3')->getStyle()->getFont()->getBold()); + self::assertFalse($sheet->getCell('E3')->getStyle()->getFont()->getItalic()); + self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('E3')->getStyle()->getFont()->getUnderline()); + self::assertFalse($sheet->getCell('E4')->getStyle()->getFont()->getBold()); + self::assertTrue($sheet->getCell('E4')->getStyle()->getFont()->getItalic()); + self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('E4')->getStyle()->getFont()->getUnderline()); + + self::assertTrue($sheet->getCell('F1')->getStyle()->getFont()->getBold()); + self::assertFalse($sheet->getCell('F1')->getStyle()->getFont()->getItalic()); + self::assertEquals(Font::UNDERLINE_SINGLE, $sheet->getCell('F1')->getStyle()->getFont()->getUnderline()); + self::assertFalse($sheet->getCell('F2')->getStyle()->getFont()->getBold()); + self::assertFalse($sheet->getCell('F2')->getStyle()->getFont()->getItalic()); + self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('F2')->getStyle()->getFont()->getUnderline()); + self::assertTrue($sheet->getCell('F3')->getStyle()->getFont()->getBold()); + self::assertTrue($sheet->getCell('F3')->getStyle()->getFont()->getItalic()); + self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('F3')->getStyle()->getFont()->getUnderline()); + self::assertFalse($sheet->getCell('F4')->getStyle()->getFont()->getBold()); + self::assertFalse($sheet->getCell('F4')->getStyle()->getFont()->getItalic()); + self::assertEquals(Font::UNDERLINE_NONE, $sheet->getCell('F4')->getStyle()->getFont()->getUnderline()); + + self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C10')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C10')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C10')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C10')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C12')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C12')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C12')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C12')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C14')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C14')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C14')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C14')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C16')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C16')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C16')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertEquals(Border::BORDER_NONE, $sheet->getCell('C16')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getTop()->getBorderStyle()); + self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getRight()->getBorderStyle()); + self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getBottom()->getBorderStyle()); + self::assertEquals(Border::BORDER_THIN, $sheet->getCell('C18')->getStyle()->getBorders()->getLeft()->getBorderStyle()); + // Have not yet figured out how C6/C7 are centred + } + + public function testSheetIndex(): void + { + $reader = new Slk(); + $sheetIndex = 2; + $reader->setSheetIndex($sheetIndex); + self::assertEquals($sheetIndex, $reader->getSheetIndex()); + $spreadsheet = $reader->load(self::$testbook); + $sheet = $spreadsheet->setActiveSheetIndex($sheetIndex); + self::assertEquals('SylkTest', $sheet->getTitle()); + + self::assertEquals('FFFF0000', $sheet->getCell('A1')->getStyle()->getFont()->getColor()->getARGB()); + } +}