Skip to content

Commit

Permalink
Merge pull request #27 from calebanthony/patch-2
Browse files Browse the repository at this point in the history
Append with header keys, refactor tests, and update documentation
  • Loading branch information
kawax committed Mar 18, 2020
2 parents 5c45832 + a350186 commit 2bbf2e3
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 92 deletions.
91 changes: 58 additions & 33 deletions README.md
Expand Up @@ -71,7 +71,7 @@ Another Google API Series.

https://docs.google.com/spreadsheets/d/{spreadsheetID}/...

### Laravel example1
### Basic Laravel Usage
```php
use Sheets;

Expand All @@ -86,14 +86,31 @@ $token = [

// all() returns array
$values = Sheets::setAccessToken($token)->spreadsheet('spreadsheetId')->sheet('Sheet 1')->all();
[
['id', 'name', 'mail'],
['1', 'name1', 'mail1'],
['2', 'name1', 'mail2']
]
// [
// ['id', 'name', 'mail'],
// ['1', 'name1', 'mail1'],
// ['2', 'name1', 'mail2']
// ]
```

### Basic Non-Laravel Usage
```php
use Revolution\Google\Sheets\Sheets;

$client = \Google_Client();
$client->setScopes([Google_Service_Sheets::DRIVE, Google_Service_Sheets::SPREADSHEETS]);
// setup Google Client
// ...

$service = new \Google_Service_Sheets($client);

$sheets = new Sheets();
$sheets->setService($service);

$values = $sheets->spreadsheet('spreadsheetID')->sheet('Sheet 1')->all();
```

### Laravel example2
### Get a sheet's values with the header as the key
```php
// get() returns Laravel Collection
$rows = Sheets::sheet('Sheet 1')->get();
Expand All @@ -114,36 +131,32 @@ view
@endforeach
```

### example3 not Laravel
### Using A1 Notation
```php
use Revolution\Google\Sheets\Sheets;

$client = \Google_Client();
$client->setScopes([Google_Service_Sheets::DRIVE, Google_Service_Sheets::SPREADSHEETS]);
// setup Google Client
// ...

$service = new \Google_Service_Sheets($client);

$sheets = new Sheets();
$sheets->setService($service);

$values = $sheets->spreadsheet('spreadsheetID')->sheet('Sheet 1')->all();
$values = Sheets::sheet('Sheet 1')->range('A1:B2')->all();
// [
// ['id', 'name'],
// ['1', 'name1'],
// ]
```

### example4 A1 notation
### Updating a specific range
```php
$values = Sheets::sheet('Sheet 1')->range('A1:B2')->all();
Sheets::sheet('Sheet 1')->range('A4')->update([['3', 'name3', 'mail3']]);
$values = Sheets::range('')->all();
[
['id', 'name'],
['1', 'name1'],
['id', 'name', 'mail'],
['1', 'name1', 'mail1'],
['2', 'name1', 'mail2'],
['3', 'name3', 'mail3']
]
```

### example5 update
### Append a set of values to a sheet
```php
Sheets::sheet('Sheet 1')->range('A4')->update([['3', 'name3', 'mail3']]);
$values = Sheets::range('')->all();
// When we don't provide a specific range, the sheet becomes the default range
Sheets::sheet('Sheet 1')->append([['3', 'name3', 'mail3']]);
$values = Sheets::all();
[
['id', 'name', 'mail'],
['1', 'name1', 'mail1'],
Expand All @@ -152,19 +165,31 @@ $values = Sheets::range('')->all();
]
```

### example6 append
### Append a set of values with keys
```php
Sheets::sheet('Sheet 1')->range('')->append([['3', 'name3', 'mail3']]);
$values = Sheets::range('')->all();
// When providing an associative array, values get matched up to the headers in the provided sheet
Sheets::sheet('Sheet 1')->append([['name' => 'name4', 'mail' => 'mail4', 'id' => 4]]);
$values = Sheets::all();
[
['id', 'name', 'mail'],
['1', 'name1', 'mail1'],
['2', 'name1', 'mail2'],
['3', 'name3', 'mail3']
['3', 'name3', 'mail3'],
['4', 'name4', 'mail4'],
]
```

### example7 Query parameters
### Add a new sheet
```php
Sheets::spreadsheetByTitle($title)->addSheet('New Sheet Title');
```

### Deleting a sheet
```php
Sheets::spreadsheetByTitle($title)->deleteSheet('Old Sheet Title');
```

### Specifying query parameters
```php
$values = Sheets::sheet('Sheet 1')->majorDimension('DIMENSION_UNSPECIFIED')
->valueRenderOption('FORMATTED_VALUE')
Expand Down
6 changes: 4 additions & 2 deletions src/Concerns/SheetsProperties.php
Expand Up @@ -9,7 +9,8 @@ trait SheetsProperties
*/
public function spreadsheetProperties()
{
return $this->serviceSpreadsheets()
return $this->getService()
->spreadsheets
->get($this->spreadsheetId)
->getProperties()
->toSimpleObject();
Expand All @@ -20,7 +21,8 @@ public function spreadsheetProperties()
*/
public function sheetProperties()
{
$sheets = $this->serviceSpreadsheets()
$sheets = $this->getService()
->spreadsheets
->get($this->spreadsheetId, ['ranges' => $this->sheet])
->getSheets();

Expand Down
76 changes: 55 additions & 21 deletions src/Concerns/SheetsValues.php
Expand Up @@ -69,10 +69,7 @@ public function update(array $value, string $valueInputOption = 'RAW')

$batch->setData($valueRange);

$response = $this->serviceValues()
->batchUpdate($this->spreadsheetId, $batch);

return $response;
return $this->serviceValues()->batchUpdate($this->spreadsheetId, $batch);
}

/**
Expand All @@ -84,53 +81,90 @@ public function clear()

$clear = new \Google_Service_Sheets_ClearValuesRequest();

$response = $this->serviceValues()
->clear($this->spreadsheetId, $range, $clear);

return $response;
return $this->serviceValues()->clear($this->spreadsheetId, $range, $clear);
}

/**
* @param array $value
* @param array $values
* @param string $valueInputOption
* @param string $insertDataOption
*
* @return mixed|\Google_Service_Sheets_AppendValuesResponse
*/
public function append(array $value, string $valueInputOption = 'RAW', string $insertDataOption = 'OVERWRITE')
public function append(array $values, string $valueInputOption = 'RAW', string $insertDataOption = 'OVERWRITE')
{
$range = $this->ranges();
$orderedValues = $this->orderAppendables($values);

$valueRange = new \Google_Service_Sheets_ValueRange();
$valueRange->setValues($value);
$valueRange->setValues($orderedValues);
$valueRange->setRange($range);

$optParams = [
'valueInputOption' => $valueInputOption,
'insertDataOption' => $insertDataOption,
];

$response = $this->serviceValues()->append($this->spreadsheetId, $range, $valueRange, $optParams);
return $this->serviceValues()->append($this->spreadsheetId, $range, $valueRange, $optParams);
}

/**
* @param array $values
*
* @return array
*/
public function orderAppendables(array $values)
{
// The array has integer keys, so just append
if (! $this->isAssociatedArray($values[0])) {
return $values;
}
// The array has keys, which we want to map to headers and order
$header = $this->first();

$ordered = [];

return $response;
// Gets just the values of an array that has been re-ordered to match the header order
foreach ($values as $value) {
array_push($ordered, array_values(array_replace(array_flip($header), $value)));
}

return $ordered;
}

/**
* https://stackoverflow.com/a/173479/6646558.
*
* @param array $arr
*
* @return bool
*/
protected function isAssociatedArray(array $arr)
{
if ($arr === []) {
return false;
}

return array_keys($arr) !== range(0, count($arr) - 1);
}

/**
* @return string
*/
public function ranges()
{
// If no range is provided, we get the sheet automatically
if (! isset($this->range)) {
return $this->sheet;
}

// If we only provide part of the range, we get the full proper range
if (strpos($this->range, '!') === false) {
if (empty($this->range)) {
$ranges = $this->sheet;
} else {
$ranges = $this->sheet.'!'.$this->range;
}
} else {
$ranges = $this->range;
return $this->sheet.'!'.$this->range;
}

return $ranges;
// If we provide the full range, it returns accurately
return $this->range;
}

/**
Expand Down
19 changes: 5 additions & 14 deletions src/Sheets.php
Expand Up @@ -3,7 +3,6 @@
namespace Revolution\Google\Sheets;

use Google_Service_Sheets;
use Google_Service_Sheets_BatchUpdateSpreadsheetRequest;
use Illuminate\Container\Container;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
Expand Down Expand Up @@ -154,7 +153,7 @@ public function sheetList(): array
{
$list = [];

$sheets = $this->serviceSpreadsheets()->get($this->spreadsheetId)->getSheets();
$sheets = $this->getService()->spreadsheets->get($this->spreadsheetId)->getSheets();

foreach ($sheets as $sheet) {
$list[$sheet->getProperties()->getSheetId()] = $sheet->getProperties()->getTitle();
Expand All @@ -163,22 +162,14 @@ public function sheetList(): array
return $list;
}

/**
* @return \Google_Service_Sheets_Resource_Spreadsheets
*/
protected function serviceSpreadsheets()
{
return $this->getService()->spreadsheets;
}

/**
* @param string $sheetTitle
*
* @return Google_Service_Sheets_BatchUpdateSpreadsheetResponse
*/
public function addSheet(string $sheetTitle)
{
$body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
$body = new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => [
'addSheet' => [
'properties' => [
Expand All @@ -188,7 +179,7 @@ public function addSheet(string $sheetTitle)
],
]);

return $this->serviceSpreadsheets()->batchUpdate($this->spreadsheetId, $body);
return $this->getService()->spreadsheets->batchUpdate($this->spreadsheetId, $body);
}

/**
Expand All @@ -201,15 +192,15 @@ public function deleteSheet(string $sheetTitle)
$list = $this->sheetList();
$id = Arr::get(array_flip($list), $sheetTitle);

$body = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
$body = new \Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
'requests' => [
'deleteSheet' => [
'sheetId' => $id,
],
],
]);

return $this->serviceSpreadsheets()->batchUpdate($this->spreadsheetId, $body);
return $this->getService()->spreadsheets->batchUpdate($this->spreadsheetId, $body);
}

/**
Expand Down

0 comments on commit 2bbf2e3

Please sign in to comment.