Skip to content

Commit

Permalink
fix empty columns being ignored in native xlsx
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala committed Jan 16, 2025
1 parent e8a4a96 commit 964767b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bin/bench-read.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@


foreach ($times as $format => $dataFormat) {
echo "Results for $format" . PHP_EOL;
echo "Results for $format" . PHP_EOL . PHP_EOL;

$results = [];
foreach ($dataFormat as $class => $times) {
Expand Down
2 changes: 1 addition & 1 deletion bin/bench-write.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
}

foreach ($times as $format => $dataFormat) {
echo "Results for $format" . PHP_EOL;
echo "Results for $format" . PHP_EOL . PHP_EOL;

$results = [];
foreach ($dataFormat as $class => $times) {
Expand Down
1 change: 0 additions & 1 deletion src/SpreadCompat.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use LeKoala\SpreadCompat\Common\ZipUtils;
use RuntimeException;
use ZipArchive;
use SimpleXMLElement;

/**
* This class provides a static facade for adapters
Expand Down
43 changes: 35 additions & 8 deletions src/Xlsx/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,60 @@ public function readFile(
throw new Exception("No data");
}

$columns = iterator_to_array(SpreadCompat::excelColumnRange());
$totalColumns = null;

// Process data
$wsXml = new SimpleXMLElement($wsData);
$headers = null;
foreach ($wsXml->sheetData->children() as $row) {
$r = [];
$rowData = [];

$col = 0;

// blank cells are excluded from xml
foreach ($row->children() as $c) {
$t = (string)$c->attributes()->t;
$attrs = $c->attributes();

$t = (string)$attrs->t;
$r = (string)$attrs->r; // cell position, eg A2
$v = (string)$c->v;

// it's a shared string
if ($t === 's' && $ssXml) {
$v = $ssXml->si[(int)$c->v]->t ?? '';
$v = (string)$ssXml->si[(int)$c->v]->t ?? '';

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Cannot access property $t on mixed.

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Cannot cast mixed to string.

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Expression on left side of ?? is not nullable.

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Cannot access property $t on mixed.

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Cannot cast mixed to string.

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Expression on left side of ?? is not nullable.

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Cannot access property $t on mixed.

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Cannot cast mixed to string.

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Expression on left side of ?? is not nullable.

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Cannot access property $t on mixed.

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Cannot cast mixed to string.

Check failure on line 69 in src/Xlsx/Native.php

View workflow job for this annotation

GitHub Actions / build-test

Expression on left side of ?? is not nullable.
}

$r[] = $v;
// add as many null values as missing columns
$colLetter = preg_replace('/\d/', '', $r);
$cellIndex = array_search($colLetter, $columns);
while ($cellIndex > $col) {
$rowData[] = null;
$col++;
}

$rowData[] = $v;
$col++;
}

// expand missing columns at the end
if ($totalColumns && $col < $totalColumns) {
$rowData[] = null;
$col++;
}
if (empty($r) || $r[0] === "") {

if (empty($rowData) || $rowData[0] === "") {
continue;
}
if ($this->assoc) {
if ($headers === null) {
$headers = $r;
$headers = $rowData;
$totalColumns = count($headers);
continue;
}
$r = array_combine($headers, $r);
$rowData = array_combine($headers, $rowData);
}
yield $r;
yield $rowData;
}
}

Expand Down
30 changes: 30 additions & 0 deletions tests/SpreadCompatXlsxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,34 @@ public function testNativeCanWriteXlsx()
$this->assertStringContainsString('[Content_Types].xml', $string);
$this->assertNotEquals($string, $string2);
}

public function testNativeDontSkipEmptyCols()
{

$Native = new Native();
$Native->assoc = true;
$data = $Native->readFile(__DIR__ . '/data/empty-col.xlsx');

$arr = iterator_to_array($data);
$this->assertEquals([
[
'col1' => "v1",
'col2' => "v2",
'col3' => null,
'col4' => "v4",
],
[
'col1' => "v1",
'col2' => null,
'col3' => null,
'col4' => "v4",
],
[
'col1' => null,
'col2' => "v2",
'col3' => "v3",
'col4' => null,
]
], $arr);
}
}
Binary file added tests/data/empty-col.xlsx
Binary file not shown.

0 comments on commit 964767b

Please sign in to comment.