mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-19 06:26:48 +00:00
02479de01a
Supersedes PR #1415 by @AndrewMonty, which went stale in May 2020, and which is not directly usable due to changes between now and then. Fix #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.
82 lines
2.3 KiB
PHP
82 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Csv;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\File;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Csv;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class VariableColumnsTest extends TestCase
|
|
{
|
|
public function testVariableColumns(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->fromArray(
|
|
[
|
|
[1, 2, 3, 4],
|
|
[1, 2],
|
|
[1, 2, 3, 4, 5],
|
|
[],
|
|
[1],
|
|
[1, 2, 3],
|
|
]
|
|
);
|
|
|
|
$filename = File::temporaryFilename();
|
|
$writer = new Csv($spreadsheet);
|
|
$writer->setVariableColumns(true);
|
|
$writer->save($filename);
|
|
|
|
$contents = (string) file_get_contents($filename);
|
|
unlink($filename);
|
|
$spreadsheet->disconnectWorksheets();
|
|
|
|
$rows = explode(PHP_EOL, $contents);
|
|
|
|
self::assertSame('"1","2","3","4"', $rows[0]);
|
|
self::assertSame('"1","2"', $rows[1]);
|
|
self::assertSame('"1","2","3","4","5"', $rows[2]);
|
|
self::assertSame('', $rows[3]);
|
|
self::assertSame('"1"', $rows[4]);
|
|
self::assertSame('"1","2","3"', $rows[5]);
|
|
}
|
|
|
|
public function testFixedColumns(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->fromArray(
|
|
[
|
|
[1, 2, 3, 4],
|
|
[1, 2],
|
|
[1, 2, 3, 4, 5],
|
|
[],
|
|
[1],
|
|
[1, 2, 3],
|
|
]
|
|
);
|
|
|
|
$filename = File::temporaryFilename();
|
|
$writer = new Csv($spreadsheet);
|
|
self::assertFalse($writer->getVariableColumns());
|
|
$writer->save($filename);
|
|
|
|
$contents = (string) file_get_contents($filename);
|
|
unlink($filename);
|
|
$spreadsheet->disconnectWorksheets();
|
|
|
|
$rows = explode(PHP_EOL, $contents);
|
|
|
|
self::assertSame('"1","2","3","4",""', $rows[0]);
|
|
self::assertSame('"1","2","","",""', $rows[1]);
|
|
self::assertSame('"1","2","3","4","5"', $rows[2]);
|
|
self::assertSame('"","","","",""', $rows[3]);
|
|
self::assertSame('"1","","","",""', $rows[4]);
|
|
self::assertSame('"1","2","3","",""', $rows[5]);
|
|
}
|
|
}
|