Skip to content

Commit

Permalink
Add timstamp for getRows() feature
Browse files Browse the repository at this point in the history
  • Loading branch information
yidas committed Feb 26, 2018
1 parent 7b2bc4c commit 912071a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
10 changes: 10 additions & 0 deletions demo/read.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

require __DIR__ . '/_config.php';

$filepath = __DIR__ . '/import.xlsx';

$data = \yidas\phpSpreadsheet\Helper::newSpreadsheet($filepath)
->getRows();

print_r($data);
16 changes: 14 additions & 2 deletions src/phpSpreadsheet/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* PhpSpreadsheet Helper
*
* @author Nick Tsai <myintaer@gmail.com>
* @version 1.0.0
* @version 1.1.1
* @filesource PhpSpreadsheet <https://github.com/PHPOffice/PhpSpreadsheet>
* @see https://github.com/yidas/phpspreadsheet-helper
* @example
Expand Down Expand Up @@ -375,6 +375,8 @@ public static function output($filename='excel', $format='Xlsx')
* @param bool $options [
* row (int) Ended row number
* column (int) Ended column number
* timestamp (bool) Excel datetime to Unixtime
* timestampFormat (string) Format for date() when usgin timestamp
* ]
* @return array Data of Spreadsheet
*/
Expand All @@ -386,6 +388,8 @@ public static function getRows($toString=true, Array $options=[])
$defaultOptions = [
'row' => NULL,
'column' => NULL,
'timestamp' => true,
'timestampFormat' => 'Y-m-d H:i:s', // False would use Unixtime
];
$options = array_replace($defaultOptions, $options);

Expand All @@ -399,7 +403,15 @@ public static function getRows($toString=true, Array $options=[])
for ($row = 1; $row <= $highestRow; ++$row) {
$pointerColumn = &$pointerRow[];
for ($col = 1; $col <= $highestColumn; ++$col) {
$value = $worksheet->getCellByColumnAndRow($col, $row)->getValue();
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$value = $cell->getValue();
// Timestamp option
if ($options['timestamp'] && \PhpOffice\PhpSpreadsheet\Shared\Date::isDateTime($cell)) {
$value = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToTimestamp($value);
// Timestamp Format option
$value = ($options['timestampFormat'])
? date($options['timestampFormat'], $value) : $value;
}
$value = ($toString) ? (string)$value : $value;
$pointerColumn[] = $value;
}
Expand Down

0 comments on commit 912071a

Please sign in to comment.