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

Method readData

lib/Datasource/LocalSpreadsheet.php:85–168  ·  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

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

Callers

nothing calls this directly

Calls 8

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

Tested by

no test coverage detected