8000 GitHub - aarishgilani/google-sheets-api-integration: Laravel backend integrated to export data and periodically update google sheets to serve as an intermediate data layer for looker studio analysis · GitHub
[go: up one dir, main page]

Skip to content

aarishgilani/google-sheets-api-integration

Repository files navigation

Google Sheets API Integration for Looker Studio

Laravel application for syncing GHG Calculator data to Google Sheets for visualization in Google Looker Studio.

Overview

This project implements a data bridge from Laravel to Google Sheets, enabling Google Looker Studio dashboards for GHG Calculator analytics. The integration uses the Google API PHP Client to securely sync data from your Laravel database to a Google Spreadsheet.

Features

  • ✅ Automated data sync from Laravel to Google Sheets
  • ✅ Service account authentication (secure, no user interaction required)
  • ✅ Flexible sync frequency (hourly, daily, weekly)
  • ✅ Full or incremental sync options
  • ✅ Automatic sheet tab creation
  • ✅ Header row formatting
  • ✅ Error handling and logging
  • ✅ Laravel scheduled tasks integration

Project Structure

/app
  /Console/Commands
    SyncToGoogleSheets.php       # Artisan command for syncing data
  /Models
    Calculation.php               # GHG calculation model
    Category.php                  # Emission category model
  /Services
    GoogleSheetsService.php       # Google Sheets API service
/config
  google-sheets.php               # Configuration file
/database/migrations
  create_calculations_table.php   # Calculations table migration
  create_categories_table.php     # Categories table migration

Installation

  1. Install dependencies:

    composer install
  2. Configure environment:

    • Copy .env.example to .env
    • Add Google Sheets configuration (see SETUP.md)
  3. 8000
  4. Run migrations:

    php artisan migrate
  5. Set up Google Service Account:

    • See SETUP.md for detailed instructions

Usage

Manual Sync

Sync data to Google Sheets:

php artisan sheets:sync

Perform a full sync (clears existing data first):

php artisan sheets:sync --full

Scheduled Sync

The sync is automatically scheduled based on your GOOGLE_SHEETS_SYNC_FREQUENCY setting:

  • hourly - Runs every hour
  • daily - Runs once per day
  • weekly - Runs once per week

Ensure your cron job is configured:

* * * * * cd /path-to-project && php artisan schedule:run >> /dev/null 2>&1

Configuration

Environment Variables

GOOGLE_SHEETS_SERVICE_ACCOUNT_JSON=storage/app/google-credentials.json
GOOGLE_SHEETS_SPREADSHEET_ID=your_spreadsheet_id
GOOGLE_SHEETS_SHEET_NAME=Main Data
GOOGLE_SHEETS_SYNC_FREQUENCY=hourly

Configuration File

Configuration options are available in config/google-sheets.php.

Data Structure

The sync exports the following fields:

  • Calculation ID
  • Date
  • CO2e Result
  • Category
  • User Type
  • Status
  • User ID
  • Created At
  • Updated At

Architecture

Laravel Database
    ↓
GoogleSheetsService
    ↓
Google Sheets API (google/apiclient)
    ↓
Google Spreadsheet
    ↓
Google Looker Studio Dashboard

Development

Adding New Data Fields

To add new fields to the sync:

  1. Update the transformDataForSheets() method in SyncToGoogleSheets.php
  2. Add the new column to the header row
  3. Include the data in the data rows

Customizing Sync Logic

You can modify the sync logic in:

  • app/Services/GoogleSheetsService.php - API interactions
  • app/Console/Commands/SyncToGoogleSheets.php - Data transformation and sync logic

Testing

Test the Google Sheets service:

php artisan tinker
$service = app(\App\Services\GoogleSheetsService::class);
$data = $service->getSheetData('Main Data!A1:Z10');

Troubleshooting

See SETUP.md for detailed troubleshooting steps.

Common issues:

  • Service account credentials not found
  • Spreadsheet access denied
  • API not enabled in Google Cloud Console

Documentation

Security

  • Service account credentials should never be committed to version control
  • Use least-privilege access for service accounts
  • Regularly rotate service account keys
  • Monitor API usage

License

This project is for internal use by the Métis Nation of Ontario (MNO) and Vincent Design.

About

Laravel backend integrated to export data and periodically update google sheets to serve as an intermediate data layer for looker studio analysis

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

0