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

Error reading file column into specific row #4063

Closed
1 of 8 tasks
Sab-tech-lab opened this issue Jun 5, 2024 · 5 comments · Fixed by #4064
Closed
1 of 8 tasks

Error reading file column into specific row #4063

Sab-tech-lab opened this issue Jun 5, 2024 · 5 comments · Fixed by #4064

Comments

@Sab-tech-lab
Copy link

Sab-tech-lab commented Jun 5, 2024

This is:

- [x ] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Correct reading of rows and columns.

What is the current behavior?

After a few empty cells, reading the file extracts the next row cell.

What are the steps to reproduce?

Try to import the attached file and try to read line 17, I tried both “getRowIterator” and “toArray” function:
This is excel with row
immagine
This is the result of reading:
Array
(
[0] => 226
[1] =>  
[2] => 38871**(this value is the first value of next row)**
)
If you read line before this row the result is correct.
The cell of the row is empty.

Please provide a Minimal, Complete, and Verifiable example of code that exhibits the issue without relying on an external Excel file or a web server:

<?php

require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$Spreadsheet = $reader->load("path-to-file");
$Sheet = $Spreadsheet->getActiveSheet();
$RowIt = $Sheet->getRowIterator(17, 17);
foreach ($RowIt as $idx => $Row) {
      $CellIterator = $Row->getCellIterator();
      $data = array();
      foreach ($CellIterator as $Cell) {
          array_push($data, (string) $Cell->getValue());
      }
  }

$data = $Sheet->toArray(null, true, true, true)[17]
;

20240605111107_it-IT_en-GB.xlsx
If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

What features do you think are causing the issue

  • Reader
  • Writer
  • Styles
  • Data Validations
  • Formula Calculations
  • Charts
  • AutoFilter
  • Form Elements

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

If open file with excel and only save it the file will be read correctly.

Which versions of PhpSpreadsheet and PHP are affected?

2.1

@Sab-tech-lab
Copy link
Author

Excel file was be created with PhpSpreadsheet.

@Sab-tech-lab
Copy link
Author

After several trials in creating the file I found that this error if occurs if I try to write a cell with empty content, if I instead jump when the content is empty the problem is no longer generated. Is it normal for entering empty content to cause this problem? Of course now that I know this I avoid writing the cell if it is empty.

@oleibman
Copy link
Collaborator

oleibman commented Jun 5, 2024

Yes, there is a flaw in Xlsx Reader. I'm surprised it hasn't surfaced before. Expect a fix in the next day or two.

@Sab-tech-lab
Copy link
Author

Thanks, fantastic.

@oleibman
Copy link
Collaborator

oleibman commented Jun 5, 2024

Aha, it was previously reported as issue #1293, which was closed as stale in March 2020. I will reopen it (and reclose it when my PR is merged).

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Jun 6, 2024
Fix PHPOffice#4063. Fix PHPOffice#1560. Fix PHPOffice#1293. PhpSpreadsheet is not accounting for an empty string in Xlsx sharedStrings.xml.The code which parses it in Reader/Xlsx looks for a `t` or `r` tag descending from `si`, but, in this case, the tag is coded as `<si/>`, with neither t nor r tag descending. An else clause is added to set the string to empty string in this case.

I was surprised that this had not turned up before, and a search through the archives found at least 2 earlier reports from 4 years ago. Those had been marked stale; the stale indicator is removed, and the issues are re-opened, to be closed when this PR is merged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants