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

Allow differing column counts per row in CSV export #1414

Closed
1 of 3 tasks
AndrewMonty opened this issue Mar 11, 2020 · 2 comments · Fixed by #4076
Closed
1 of 3 tasks

Allow differing column counts per row in CSV export #1414

AndrewMonty opened this issue Mar 11, 2020 · 2 comments · Fixed by #4076

Comments

@AndrewMonty
Copy link

This is:

What is the expected behavior?

There should be an option for each row in a CSV file to have a different number of columns.

Consider a spreadsheet where the first few rows are not actually column headers, but rather some custom heading (in my case a requirement of some third party system consuming the CSV file).

Represented as an array, this might look like:

$data = [
    ['Pretext', 'More pretext'],
    ['this', 'is not', 'a header'],
    ['value 1'],
    ['value 2'],
    ['value 3']
];

And as a spreadsheet:

A B C
Pretext More pretext
this is not a header
value 1
value 2
value 3

Writing to a CSV should have the option to exclude the blank cells on each row:

"Pretext","More pretext"
"this","is not","a header"
"value 1"
"value 2"
"value 3"

What is the current behavior?

Currently, the spreadsheet described above would save as a csv like:

"Pretext","More pretext",
"this","is not","a header"
"value 1",,
"value 2",,
"value 3",,

Where the row with the most columns dictates the column count for all rows, and any that fall short are filled with blanks.

What are the steps to reproduce?

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

namespace PhpOffice\PhpSpreadsheetTests\Writer\Csv;

use PHPUnit\Framework\TestCase;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Shared\File;

class VariableColumnsTest extends TestCase
{
    public function testVariableColumns()
    {
        $spreadsheet = new Spreadsheet();
        $spreadsheet->setActiveSheetIndex(0);
        $spreadsheet->getActiveSheet()->setCellValue('A1', 'A1');
        $spreadsheet->getActiveSheet()->setCellValue('B1', 'B1');
        $spreadsheet->getActiveSheet()->setCellValue('A2', 'A2');
        $spreadsheet->getActiveSheet()->setCellValue('B2', 'B2');
        $spreadsheet->getActiveSheet()->setCellValue('C2', 'C2');
        $spreadsheet->getActiveSheet()->setCellValue('A3', 'A3');

        $filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test');
        $writer = IOFactory::createWriter($spreadsheet, 'Csv');
        $writer->save($filename);

        $contents = file_get_contents($filename);

        $rows = explode(PHP_EOL, $contents);

        // current
        $this->assertEquals('"A1","B1",""', $rows[0]);
        $this->assertEquals('"A2","B2","C2"', $rows[1]);
        $this->assertEquals('"A3","",""', $rows[2]);

        // desired
        $this->assertEquals('"A1","B1"', $rows[0]);
        $this->assertEquals('"A2","B2","C2"', $rows[1]);
        $this->assertEquals('"A3"', $rows[2]);
    }
}

Which versions of PhpSpreadsheet and PHP are affected?

PhpSpreadsheet 1.10.1

PHP 7.3

@stale
Copy link

stale bot commented May 10, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If this is still an issue for you, please try to help by debugging it further and sharing your results.
Thank you for your contributions.

@stale stale bot added the stale label May 10, 2020
@stale stale bot closed this as completed May 17, 2020
oleibman added a commit to oleibman/PhpSpreadsheet that referenced this issue Jun 27, 2024
Supersedes PR PHPOffice#1415 by @AndrewMonty, which went stale in May 2020, and which is not directly usable due to changes between now and then. Fix PHPOffice#1414, which also went stale; I will remove the stale status and reopen the issue pending the merging of this PR.

Add an option to CSV Writer so that it writes the cells for a row only through the highest data column used in the row, rather than through the highest data column used in the worksheet.
@oleibman
Copy link
Collaborator

Will be fixed by 4076.

@oleibman oleibman reopened this Jun 27, 2024
@stale stale bot removed the stale label Jun 27, 2024
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