partial updates create service google sheet

This commit is contained in:
arifal
2025-02-05 13:38:21 +07:00
parent ff324014f6
commit 604e0d8479
6 changed files with 648 additions and 4 deletions

View File

@@ -0,0 +1,67 @@
<?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;
}
}