68 lines
2.2 KiB
PHP
68 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Google_Client;
|
|
use Google_Service_Sheets;
|
|
use Google_Service_Sheets_Sheet;
|
|
|
|
class GoogleSheetService
|
|
{
|
|
/**
|
|
* Create a new class instance.
|
|
*/
|
|
protected $client;
|
|
protected $service;
|
|
protected $spreadsheetID;
|
|
public function __construct()
|
|
{
|
|
$this->client = new Google_Client();
|
|
$this->client->setApplicationName("Sibedas Google Sheets API");
|
|
$this->client->setScopes([Google_Service_Sheets::SPREADSHEETS_READONLY]);
|
|
$this->client->setAuthConfig(storage_path("app/teak-banner-450003-s8-ea05661d9db0.json"));
|
|
$this->client->setAccessType("offline");
|
|
|
|
$this->service = new Google_Service_Sheets($this->client);
|
|
$this->spreadsheetID = env("SPREAD_SHEET_ID");
|
|
|
|
$this->service_sheets = new Google_Service_Sheets($this->client);
|
|
}
|
|
|
|
public function getSheetData($range){
|
|
$response = $this->service->spreadsheets_values->get($this->spreadsheetID, $range);
|
|
return $response->getValues();
|
|
}
|
|
|
|
public function getLastRow($sheetName = "Sheet1"){
|
|
$spreadsheet = $this->service->spreadsheets->get($this->spreadsheetID);
|
|
$sheets = $spreadsheet->getSheets();
|
|
foreach ($sheets as $sheet) {
|
|
if ($sheet->getProperties()->getTitle() === $sheetName) {
|
|
return [
|
|
'rowCount' => $sheet->getProperties()->getGridProperties()->getRowCount(),
|
|
'columnCount' => $sheet->getProperties()->getGridProperties()->getColumnCount(),
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
public function getHeader($sheetName = "Sheet1", $range = "Sheet1!1:1"){
|
|
$response = $this->service->spreadsheets_values->get($this->spreadsheetID, $range);
|
|
$values = $response->getValues();
|
|
return !empty($values) && isset($values[0]) ? $values[0] : [];
|
|
}
|
|
|
|
public function getLastColumn($sheetName = 'Sheet1')
|
|
{
|
|
$range = "{$sheetName}!1:1";
|
|
$response = $this->service->spreadsheets_values->get($this->spreadsheetID, $range);
|
|
$values = $response->getValues();
|
|
|
|
if (!empty($values) && isset($values[0])) {
|
|
return count($values[0]);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|