Php 使用Laravel通过API检索Google电子表格中的所有行
我试图通过使用以下库从Google电子数据表中获取数据:,我按照本教程配置API的设置,没有任何错误,但一旦我尝试实现上面链接中提供的任何示例,它就会显示一些错误: 示例1(在其链接上): 有人可以指导我如何从电子表格中获取数据或解决此处考虑的问题。您可以使用软件包完成任务 要安装此软件包,请添加 “google/apiclient”:“^2.0” 转到composer.json并运行composer update命令 请按照教程了解更多信息 安装包后,需要在config文件夹中的.env文件和constants.php文件中设置值 这里我使用了一个sheet name变量。如果省略此项,则将使用电子表格中的第一张表(选项卡) 示例代码:Php 使用Laravel通过API检索Google电子表格中的所有行,php,laravel,laravel-5,laravel-5.2,google-api-php-client,Php,Laravel,Laravel 5,Laravel 5.2,Google Api Php Client,我试图通过使用以下库从Google电子数据表中获取数据:,我按照本教程配置API的设置,没有任何错误,但一旦我尝试实现上面链接中提供的任何示例,它就会显示一些错误: 示例1(在其链接上): 有人可以指导我如何从电子表格中获取数据或解决此处考虑的问题。您可以使用软件包完成任务 要安装此软件包,请添加 “google/apiclient”:“^2.0” 转到composer.json并运行composer update命令 请按照教程了解更多信息 安装包后,需要在config文件夹中的.env文件和
namespace App\Utilities;
use App\Exceptions\LogExceptions;
use AWS;
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
use Config;
class SpreadSheetManagemnt{
private $CLIENT;
private $SERVICE;
private $SPREAD_SHEET;
private $SPREAD_SHEET_ID;
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
public function __construct() {
$this->CLIENT = new \Google_Client();
$this->CLIENT->setApplicationName("My Sample App");
$this->CLIENT->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$this->CLIENT->setAccessType('offline');
$this->CLIENT->setAuthConfig(Config::get('constants.SPREAD_SHEET_AUTH_FILE'));
$this->SPREAD_SHEET_ID = Config::get('constants.SPREAD_SHEET_ID');
$this->SERVICE = new \Google_Service_Sheets($this->CLIENT);
$this->SPREAD_SHEET = $this->SERVICE->spreadsheets->get($this->SPREAD_SHEET_ID);
}
/**
* Function to show sheet datas
* @param string sheet name
* @return boolean
*/
public function showDatas($sheetName)
{
try{
// column ranges in spreadsheet
$range = "'".$sheetName."'!A2:F3";
$rows = $this->SERVICE->spreadsheets_values->get($this->SPREAD_SHEET_ID, $range, ['majorDimension' => 'ROWS']);
dd($rows['values']);
}catch( \Throwable $e){
LogExceptions::writeLog($e);
}
}
}
要从google电子表格中检索所有行,只需从范围中删除单元格编号
$getrange = 'A:E';
$data = $service->spreadsheets_values->get($spreadsheetId, $getrange);
$values = $data->getValues();
if (empty($values)) {
print "No data found.\n";
} else {
echo "<pre>";
print_r($values);
}
$getrange='A:E';
$data=$service->spreadsheets\u values->get($spreadsheetId,$getrange);
$values=$data->getValues();
if(空($value)){
打印“未找到数据。\n”;
}否则{
回声“;
打印(价值);
}
我想你错过了src
files我也更新了我的链接;)如何获取一行中的所有值。但在连续的一段时间内可能会有多个细胞。是否有任何函数可以从google工作表中获取一行中的所有值?因为在这里我可以看到列名是硬编码的。
<?php
return [
/*
|----------------------------------------------------------------------------
| Google application name
|----------------------------------------------------------------------------
*/
'application_name' => env('GOOGLE_APPLICATION_NAME', ''),
/*
|----------------------------------------------------------------------------
| Google OAuth 2.0 access
|----------------------------------------------------------------------------
|
| Keys for OAuth 2.0 access, see the API console at
| https://developers.google.com/console
|
*/
'client_id' => env('GOOGLE_CLIENT_ID', ''),
'client_secret' => env('GOOGLE_CLIENT_SECRET', ''),
'redirect_uri' => env('GOOGLE_REDIRECT', ''),
'scopes' => [],
'access_type' => 'online',
'approval_prompt' => 'auto',
/*
|----------------------------------------------------------------------------
| Google developer key
|----------------------------------------------------------------------------
|
| Simple API access key, also from the API console. Ensure you get
| a Server key, and not a Browser key.
|
*/
'developer_key' => env('GOOGLE_DEVELOPER_KEY', ''),
/*
|----------------------------------------------------------------------------
| Google service account
|----------------------------------------------------------------------------
|
| Set the credentials JSON's location to use assert credentials, otherwise
| app engine or compute engine will be used.
|
*/
'service' => [
/*
| Enable service account auth or not.
*/
'enable' => env('GOOGLE_SERVICE_ENABLED', false),
/*
| Path to service account json file
*/
'file' => env('GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION', '')
],
/*
|----------------------------------------------------------------------------
| Additional config for the Google Client
|----------------------------------------------------------------------------
|
| Set any additional config variables supported by the Google Client
| Details can be found here:
| https://github.com/google/google-api-php-client/blob/master/src/Google/Client.php
|
| NOTE: If client id is specified here, it will get over written by the one above.
|
*/
'config' => [],
];
GOOGLE_APPLICATION_NAME= Append Data
GOOGLE_CLIENT_ID= 995576272219-q9t67ufvehsqoctcq7g.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET= ow1kEPcuSEGmGIXZtl
GOOGLE_REDIRECT=
GOOGLE_DEVELOPER_KEY=
GOOGLE_SERVICE_ENABLED=
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION=
namespace App\Utilities;
use App\Exceptions\LogExceptions;
use AWS;
use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
use Config;
class SpreadSheetManagemnt{
private $CLIENT;
private $SERVICE;
private $SPREAD_SHEET;
private $SPREAD_SHEET_ID;
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
public function __construct() {
$this->CLIENT = new \Google_Client();
$this->CLIENT->setApplicationName("My Sample App");
$this->CLIENT->setScopes([\Google_Service_Sheets::SPREADSHEETS]);
$this->CLIENT->setAccessType('offline');
$this->CLIENT->setAuthConfig(Config::get('constants.SPREAD_SHEET_AUTH_FILE'));
$this->SPREAD_SHEET_ID = Config::get('constants.SPREAD_SHEET_ID');
$this->SERVICE = new \Google_Service_Sheets($this->CLIENT);
$this->SPREAD_SHEET = $this->SERVICE->spreadsheets->get($this->SPREAD_SHEET_ID);
}
/**
* Function to show sheet datas
* @param string sheet name
* @return boolean
*/
public function showDatas($sheetName)
{
try{
// column ranges in spreadsheet
$range = "'".$sheetName."'!A2:F3";
$rows = $this->SERVICE->spreadsheets_values->get($this->SPREAD_SHEET_ID, $range, ['majorDimension' => 'ROWS']);
dd($rows['values']);
}catch( \Throwable $e){
LogExceptions::writeLog($e);
}
}
}
$getrange = 'A:E';
$data = $service->spreadsheets_values->get($spreadsheetId, $getrange);
$values = $data->getValues();
if (empty($values)) {
print "No data found.\n";
} else {
echo "<pre>";
print_r($values);
}