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

Method convertExcelDate

lib/Datasource/LocalSpreadsheet.php:268–328  ·  view source on GitHub ↗

* every cell is checked if it is an excel date (stored in number of days since 1990) * then, the date format from excel is reapplied in php * @param $spreadsheet * @param $values * @param $range * @return array * @throws \PhpOffice\PhpSpreadsheet\Exception */

($spreadsheet, $values, $range)

Source from the content-addressed store, hash-verified

266 * @throws \PhpOffice\PhpSpreadsheet\Exception
267 */
268 private function convertExcelDate($spreadsheet, $values, $range): array {
269 $start = str_getcsv($range, ':');
270 $startCell = Coordinate::coordinateFromString($start[0]);
271 $startColumn = (int)Coordinate::columnIndexFromString($startCell[0]);
272 $startRow = (int)$startCell[1];
273
274 foreach ($values as $rowIndex => $row) {
275 foreach ($row as $columnIndex => $cellValue) {
276 $columnLetter = Coordinate::stringFromColumnIndex($columnIndex + $startColumn);
277 $rowNumber = $rowIndex + $startRow;
278 $coordinate = $columnLetter . $rowNumber;
279 $cell = $spreadsheet->getActiveSheet()->getCell($coordinate);
280
281 // Check if it's part of a merged range
282 if ($cell->isInMergeRange()) {
283 // Overwrite the cell with the top-left cell of the merged range
284 $mergedRange = $cell->getMergeRange();
285 [$startCell] = explode(':', $mergedRange);
286 $cell = $spreadsheet->getActiveSheet()->getCell($startCell);
287 $values[$rowIndex][$columnIndex] = $cell->getValue();
288 }
289
290 $excelFormat = $cell->getStyle()->getNumberFormat()->getFormatCode();
291
292 if (preg_match('/%/', $excelFormat)) {
293 // Convert percentage to decimal value
294 $cellValue = $cell->getCalculatedValue();
295 $values[$rowIndex][$columnIndex] = round($cellValue, 2);
296 } elseif (Date::isDateTime($cell)) {
297 // handle date values
298 $excelFormat = rtrim($excelFormat, ";@");
299
300 // Check if it's a duration format (e.g., h:mm, [h]:mm, h:mm:ss).
301 // A date-time format can contain the same time tokens.
302 if (!preg_match('/[dy]/i', $excelFormat) && preg_match('/(?:\[h+\]|h+):?m+(?::?s+)?/i', $excelFormat)) {
303 // Convert time duration to decimal
304 $excelTime = $cell->getCalculatedValue();
305 $totalHours = Date::excelToDateTimeObject($excelTime)->format('G'); // Extract hours
306 $totalMinutes = Date::excelToDateTimeObject($excelTime)->format('i'); // Extract minutes
307 $totalSeconds = Date::excelToDateTimeObject($excelTime)->format('s'); // Extract seconds
308
309 // Convert the time to decimal (minutes)
310 $totalMinutesValue = ($totalHours * 60) + $totalMinutes + ($totalSeconds / 60);
311 $values[$rowIndex][$columnIndex] = round($totalMinutesValue, 2); // Rounded to 2 decimal places
312 } else {
313 // Keep date values independent of spreadsheet and server locales.
314 // Charts can parse the ISO value reliably; presentation formatting
315 // is applied by the client without changing the underlying value.
316 $date = Date::excelToDateTimeObject($cell->getCalculatedValue());
317 $formatTokens = preg_replace('/"[^"]*"|\\\\.|\[[^\]]*\]/', '', $excelFormat);
318 $targetFormat = 'Y-m-d';
319 if (preg_match('/[hs]/i', $formatTokens)) {
320 $targetFormat .= preg_match('/s/i', $formatTokens) ? ' H:i:s' : ' H:i';
321 }
322 $values[$rowIndex][$columnIndex] = $date->format($targetFormat);
323 }
324 }
325 }

Callers 1

readDataMethod · 0.95

Calls 3

getValueMethod · 0.80
getStyleMethod · 0.80
formatMethod · 0.45

Tested by

no test coverage detected