MyReadFilter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 5
dl 0
loc 19
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A readCell() 0 9 4
1
<?php
2
3
namespace Samples\Sample10;
4
5
use PhpOffice\PhpSpreadsheet\IOFactory;
6
use PhpOffice\PhpSpreadsheet\Reader\IReadFilter;
7
8
require __DIR__ . '/../Header.php';
9
/** @var \PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
10
$inputFileType = 'Xls';
11
$inputFileName = __DIR__ . '/sampleData/example1.xls';
12
$sheetname = 'Data Sheet #3';
13
14
class MyReadFilter implements IReadFilter
15
{
16
    /** @param array<int, string> $columns */
17
    public function __construct(
18
        private int $startRow,
19
        private int $endRow,
20
        private array $columns
21
    ) {
22
    }
23
24
    public function readCell(string $columnAddress, int $row, string $worksheetName = ''): bool
25
    {
26
        if ($row >= $this->startRow && $row <= $this->endRow) {
27
            if (in_array($columnAddress, $this->columns)) {
28
                return true;
29
            }
30
        }
31
32
        return false;
33
    }
34
}
35
36
$filterSubset = new MyReadFilter(9, 15, range('G', 'K'));
37
38
$helper->log('Loading file ' . pathinfo($inputFileName, PATHINFO_BASENAME) . ' using IOFactory with a defined reader type of ' . $inputFileType);
39
$helper->log('Filter range is G9:K15');
40
$reader = IOFactory::createReader($inputFileType);
41
$helper->log('Loading Sheet "' . $sheetname . '" only');
42
$reader->setLoadSheetsOnly($sheetname);
43
$helper->log('Loading Sheet using configurable filter');
44
$reader->setReadFilter($filterSubset);
45
$spreadsheet = $reader->load($inputFileName);
46
47
$activeRange = $spreadsheet->getActiveSheet()->calculateWorksheetDataDimension();
48
$sheetData = $spreadsheet->getActiveSheet()->rangeToArray($activeRange, null, true, true, true);
49
$helper->displayGrid($sheetData);
50