Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 2216 resolve office365 auto filter structure move #2218

Merged
Next Next commit
Initial adjustments to Xlsx Reader for two possible locations for Aut…
…oFiter information, either on the sheet itself for older files, or in the tables/tableX file for more recent files
  • Loading branch information
MarkBaker committed Jul 9, 2021
commit 2013a548ab3bd2ab9effc25e3e975032e43bf9af
40 changes: 38 additions & 2 deletions src/PhpSpreadsheet/Reader/Xlsx.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,31 @@ private function castToFormula($c, $r, &$cellDataType, &$value, &$calculatedValu
}
}

/**
* @param string $fileName
*
* @return string
*/
private function fileExistsInArchive(ZipArchive $archive, $fileName = '')
{
// Root-relative paths
if (strpos($fileName, '//') !== false) {
$fileName = substr($fileName, strpos($fileName, '//') + 1);
}
$fileName = File::realpath($fileName);

// Sadly, some 3rd party xlsx generators don't use consistent case for filenaming
// so we need to load case-insensitively from the zip file

// Apache POI fixes
$contents = $archive->locateName($fileName, ZipArchive::FL_NOCASE);
if ($contents === false) {
$contents = $archive->locateName(substr($fileName, 1), ZipArchive::FL_NOCASE);
}

return ($contents !== false);
}

/**
* @param string $fileName
*
Expand Down Expand Up @@ -821,8 +846,19 @@ public function load(string $pFilename, int $flags = 0): Spreadsheet
$this->readSheetProtection($docSheet, $xmlSheet);
}

if ($xmlSheet && $xmlSheet->autoFilter && !$this->readDataOnly) {
(new AutoFilter($docSheet, $xmlSheet))->load();
if ($this->readDataOnly === false) {
if ($xmlSheet && $xmlSheet->autoFilter) {
(new AutoFilter($docSheet, $xmlSheet))->load();
} elseif ($xmlSheet && $xmlSheet->tableParts && $xmlSheet->tableParts['count'] > 0) {
foreach ($xmlSheet->tableParts->children('') as $tablePart) {
$ele = self::getAttributes($tablePart, 'r');
var_dump($ele);
if ($this->fileExistsInArchive($this->zip, "xl/tables/table1.xml")) {
$autoFilter = $this->loadZip("xl/tables/table1.xml");
(new AutoFilter($docSheet, $autoFilter))->load();
}
}
}
}

if ($xmlSheet && $xmlSheet->mergeCells && $xmlSheet->mergeCells->mergeCell && !$this->readDataOnly) {
Expand Down