-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
48 lines (43 loc) · 1.13 KB
/
code.power
File metadata and controls
48 lines (43 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Get CSV or Excel headers from the provided file path.
*
* @param string $filePath
* @param int $targetRow
*
* @return array|null
* @since 3.2.0
*/
public function get(string $filePath, int $targetRow = 1): ?array
{
if (!is_file($filePath))
{
return null;
}
try {
$chunkFilter = new ChunkReadFilter(1, 20);
$inputFileType = IOFactory::identify($filePath);
$reader = IOFactory::createReader($inputFileType);
$reader->setReadFilter($chunkFilter);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($filePath);
$headers = [];
foreach ($spreadsheet->getActiveSheet()->getRowIterator() as $row)
{
if ($row->getRowIndex() === $targetRow)
{
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $cell)
{
$headers[$cell->getColumn()] = $cell->getValue();
}
break;
}
}
$spreadsheet->disconnectWorksheets();
return $headers;
} catch (\Exception $e) {
// Log or handle exceptions as necessary
return null;
}
}