1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Csv; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class CsvContiguousTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
private $inputFileName = __DIR__ . '/../../../samples/Reader/sampleData/example2.csv'; |
12
|
|
|
|
13
|
|
|
public function testContiguous() |
14
|
|
|
{ |
15
|
|
|
// Create a new Reader of the type defined in $inputFileType |
16
|
|
|
$reader = new Csv(); |
17
|
|
|
|
18
|
|
|
// Define how many rows we want to read for each "chunk" |
19
|
|
|
$chunkSize = 100; |
20
|
|
|
// Create a new Instance of our Read Filter |
21
|
|
|
$chunkFilter = new CsvContiguousFilter(); |
22
|
|
|
|
23
|
|
|
// Tell the Reader that we want to use the Read Filter that we've Instantiated |
24
|
|
|
// and that we want to store it in contiguous rows/columns |
25
|
|
|
self::assertFalse($reader->getContiguous()); |
26
|
|
|
$reader->setReadFilter($chunkFilter) |
27
|
|
|
->setContiguous(true); |
28
|
|
|
|
29
|
|
|
// Instantiate a new PhpSpreadsheet object manually |
30
|
|
|
$spreadsheet = new Spreadsheet(); |
31
|
|
|
|
32
|
|
|
// Set a sheet index |
33
|
|
|
$sheet = 0; |
34
|
|
|
// Loop to read our worksheet in "chunk size" blocks |
35
|
|
|
/** $startRow is set to 2 initially because we always read the headings in row #1 * */ |
36
|
|
|
for ($startRow = 2; $startRow <= 240; $startRow += $chunkSize) { |
37
|
|
|
// Tell the Read Filter, the limits on which rows we want to read this iteration |
38
|
|
|
$chunkFilter->setRows($startRow, $chunkSize); |
39
|
|
|
|
40
|
|
|
// Increment the worksheet index pointer for the Reader |
41
|
|
|
$reader->setSheetIndex($sheet); |
42
|
|
|
// Load only the rows that match our filter into a new worksheet in the PhpSpreadsheet Object |
43
|
|
|
$reader->loadIntoExisting($this->inputFileName, $spreadsheet); |
44
|
|
|
// Set the worksheet title (to reference the "sheet" of data that we've loaded) |
45
|
|
|
// and increment the sheet index as well |
46
|
|
|
$spreadsheet->getActiveSheet()->setTitle('Country Data #' . (++$sheet)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$sheet = $spreadsheet->getSheetByName('Country Data #1'); |
50
|
|
|
self::assertEquals('Kabul', $sheet->getCell('A2')->getValue()); |
51
|
|
|
$sheet = $spreadsheet->getSheetByName('Country Data #2'); |
52
|
|
|
self::assertEquals('Lesotho', $sheet->getCell('B4')->getValue()); |
53
|
|
|
$sheet = $spreadsheet->getSheetByName('Country Data #3'); |
54
|
|
|
self::assertEquals(-20.1, $sheet->getCell('C6')->getValue()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testContiguous2() |
58
|
|
|
{ |
59
|
|
|
// Create a new Reader of the type defined in $inputFileType |
60
|
|
|
$reader = new Csv(); |
61
|
|
|
|
62
|
|
|
// Create a new Instance of our Read Filter |
63
|
|
|
$chunkFilter = new CsvContiguousFilter(); |
64
|
|
|
$chunkFilter->setFilterType(1); |
65
|
|
|
|
66
|
|
|
// Tell the Reader that we want to use the Read Filter that we've Instantiated |
67
|
|
|
// and that we want to store it in contiguous rows/columns |
68
|
|
|
$reader->setReadFilter($chunkFilter) |
69
|
|
|
->setContiguous(true); |
70
|
|
|
|
71
|
|
|
// Instantiate a new PhpSpreadsheet object manually |
72
|
|
|
$spreadsheet = new Spreadsheet(); |
73
|
|
|
|
74
|
|
|
// Loop to read our worksheet in "chunk size" blocks |
75
|
|
|
$reader->loadIntoExisting($this->inputFileName, $spreadsheet); |
76
|
|
|
|
77
|
|
|
$sheet = $spreadsheet->getActiveSheet(); |
78
|
|
|
self::assertEquals('Kabul', $sheet->getCell('A2')->getValue()); |
79
|
|
|
self::assertEquals('Kuwait', $sheet->getCell('B11')->getValue()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|