Files
gravity/อนุมัติเดินทาง-ประชุม(ธุรการ)/src/Services/GoogleSheetsService.php
T
2026-09-16 23:20:08 +07:00

48 lines
1.6 KiB
PHP

<?php
namespace App\Services;
class GoogleSheetsService
{
private string $spreadsheetId;
private string $apiKey; // or Service Account JSON path
public function __construct(string $spreadsheetId)
{
$this->spreadsheetId = $spreadsheetId;
// In a real scenario, API Key or OAuth JSON should be loaded from env or DB
$this->apiKey = 'DUMMY_API_KEY';
}
/**
* Fetch data from a specific range using Google Sheets API v4
*/
public function fetchData(string $range): array
{
// Mock implementation
// The real implementation would use file_get_contents or cURL:
// $url = "https://sheets.googleapis.com/v4/spreadsheets/{$this->spreadsheetId}/values/{$range}?key={$this->apiKey}";
return [
['DOC001', 'ประชุมวิชาการประจำปี', 'เพื่อพัฒนาศักยภาพ', 'กรุงเทพมหานคร', '2023-11-01', '2023-11-05', '5000'],
['DOC002', 'อบรมหลักสูตร AI', 'เรียนรู้เทคโนโลยีใหม่', 'เชียงใหม่', '2023-12-10', '2023-12-12', '8000'],
];
}
/**
* Parse raw data and map it to DB structure based on column mapping
*/
public function parseData(array $rawData, array $mapping): array
{
$parsed = [];
foreach ($rawData as $row) {
$entry = [];
foreach ($mapping as $index => $field) {
$entry[$field] = $row[$index] ?? null;
}
$parsed[] = $entry;
}
return $parsed;
}
}