From dc88b3b31ab578c2755cb6f529f8aaf935f2d26a Mon Sep 17 00:00:00 2001 From: mach3 Date: Wed, 22 Feb 2017 20:39:12 +0900 Subject: [PATCH 1/2] let Sheet::update() to receive condition as 1st argument --- src/Google/Spreadsheet/Sheet.php | 46 +++++++++++++++++--------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/Google/Spreadsheet/Sheet.php b/src/Google/Spreadsheet/Sheet.php index b31cc51..a514995 100644 --- a/src/Google/Spreadsheet/Sheet.php +++ b/src/Google/Spreadsheet/Sheet.php @@ -61,41 +61,45 @@ public function select($condition = null){ return array_filter($this->items, $condition); } if(is_array($condition)){ - $result = array(); - foreach($this->items as $row){ - $invalid = false; + return array_filter($this->items, function($item) use ($condition){ + $invalid = 0; foreach($condition as $key => $value){ - if($row[$key] !== $value){ $invalid = true; } + if($item[$key] !== $value){ $invalid ++; } } - if($invalid){ continue; } - array_push($result, $row); - } - return $result; + return ! $invalid; + }); } return $this->items; } /** * Update the value of column - * @param {Integer} $row + * @param {Integer|Closure|Array} $row ... Row number or condition to select * @param {Integer|String} $col ... Column number or field's name * @param {String} $value * @return {Google_Spreadsheet_Sheet} ... This */ public function update($row, $col, $value){ $col = is_string($col) ? array_search($col, array_values($this->fields), true) + 1 : $col; - $body = sprintf( - ' - - ', - $row, $col, htmlspecialchars($value) - ); - $this->client->request( - $this->link["cellsfeed"], - "POST", - array("Content-Type" => "application/atom+xml"), - $body - ); + if((is_array($row) && array_values($row) !== $row) || is_callable($row)){ + $row = array_keys($this->select($row)); + } else if(! is_array($row)){ + $row = array($row); + } + foreach($row as $r){ + $body = sprintf( + '' + . '' + . '', + $r, $col, htmlspecialchars($value) + ); + $this->client->request( + $this->link["cellsfeed"], + "POST", + array("Content-Type" => "application/atom+xml"), + $body + ); + } return $this; } From 5590fac8336ef2b3f95f238e86b40e7ebe2851f7 Mon Sep 17 00:00:00 2001 From: mach3 Date: Wed, 22 Feb 2017 20:47:22 +0900 Subject: [PATCH 2/2] (dist) 0.1.5 --- CHANGELOG.md | 4 ++++ README.md | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72456f5..18bf015 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Change Log +## 0.1.5 + +- improve: let Sheet::update() to receive condition as 1st argument + ## 0.1.4 - improve: let client to throw exception when unexpected response is returned diff --git a/README.md b/README.md index 9f94a84..a426c78 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,28 @@ $sheet->update( "name", // field's name (or column number as Integer) "Tom" ); + +$sheet->update( + array(8,16,24), // row numbers + "name", + "Tom" +); + +$sheet->update( + array( + "name" => "Tom" // condition to select + ), + "email", + "tom@example.com" +); + +$sheet->update( + function($row){ + return (int) $row["age"] > 80; // condition to select as closure + }, + "active", + "false" +); ``` ### Get up-to-date table data