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,61 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Services\GoogleSheetService;
use Illuminate\Http\Request;
class GoogleSheetController extends Controller
{
protected $googleSheetService;
public function __construct(GoogleSheetService $googleSheetService){
$this->googleSheetService = $googleSheetService;
}
/**
* Display a listing of the resource.
*/
public function index()
{
$data = $this->googleSheetService->getSheetData("Sheet1!A2:BB2");
$result = [
"data" => $data,
"last_row" => $this->googleSheetService->getLastRow("Sheet1"),
"header" => $this->googleSheetService->getHeader("Sheet1"),
"last_column" => $this->googleSheetService->getLastColumn("Sheet1")
];
return response()->json($result);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}

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;
}
}