Skip to content

Commit

Permalink
Merge pull request #18 from mach3/develop
Browse files Browse the repository at this point in the history
Version 1.1.0
  • Loading branch information
mach3 authored Nov 29, 2019
2 parents 71b03d0 + 5986440 commit d7463af
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, '[email protected]'));
```

### Get up-to-date table data

```php
Expand Down
50 changes: 41 additions & 9 deletions src/Google/Spreadsheet/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class Google_Spreadsheet_Sheet {
'cache_expires' => 600
);

private $params = array(
'valueInputOption' => 'USER_ENTERED'
);

/**
* @constructor
* @param string $name
Expand Down Expand Up @@ -51,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
Expand Down Expand Up @@ -128,6 +146,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
Expand All @@ -144,8 +178,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);
}

/**
Expand All @@ -162,8 +195,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(
Expand All @@ -175,10 +208,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;
Expand All @@ -193,7 +225,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));
}
Expand Down

0 comments on commit d7463af

Please sign in to comment.