MCPcopy Create free account
hub / github.com/Rello/analytics / readData

Method readData

lib/Datasource/LocalSpreadsheet.php:86–169  ·  view source on GitHub ↗

* Read the Data * @param $option * @return array * @throws NotFoundException * @throws \OCP\Files\NotPermittedException * @throws Exception * @throws \PhpOffice\PhpSpreadsheet\Exception */

($option)

Source from the content-addressed store, hash-verified

84 * @throws \PhpOffice\PhpSpreadsheet\Exception
85 */
86 public function readData($option): array {
87 $header = $dataClean = $data = array();
88 $headerrow = $error = 0;
89
90 $file = $this->rootFolder->getUserFolder($option['user_id'])->get($option['link']);
91 $cache = $this->getCacheMetadata($option, $file->getMTime());
92 if ($cache['notModified'] === true) {
93 return [
94 'header' => [],
95 'dimensions' => [],
96 'data' => [],
97 'error' => $error,
98 'cache' => $cache,
99 ];
100 }
101
102 include_once __DIR__ . '/../../vendor/autoload.php';
103 $fileName = $file->getStorage()->getLocalFile($file->getInternalPath());
104
105 $inputFileType = IOFactory::identify($fileName);
106 $reader = IOFactory::createReader($inputFileType);
107 //$reader->setReadDataOnly(true); disabled as idDate is not working otherwise
108 if (strlen($option['sheet']) > 0) {
109 $reader->setLoadSheetsOnly([$option['sheet']]);
110 }
111
112 $rangeError = $this->validateRanges((string)$option['range']);
113 if ($rangeError !== null) {
114 return [
115 'header' => [],
116 'dimensions' => [],
117 'data' => [],
118 'error' => $rangeError,
119 'cache' => $cache,
120 ];
121 }
122
123 $spreadsheet = $reader->load($fileName);
124
125 // separated columns can be selected via ranges e.g. "A1:B9,C1:C9"
126 // these ranges are read and linked
127 $ranges = str_getcsv($option['range']);
128 foreach ($ranges as $range) {
129 $values = $spreadsheet->getActiveSheet()
130 ->rangeToArray($range, // The worksheet range that we want to retrieve
131 null, // Value that should be returned for empty cells
132 true, // Should formulas be calculated (the equivalent of getCalculatedValue() for each cell)
133 true, // Should values be formatted (the equivalent of getFormattedValue() for each cell)
134 false // Should the array be indexed by cell row and cell column
135 );
136
137 $values = $this->convertExcelDate($spreadsheet, $values, $range);
138
139 if (empty($data)) {
140 // first range will fill the array with all rows
141 $data = $values;
142 } else {
143 // further columns will be attached to the first ones

Callers

nothing calls this directly

Calls 9

getCacheMetadataMethod · 0.95
validateRangesMethod · 0.95
convertExcelDateMethod · 0.95
removeNullColumnsMethod · 0.95
containsOnlyNullMethod · 0.95
getUserFolderMethod · 0.80
getMTimeMethod · 0.80
getMethod · 0.45
loadMethod · 0.45

Tested by

no test coverage detected