8000 First commit · javanile/php-sheetbase@daae933 · GitHub
[go: up one dir, main page]

Skip to content

Commit daae933

Browse files
author
Francesco Bianco
committed
First commit
1 parent 72c7153 commit daae933

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/Drivers/Google/Google.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,23 @@ public function getTables()
294294
return $tables;
295295
}
296296

297+
/**
298+
*
299+
*/
300+
protected function getTableHeader()
301+
{
302+
$header = [];
303+
$headerRow = $this->getRow(0);
304+
foreach ($headerRow as $colIndex => $colName) {
305+
if (empty($colName)) {
306+
continue;
307+
}
308+
$header[$colName] = $colIndex;
309+
}
310+
311+
return $header;
312+
}
313+
297314
/**
298315
* @param $row
299316
* @param $col
@@ -422,8 +439,11 @@ public function insert($row)
422439
{
423440
$this->requireSheet();
424441

425-
$sheetTitle = $this->sheet->getProperties()->getTitle();
442+
if (self::isKeyValueRow($row)) {
443+
$row = $this->convertKeyValueRow($row);
444+
}
426445

446+
$sheetTitle = $this->sheet->getProperties()->getTitle();
427447
$range = $sheetTitle.'!A:A';
428448
$requestBody = new \Google\Service\Sheets\ValueRange([
429449
'values' => [
@@ -480,12 +500,43 @@ public function getTranskey() {
480500
public function getRow($row)
481501
{
482502
$this->requireSheetTitle();
483-
$range = '\''.$this->sheetTitle.'\'!'.$row.':'.$row;
503+
$range = '\''.$this->sheetTitle.'\'!A'.($row + 1).':'.($row + 1);
484504
$response = $this->service->spreadsheets_values->get($this->spreadsheetId, $range);
485505

486506
return $response->values[0];
487507
}
488508

509+
/**
510+
*
511+
*/
512+
protected function convertKeyValueRow($row)
513+
{
514+
$tempRow = $row;
515+
$size = count($tempRow);
516+
if (!isset($row[$size - 1])) {
517+
$row[$size - 1] = '';
518+
}
519+
$header = $this->getTableHeader();
520+
foreach ($tempRow as $key => $value) {
521+
if (isset($header[$key])) {
522+
if (isset($row[$header[$key]])) {
523+
$row[] = $row[$header[$key]];
524+
}
525+
$row[$header[$key]] = $value;
526+
unset($row[$key]);
527+
}
528+
}
529+
$maxKey = max(array_keys($row));
530+
for ($index = 0; $index < $maxKey; $index++) {
531+
if (!isset($row[$index])) {
532+
$row[$index] = '';
533+
}
534+
}
535+
ksort($row);
536+
537+
return $row;
538+
}
539+
489540
/**
490541
* @return int
491542
*/
@@ -697,4 +748,16 @@ protected static function transformArrayToOneBased($values)
697748

698749
return $newValues;
699750
}
751+
752+
/**
753+
*
754+
*/
755+
protected static function isKeyValueRow($row)
756+
{
757+
if (array() === $row) {
758+
return false;
759+
}
760+
761+
return array_keys($row) !== range(0, count($row) - 1);
762+
}
700763
}

tests/Drivers/Google/GoogleTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ public function testInsert()
150150
{
151151
$this->driver->setDatabase('test');
152152
$this->driver->setTable('test');
153-
$this->driver->insert(['A', 'B', 'C']);
153+
$header = ['A', 'B', 'C', 'D', 'E'];
154+
foreach ($header as $index => $column) {
155+
$this->driver->set(1, $index + 1, $column);
156+
}
157+
$this->driver->insert(['B' => 1, 'C' => 2, 'D' => 3]);
154158
}
155159

156160
public function testAll()

0 commit comments

Comments
 (0)
0