• Stars
    star
    359
  • Rank 118,537 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 8 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Google Sheets API v4 for Laravel

packagist Maintainability Test Coverage

Requirements

  • PHP >= 8.0
  • Laravel >= 9.0

Versioning

  • Basic : semver
  • Drop old PHP or Laravel version : +0.1. composer should handle it well.
  • Support only latest major version (master branch), but you can PR to old branches.

Installation

Composer

composer require revolution/laravel-google-sheets

Laravel

  1. Run php artisan vendor:publish --tag="google-config" to publish the google config file

     // config/google.php
    
     // OAuth
     'client_id'        => env('GOOGLE_CLIENT_ID', ''),
     'client_secret'    => env('GOOGLE_CLIENT_SECRET', ''),
     'redirect_uri'     => env('GOOGLE_REDIRECT', ''),
     'scopes'           => [\Google\Service\Sheets::DRIVE, \Google\Service\Sheets::SPREADSHEETS],
     'access_type'      => 'online',
     'approval_prompt'  => 'auto',
     'prompt'           => 'consent', //"none", "consent", "select_account" default:none
    
     // or Service Account
     'file'    => storage_path('credentials.json'),
     'enable'  => env('GOOGLE_SERVICE_ENABLED', true),
    
  2. Get API Credentials from https://developers.google.com/console
    Enable Google Sheets API, Google Drive API.

  3. Configure .env as needed

     GOOGLE_APPLICATION_NAME=
     GOOGLE_CLIENT_ID=
     GOOGLE_CLIENT_SECRET=
     GOOGLE_REDIRECT=
     GOOGLE_DEVELOPER_KEY=
     GOOGLE_SERVICE_ENABLED=
     GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=
    

Demo

Another Google API Series.

Usage

id name mail
1 name1 mail1
2 name2 mail2

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

Basic Laravel Usage

use Revolution\Google\Sheets\Facades\Sheets;

$user = $request->user();

$token = [
      'access_token'  => $user->access_token,
      'refresh_token' => $user->refresh_token,
      'expires_in'    => $user->expires_in,
      'created'       => $user->updated_at->getTimestamp(),
];

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

Basic Non-Laravel Usage

use Google\Client;
use Revolution\Google\Sheets\Sheets;

$client = new 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();

Get a sheet's values with the header as the key

use Revolution\Google\Sheets\Facades\Sheets;

// get() returns Laravel Collection
$rows = Sheets::sheet('Sheet 1')->get();

$header = $rows->pull(0);
$values = Sheets::collection(header: $header, rows: $rows);
$values->toArray()
// [
//   ['id' => '1', 'name' => 'name1', 'mail' => 'mail1'],
//   ['id' => '2', 'name' => 'name2', 'mail' => 'mail2']
// ]

Blade

@foreach($values as $value)
  {{ data_get($value, 'name') }}
@endforeach

Using A1 Notation

use Revolution\Google\Sheets\Facades\Sheets;

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

Updating a specific range

use Revolution\Google\Sheets\Facades\Sheets;

Sheets::sheet('Sheet 1')->range('A4')->update([['3', 'name3', 'mail3']]);
$values = Sheets::range('')->all();
// [
//   ['id', 'name', 'mail'],
//   ['1', 'name1', 'mail1'],
//   ['2', 'name1', 'mail2'],
//   ['3', 'name3', 'mail3']
// ]

Append a set of values to a sheet

use Revolution\Google\Sheets\Facades\Sheets;

// 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'],
//   ['2', 'name1', 'mail2'],
//   ['3', 'name3', 'mail3']
// ]

Append a set of values with keys

use Revolution\Google\Sheets\Facades\Sheets;

// 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'],
//   ['4', 'name4', 'mail4'],
// ]

Add a new sheet

use Revolution\Google\Sheets\Facades\Sheets;

Sheets::spreadsheetByTitle($title)->addSheet('New Sheet Title');

Deleting a sheet

use Revolution\Google\Sheets\Facades\Sheets;

Sheets::spreadsheetByTitle($title)->deleteSheet('Old Sheet Title');

Specifying query parameters

use Revolution\Google\Sheets\Facades\Sheets;

$values = Sheets::sheet('Sheet 1')->majorDimension('DIMENSION_UNSPECIFIED')
                                  ->valueRenderOption('FORMATTED_VALUE')
                                  ->dateTimeRenderOption('SERIAL_NUMBER')
                                  ->all();

https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get#query-parameters

Use original Google_Service_Sheets

use Revolution\Google\Sheets\Facades\Sheets;

$sheets->spreadsheets->...
$sheets->spreadsheets_sheets->...
$sheets->spreadsheets_values->...

Sheets::getService()->spreadsheets->...

see https://github.com/google/google-api-php-client-services/blob/master/src/Google/Service/Sheets.php

LICENSE

MIT
Copyright kawax

More Repositories

1

laravel-amazon-product-api

PHP
75
star
2

google-sheets-project

PHP
50
star
3

laravel-google-photos

PHP
29
star
4

laravel-mastodon-api

PHP
22
star
5

hugo-vue-mix

Vue
16
star
6

composer-update-action

GitHub Actions
PHP
15
star
7

laravel-line-sdk

PHP
14
star
8

self-ordering

ใ‚ชใƒผใƒ—ใƒณใ‚ฝใƒผใ‚นใฎใ‚ปใƒซใƒ•ใ‚ชใƒผใƒ€ใƒผใ‚ทใ‚นใƒ†ใƒ 
PHP
14
star
9

socialite-amazon

PHP
13
star
10

laravel-uikit

PHP
12
star
11

socialite-mastodon

PHP
11
star
12

laravel-google-searchconsole

PHP
10
star
13

laravel-line-project

PHP
7
star
14

salvager

PHP
6
star
15

laravel-pagination-bulma

Blade
6
star
16

laravel-notification-mastodon

PHP
5
star
17

socialite-discord

PHP
5
star
18

nostr-vercel-api

TypeScript
5
star
19

artisans

PHP
4
star
20

botman-chatwork

PHP
4
star
21

sail-db-backup

PHP
3
star
22

niconico

ใƒ‹ใ‚ณใƒ‹ใ‚ณๅ‹•็”ปAPI
PHP
3
star
23

laravel-paypay

PHP
3
star
24

arty

PHP
3
star
25

discord-manager

PHP
3
star
26

socialite-project

PHP
3
star
27

laravel-server-push

Laravel Server Push Middleware
PHP
2
star
28

socialite-qiita

PHP
2
star
29

composer-workflow

2
star
30

hatebu-delete

PHP
2
star
31

headless-chrome-google-login

JavaScript
2
star
32

packagist-bot

Yet Another Packagist Mirror
PHP
2
star
33

laravel-google-photos-project

PHP
2
star
34

laravel-zend-form

Laravel Zend Form
PHP
2
star
35

laravel-str-mixins

PHP
2
star
36

laravel-vercel-installer

PHP
1
star
37

self-ordering-theme-tablet

Blade
1
star
38

self-ordering-starter

PHP
1
star
39

laravel8-jetstream-inertia

PHP
1
star
40

socialite-wordpress

PHP
1
star
41

tootlog

PHP
1
star
42

fortify-bootstrap4

Blade
1
star
43

nuxt-socialite-demo

PHP
1
star
44

qiita

PHP
1
star
45

zenn

1
star
46

laravel-carsensor-api

sample
PHP
1
star
47

laravel-logger-project

PHP
1
star
48

laravel-logger-cwlogs

PHP
1
star
49

laravel-react-tutorial

PHP
1
star
50

laravel-logger-chatwork

PHP
1
star
51

socialite-chatwork

PHP
1
star
52

kawax

1
star
53

laravel-pagination-uikit

HTML
1
star
54

laravel-laminas-form

PHP
1
star
55

laravel-vuejs-tutorial

PHP
1
star