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

empty columns trigger duplicate column names error #543

Closed
6 tasks done
lcdservices opened this issue Dec 12, 2024 · 1 comment
Closed
6 tasks done

empty columns trigger duplicate column names error #543

lcdservices opened this issue Dec 12, 2024 · 1 comment

Comments

@lcdservices
Copy link

Bug Report

Information Description
Version 9.19.0
PHP version 8.1.31
OS Platform CloudLinux v7.9.0

Summary

When importing a CSV file that has empty columns, a "duplicate column names" error is thrown when calling getHeader(). According to the documentation, extra fields should be truncated. I've examined the raw text file and the extra columns are "clean" -- there are no characters between the commas delineating columns. I don't see anything in the documentation suggesting additional steps required to auto-truncate these fields. Ref: https://csv.thephpleague.com/9.0/reader/

Standalone code, or other way to reproduce the problem

$csv = Reader::createFromPath($file['name'], 'r');
$csv->setHeaderOffset(0);
$csv->setEscape('');
$header = $csv->getHeader();
$records = $csv->getRecords();

Expected result

Empty columns ignored/truncated and getHeader() returns column names without error.

Actual result

Error thrown.

Checks before submitting

  • Be sure that there isn't already an issue about this. See: Issues list
  • Be sure that there isn't already a pull request about this. See: Pull requests
  • I have added every step to reproduce the bug.
  • If possible I added relevant code examples.
  • This issue is about 1 bug and nothing more.
  • The issue has a descriptive title. For example: "JSON rendering failed on Windows for filenames with space".
@nyamsprod
Copy link
Member

nyamsprod commented Dec 13, 2024

@lcdservices thanks for using the package. I believe you misread the documentation or perhaps the documentation is not clear enough. When using the setHeaderOffset the package will use the selected record as header. Upon selection the package will examine the content of the selected record.

In you case you seem to have multiple empty columns which means that the record you selected contains multiple fields with empty value which result in the error you are seeing.

To avoid that error you can either stop using ay header at all OR when using a retrieving method like getRecords or getRecordsAsObject fill in the $heaader parameters which will super-seed the header record.

What you are describing is expected behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants