From fe4aacef2d245831d93c6bfb66957348381c4626 Mon Sep 17 00:00:00 2001 From: mach3 Date: Sat, 16 Nov 2019 20:09:31 +0900 Subject: [PATCH 1/4] fix bug in getColumnLetter() --- src/Google/Spreadsheet/Sheet.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Google/Spreadsheet/Sheet.php b/src/Google/Spreadsheet/Sheet.php index 378508a..fd6dcd1 100644 --- a/src/Google/Spreadsheet/Sheet.php +++ b/src/Google/Spreadsheet/Sheet.php @@ -162,8 +162,8 @@ public function update ($vars, $condition) { $data = array(); foreach ($vars as $key => $value) { $c = array_search($key, $this->header); - if (!$c) continue; - $col = $this->getColumnLetter($c); + if (false === $c) continue; + $col = $this->getColumnLetter($c + 1); foreach ($rows as $r) { $r += 1; $data[] = new Google_Service_Sheets_ValueRange(array( @@ -193,7 +193,7 @@ public function update ($vars, $condition) { private function getColumnLetter ($index) { $s = array(); for ($i = $index; $i > 0; $i = intval(($i) / 26)) { - array_push($s, chr(65 + (($i) % 26))); + array_push($s, chr(65 + (($i - 1) % 26))); } return implode('', array_reverse($s)); } From 49d3e398427ee70126ac4e3c6b510b9797075ec6 Mon Sep 17 00:00:00 2001 From: mach3 Date: Sat, 16 Nov 2019 20:45:01 +0900 Subject: [PATCH 2/4] add `->edit()` --- src/Google/Spreadsheet/Sheet.php | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Google/Spreadsheet/Sheet.php b/src/Google/Spreadsheet/Sheet.php index fd6dcd1..eadbe00 100644 --- a/src/Google/Spreadsheet/Sheet.php +++ b/src/Google/Spreadsheet/Sheet.php @@ -23,6 +23,10 @@ class Google_Spreadsheet_Sheet { 'cache_expires' => 600 ); + private $params = array( + 'valueInputOption' => 'USER_ENTERED' + ); + /** * @constructor * @param string $name @@ -128,6 +132,22 @@ public function select ($condition) { return $result; } + /** + * Update cells' value by row and column number + * + * @param integer $row + * @param integer $col + * @param array|string $values + * @return Google_Service_Sheets_AppendValuesResponse $response + */ + public function edit ($row, $col, $values) { + $values = gettype($values) !== 'array' ? (array) $values : $values; + $target = $this->getColumnLetter($col) . $row; + $range = implode('!', array($this->name, $target)); + $body = new Google_Service_Sheets_ValueRange(array('values' => array($values))); + return $this->sheet->spreadsheets_values->update($this->id, $range, $body, $this->params); + } + /** * Insert a new row to spreadsheet * Forcely fetch up-to-date data from remote before inserting @@ -144,8 +164,7 @@ public function insert ($vars) { array_push($values[0], in_array($key, array_keys($vars)) ? (string) $vars[$key] : ''); } $body = new Google_Service_Sheets_ValueRange(array('values' => $values)); - $params = array('valueInputOption' => 'USER_ENTERED'); - return $this->sheet->spreadsheets_values->append($this->id, $this->name, $body, $params); + return $this->sheet->spreadsheets_values->append($this->id, $this->name, $body, $this->params); } /** @@ -175,10 +194,9 @@ public function update ($vars, $condition) { } } if (count($data)) { - $body = new Google_Service_Sheets_BatchUpdateValuesRequest(array( - 'valueInputOption' => 'USER_ENTERED', - 'data' => $data - )); + $params = $this->params; + $params['data'] = $data; + $body = new Google_Service_Sheets_BatchUpdateValuesRequest($params); return $this->sheet->spreadsheets_values->batchUpdate($this->id, $body); } return null; From 57b155fcbba5387ad865e62afda23c19a560f1cd Mon Sep 17 00:00:00 2001 From: mach3 Date: Sat, 16 Nov 2019 21:01:03 +0900 Subject: [PATCH 3/4] add `->init()` --- src/Google/Spreadsheet/Sheet.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Google/Spreadsheet/Sheet.php b/src/Google/Spreadsheet/Sheet.php index eadbe00..86a7b6d 100644 --- a/src/Google/Spreadsheet/Sheet.php +++ b/src/Google/Spreadsheet/Sheet.php @@ -55,6 +55,20 @@ public function config ($options) { return $this; } + /** + * Initialize sheet with header items + * + * @param array $header + * @return Google_Service_Sheets_AppendValuesResponse $response + */ + public function init ($header) { + $this->fetch(true); + if (!empty($this->values)) { + throw new Exception("'{$this->name}' is not empty"); + } + return $this->edit(1, 1, $header); + } + /** * Fetch data from Sheets API or cache * - Automatically parse the data From c3db33e69cbda506f3239be913b5b78510aef328 Mon Sep 17 00:00:00 2001 From: mach3 Date: Sat, 16 Nov 2019 21:12:37 +0900 Subject: [PATCH 4/4] update document --- CHANGELOG.md | 5 +++++ README.md | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3067396..1d322db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Change Log +## 1.1.0 + +- add `$sheet->edit()` to update cells' value by row and column number manually +- add `$sheet->init()` to initialize sheet with header items + ## 1.0.0 - rewrite all for Sheets API v4 diff --git a/README.md b/README.md index 7fd27db..0acafe7 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,20 @@ var_dump($sheet->items); ## Usage +### Initialize sheet (>= 1.1.0) + +The target sheet must be empty + +```php +$sheet->init(array( + 'id', + 'name', + 'age', + 'email', + 'note' +)); +``` + ### Select rows ```php @@ -88,6 +102,18 @@ $sheet->update( $items = $sheet->fetch(true)->items; ``` +### Update cells (>=1.1.0) + +`edit` method let you to update cells' value manually + +```php +// Update `B2` cell +$sheet->edit(2, 2, 'Tom'); + +// Update `C1:C4` cells +$sheet->edit(3, 1, array(1, 'John', 23, 'john@example.com')); +``` + ### Get up-to-date table data ```php