Skip to content

Commit

Permalink
Merge pull request #7 from mach3/develop
Browse files Browse the repository at this point in the history
(dist) 0.1.5
  • Loading branch information
mach3 authored Feb 22, 2017
2 parents c717735 + 5590fac commit ad7c106
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"[email protected]"
);

$sheet->update(
function($row){
return (int) $row["age"] > 80; // condition to select as closure
},
"active",
"false"
);
```

### Get up-to-date table data
Expand Down
46 changes: 25 additions & 21 deletions src/Google/Spreadsheet/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006">
<gs:cell row="%u" col="%u" inputValue="%s"/>
</entry>',
$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(
'<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006">'
. '<gs:cell row="%u" col="%u" inputValue="%s"/>'
. '</entry>',
$r, $col, htmlspecialchars($value)
);
$this->client->request(
$this->link["cellsfeed"],
"POST",
array("Content-Type" => "application/atom+xml"),
$body
);
}
return $this;
}

Expand Down

0 comments on commit ad7c106

Please sign in to comment.