Skip to content

Commit

Permalink
Merge branch 'master' into nooctal
Browse files Browse the repository at this point in the history
  • Loading branch information
oleibman authored Aug 23, 2023
2 parents 54e8661 + b879cdd commit f8f9805
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 21 deletions.
23 changes: 12 additions & 11 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$hyperlinkReader = new Hyperlinks($docSheet);
// Locate hyperlink relations
$relationsFileName = dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels';
if ($zip->locateName($relationsFileName)) {
if ($zip->locateName($relationsFileName) !== false) {
$relsWorksheet = $this->loadZip($relationsFileName, Namespaces::RELATIONSHIPS);
$hyperlinkReader->readHyperlinks($relsWorksheet);
}
Expand All @@ -1066,7 +1066,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
if (!$this->readDataOnly) {
// Locate comment relations
$commentRelations = dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels';
if ($zip->locateName($commentRelations)) {
if ($zip->locateName($commentRelations) !== false) {
$relsWorksheet = $this->loadZip($commentRelations, Namespaces::RELATIONSHIPS);
foreach ($relsWorksheet->Relationship as $elex) {
$ele = self::getAttributes($elex);
Expand Down Expand Up @@ -1128,7 +1128,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
$drowingImages = [];
$VMLDrawingsRelations = dirname($relPath) . '/_rels/' . basename($relPath) . '.rels';
$vmlDrawingContents[$relName] = $this->getSecurityScannerOrThrow()->scan($this->getFromZipArchive($zip, $relPath));
if ($zip->locateName($VMLDrawingsRelations)) {
if ($zip->locateName($VMLDrawingsRelations) !== false) {
$relsVMLDrawing = $this->loadZip($VMLDrawingsRelations, Namespaces::RELATIONSHIPS);
foreach ($relsVMLDrawing->Relationship as $elex) {
$ele = self::getAttributes($elex);
Expand Down Expand Up @@ -1192,7 +1192,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
if (isset($drowingImages[$fillImageRelId])) {
$objDrawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$objDrawing->setName($fillImageTitle);
$imagePath = str_replace('../', 'xl/', $drowingImages[$fillImageRelId]);
$imagePath = str_replace(['../', '/xl/'], 'xl/', $drowingImages[$fillImageRelId]);
$objDrawing->setPath(
'zip://' . File::realpath($filename) . '#' . $imagePath,
true,
Expand Down Expand Up @@ -1249,7 +1249,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
if ($vmlHfRidAttr !== null && isset($vmlHfRidAttr['id'])) {
$vmlHfRid = (string) $vmlHfRidAttr['id'][0];
}
if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) {
if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') !== false) {
$relsWorksheet = $this->loadZipNoNamespace(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels', Namespaces::RELATIONSHIPS);
$vmlRelationship = '';

Expand Down Expand Up @@ -1328,7 +1328,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
if (substr($drawingFilename, 0, 8) === '/xl//xl/') {
$drawingFilename = substr($drawingFilename, 5);
}
if ($zip->locateName($drawingFilename)) {
if ($zip->locateName($drawingFilename) !== false) {
$relsWorksheet = $this->loadZipNoNamespace($drawingFilename, Namespaces::RELATIONSHIPS);
$drawings = [];
foreach ($relsWorksheet->Relationship as $ele) {
Expand Down Expand Up @@ -2095,7 +2095,7 @@ private static function getLockValue(SimpleXmlElement $protection, string $key):
private function readFormControlProperties(Spreadsheet $excel, string $dir, string $fileWorksheet, Worksheet $docSheet, array &$unparsedLoadedData): void
{
$zip = $this->zip;
if (!$zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) {
if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') === false) {
return;
}

Expand All @@ -2122,7 +2122,7 @@ private function readFormControlProperties(Spreadsheet $excel, string $dir, stri
private function readPrinterSettings(Spreadsheet $excel, string $dir, string $fileWorksheet, Worksheet $docSheet, array &$unparsedLoadedData): void
{
$zip = $this->zip;
if (!$zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels')) {
if ($zip->locateName(dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels') === false) {
return;
}

Expand All @@ -2142,8 +2142,9 @@ private function readPrinterSettings(Spreadsheet $excel, string $dir, string $fi
$rId = $rId . 'ps'; // rIdXXX, add 'ps' suffix to avoid identical resource identifier collision with unparsed vmlDrawing
}
$unparsedPrinterSettings[$rId] = [];
$unparsedPrinterSettings[$rId]['filePath'] = self::dirAdd("$dir/$fileWorksheet", $printerSettings['Target']);
$unparsedPrinterSettings[$rId]['relFilePath'] = (string) $printerSettings['Target'];
$target = (string) str_replace('/xl/', '../', (string) $printerSettings['Target']);
$unparsedPrinterSettings[$rId]['filePath'] = self::dirAdd("$dir/$fileWorksheet", $target);
$unparsedPrinterSettings[$rId]['relFilePath'] = $target;
$unparsedPrinterSettings[$rId]['content'] = $this->getSecurityScannerOrThrow()->scan($this->getFromZipArchive($zip, $unparsedPrinterSettings[$rId]['filePath']));
}
unset($unparsedPrinterSettings);
Expand Down Expand Up @@ -2238,7 +2239,7 @@ private function readTablesInTablesFile(
$tablePartRel = (string) $relation['id'];
$relationsFileName = dirname("$dir/$fileWorksheet") . '/_rels/' . basename($fileWorksheet) . '.rels';

if ($zip->locateName($relationsFileName)) {
if ($zip->locateName($relationsFileName) !== false) {
$relsTableReferences = $this->loadZip($relationsFileName, Namespaces::RELATIONSHIPS);
foreach ($relsTableReferences->Relationship as $relationship) {
$relationshipAttributes = self::getAttributes($relationship, '');
Expand Down
16 changes: 6 additions & 10 deletions src/PhpSpreadsheet/Writer/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ private function generateRowCellDataValue(Worksheet $worksheet, Cell $cell, stri
* @param null|Cell|string $cell
* @param array|string $cssClass
*/
private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass, string $cellType): string
private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass): string
{
$cellData = ' ';
if ($cell instanceof Cell) {
Expand All @@ -1384,14 +1384,10 @@ private function generateRowCellData(Worksheet $worksheet, $cell, &$cssClass, st
$cssClass .= ' style' . $cell->getXfIndex();
$cssClass .= ' ' . $cell->getDataType();
} elseif (is_array($cssClass)) {
if ($cellType == 'th') {
if (isset($this->cssStyles['th.style' . $cell->getXfIndex()])) {
$cssClass = array_merge($cssClass, $this->cssStyles['th.style' . $cell->getXfIndex()]);
}
} else {
if (isset($this->cssStyles['td.style' . $cell->getXfIndex()])) {
$cssClass = array_merge($cssClass, $this->cssStyles['td.style' . $cell->getXfIndex()]);
}
$index = $cell->getXfIndex();
$styleIndex = 'td.style' . $index . ', th.style' . $index;
if (isset($this->cssStyles[$styleIndex])) {
$cssClass = array_merge($cssClass, $this->cssStyles[$styleIndex]);
}

// General horizontal alignment: Actual horizontal alignment depends on dataType
Expand Down Expand Up @@ -1511,7 +1507,7 @@ private function generateRow(Worksheet $worksheet, array $values, $row, $cellTyp
[$cell, $cssClass, $coordinate] = $this->generateRowCellCss($worksheet, $cellAddress, $row, $colNum);

// Cell Data
$cellData = $this->generateRowCellData($worksheet, $cell, $cssClass, $cellType);
$cellData = $this->generateRowCellData($worksheet, $cell, $cssClass);

// Hyperlink?
if ($worksheet->hyperlinkExists($coordinate) && !$worksheet->getHyperlink($coordinate)->isInternal()) {
Expand Down
47 changes: 47 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xlsx/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,51 @@ public function testIssue2316(): void
self::assertStringContainsString('若為宅配物流僅能選「純配送」', $commentString);
self::assertSame('Anderson Chen 陳宗棠', $comment->getAuthor());
}

public function testIssue3654(): void
{
// Reader was ignoring comments.
$filename = 'tests/data/Reader/XLSX/issue.3654.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);

$sheet = $spreadsheet->getActiveSheet();
$expectedComments = [
'X4', 'AD4', 'AN4',
'X5', 'AD5', 'AN5',
'AN6', // X6 and AD6 are uncommented on original
'X7', 'AD7', 'AN7',
'X8', 'AD8', 'AN8',
'X9', 'AD9', 'AN9',
'X10', 'AD10', 'AN10',
'X11', 'AD11', 'AN11',
'X12', 'AD12', 'AN12',
];
self::assertEquals($expectedComments, array_keys($sheet->getComments()));
self::assertStringContainsString('.png', $sheet->getComment('X4')->getBackgroundImage()->getPath());
self::assertStringContainsString('.jpeg', $sheet->getComment('AN12')->getBackgroundImage()->getPath());
$spreadsheet->disconnectWorksheets();
}

public function testIssue3654c(): void
{
// Reader was ignoring comments.
$filename = 'tests/data/Reader/XLSX/issue.3654c.xlsx';
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);

$sheet = $spreadsheet->getActiveSheet();
$comments = $sheet->getComments();
self::assertCount(326, $comments);
self::assertStringContainsString('.png', $sheet->getComment('X4')->getBackgroundImage()->getPath());
self::assertStringContainsString('.jpeg', $sheet->getComment('AN12')->getBackgroundImage()->getPath());

// Spreadsheet generated by Flexcel. Make sure we managed
// to locate printerSettings since it used an unexpected link.
$pageSetupRel = $spreadsheet->getUnparsedLoadedData()['sheets']['Worksheet']['pageSetupRelId'] ?? '';
self::assertSame('flId1ps', $pageSetupRel);
$pageSetupPath = $spreadsheet->getUnparsedLoadedData()['sheets']['Worksheet']['printerSettings'][substr($pageSetupRel, 3)]['filePath'] ?? '';
self::assertSame('xl/printerSettings/printerSettings1.bin', $pageSetupPath);
$spreadsheet->disconnectWorksheets();
}
}
37 changes: 37 additions & 0 deletions tests/PhpSpreadsheetTests/Writer/Html/Issue3678Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Writer\Html;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Writer\Html;
use PHPUnit\Framework\TestCase;

class Issue3678Test extends TestCase
{
public function testInlineAndNot(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue(1);
$styleArray = [
'fill' => [
'fillType' => Fill::FILL_SOLID,
'color' => ['rgb' => 'FFFF00'],
],
];
$sheet->getStyle('A1')->applyFromArray($styleArray);
$style1 = "vertical-align:bottom; border-bottom:none #000000; border-top:none #000000; border-left:none #000000; border-right:none #000000; color:#000000; font-family:'Calibri'; font-size:11pt; background-color:#FFFF00";
$style2 = $style1 . '; text-align:right; width:42pt';
$writer = new Html($spreadsheet);
$html = $writer->generateHtmlAll();
self::assertStringContainsString('td.style1, th.style1 { ' . $style1 . ' }', $html);
self::assertStringContainsString('<td class="column0 style1 n">1</td>', $html);
self::assertStringContainsString('table.sheet0 col.col0 { width:42pt }', $html);
self::assertStringContainsString('.n { text-align:right }', $html);
$writer->setUseInlineCss(true);
$html = $writer->generateHtmlAll();
self::assertStringContainsString('<td style="' . $style2 . '">1</td>', $html);
$spreadsheet->disconnectWorksheets();
}
}
Binary file added tests/data/Reader/XLSX/issue.3654.xlsx
Binary file not shown.
Binary file added tests/data/Reader/XLSX/issue.3654c.xlsx
Binary file not shown.

0 comments on commit f8f9805

Please sign in to comment.