• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

kawax/laravel-google-sheets

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

kawax/laravel-google-sheets

开源软件地址(OpenSource Url):

https://github.com/kawax/laravel-google-sheets

开源编程语言(OpenSource Language):

PHP 100.0%

开源软件介绍(OpenSource Introduction):

Google Sheets API v4 for Laravel

packagist Maintainability Test Coverage

Requirements

  • PHP >= 8.0
  • Laravel >= 8.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. This package depends on https://github.com/pulkitjalan/google-apiclient

  2. Run php artisan vendor:publish --provider="PulkitJalan\Google\GoogleServiceProvider" --tag="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),
    
  3. Get API Credentials from https://developers.google.com/console
    Enable Google Sheets API, Google Drive API.

  4. 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 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

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

$header = $rows->pull(0);
$values = Sheets::collection($header, $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

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

Updating a specific range

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

// 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

// 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

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

Deleting a sheet

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

Specifying query parameters

$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

$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




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
laravel/art: Laravel logo and other artwork.发布时间:2022-07-09
下一篇:
digitalocean/sample-laravel: ⛵ App Platform sample Laravel application.发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap