-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpreadsheetDataLoader.php
More file actions
110 lines (91 loc) · 3.06 KB
/
SpreadsheetDataLoader.php
File metadata and controls
110 lines (91 loc) · 3.06 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace RowBloom\SpreadsheetDataLoader;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Gnumeric;
use PhpOffice\PhpSpreadsheet\Reader\Html;
use PhpOffice\PhpSpreadsheet\Reader\IReader;
use PhpOffice\PhpSpreadsheet\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Reader\Slk;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xml;
use RowBloom\RowBloom\Config;
use RowBloom\RowBloom\DataLoaders\FsContract as DataLoadersFsContract;
use RowBloom\RowBloom\Fs\File;
use RowBloom\RowBloom\Types\Table;
use RowBloom\RowBloom\Types\TableLocation;
class SpreadsheetDataLoader implements DataLoadersFsContract
{
public const NAME = 'Spreadsheet';
public function __construct(protected ?Config $config = null)
{
}
public function getTable(TableLocation $tableLocation, ?Config $config = null): Table
{
$file = $tableLocation->toFile();
$file->mustExist()->mustBeReadable()->mustBeFile();
$reader = $this->getReaderTypeFromExtension($file);
$spreadsheet = $reader->load($file);
// TODO access all sheets
$data = $spreadsheet->getActiveSheet()->toArray();
$labels = array_shift($data);
if (count($labels) > count(array_flip($labels))) {
throw new SpreadsheetException('Duplicate labels '.implode(', ', $labels));
}
// ? check empty label
$data = array_map(
fn (array $row) => array_combine($labels, $row),
$data
);
return Table::fromArray($data);
}
public static function getSupportedFileExtensions(): array
{
return [
'xlsx' => 100,
'xlsm' => 100,
'xltx' => 100,
'xltm' => 100,
'xls' => 100,
'xlt' => 100,
'ods' => 100,
'ots' => 100,
'slk' => 100,
'xml' => 100,
'gnumeric' => 100,
'htm' => 100,
'html' => 100,
'csv' => 100,
];
}
public static function getFolderSupport(): ?int
{
return null;
}
/**
* Copied from phpoffice/phpspreadsheet:
* - Version: 1.28.0
* - File: vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\IOFactory.php
*
* setTestAutoDetect() is unique to CSV reader
*/
private function getReaderTypeFromExtension(File $file): IReader
{
$class = match ($file->extension()) {
'xlsx', 'xlsm', 'xltx', 'xltm' => Xlsx::class,
'xls', 'xlt' => Xls::class,
'ods', 'ots' => Ods::class,
'slk' => Slk::class,
'xml' => Xml::class,
'gnumeric' => Gnumeric::class,
'htm', 'html' => Html::class,
'csv' => Csv::class,
default => throw new SpreadsheetException("Unable to identify a Spreadsheet reader for {$file}"),
};
$reader = new $class;
if ($reader instanceof Csv) {
$reader->setTestAutoDetect(false);
}
return $reader;
}
}