PHP code example of gulaandrij / google-sheets-bundle

1. Go to this page and download the library: Download gulaandrij/google-sheets-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gulaandrij / google-sheets-bundle example snippets


return [
    // ...
    Gulaandrij\GoogleSheetsBundle\GoogleSheetsBundle::class => ['all' => true],
];

use Gulaandrij\GoogleSheetsBundle\Service\SheetsService;

final class AllocatorReport
{
    public function __construct(
        private readonly SheetsService $allocators,   // → bound to spreadsheets.allocators
        private readonly SheetsService $reports,      // → bound to spreadsheets.reports
    ) {}

    public function run(): void
    {
        $rows = $this->allocators->readAssoc('Allocator List');
        // $rows = [['Name' => 'Alice', 'Email' => '[email protected]'], ...]

        $this->allocators->append('Allocator List', [
            ['Name' => 'Bob', 'Email' => '[email protected]'],
        ]);

        $this->reports->update('Daily', 'A1', [['Date', 'Count'], ['2026-06-07', 42]]);
    }
}

SheetsService::readRaw(?string $sheetName = null, ?string $range = null, ?string $majorDimension = null, ?string $valueRenderOption = null, ?string $dateTimeRenderOption = null): array
SheetsService::readAssoc(?string $sheetName = null, ?string $range = null, ?string $majorDimension = null, ?string $valueRenderOption = null, ?string $dateTimeRenderOption = null): array
SheetsService::firstRow(?string $sheetName = null, ?string $range = null, ?string $valueRenderOption = null, ?string $dateTimeRenderOption = null): array

SheetsService::append(array $rows, ?string $sheetName = null, string $valueInputOption = 'RAW', string $insertDataOption = 'OVERWRITE'): AppendValuesResponse
SheetsService::update(string $range, array $values, ?string $sheetName = null, string $valueInputOption = 'RAW'): BatchUpdateValuesResponse
SheetsService::clear(?string $sheetName = null, ?string $range = null): ?ClearValuesResponse

SheetsService::addSheet(string $title): BatchUpdateSpreadsheetResponse
SheetsService::deleteSheet(string $title): BatchUpdateSpreadsheetResponse

SheetsService::listSheets(): array
SheetsService::listSheetsWithIds(): array
SheetsService::findSheetNameById(int $sheetId): ?string

SheetsClientFactory::listSpreadsheets(): array     // global Drive query — lives on the factory, not a bound SheetsService

SheetsService::spreadsheetProperties(): object
SheetsService::sheetProperties(?string $sheetName = null): object

SheetsService::client(): SheetsClient
SheetsService::driveService(): \Google\Service\Drive
SheetsService::getSpreadsheetId(): string
SheetsService::getBoundSheet(): ?string

use Gulaandrij\GoogleSheetsBundle\Attribute\SheetColumn;

final class Allocator
{
    #[SheetColumn('Record ID - Contact')]
    public ?string $contactId = null;

    #[SheetColumn('First Name')]
    public ?string $firstName = null;

    #[SheetColumn('Email')]
    public ?string $email = null;
}

// In your service:
$allocators = $this->allocators->readEntities(Allocator::class);
// returns Allocator[] — the serializer denormalizes each row.

foreach ($this->reports->readAssocIterable(batchSize: 1000) as $row) {
    $this->process($row);
}

use Gulaandrij\GoogleSheetsBundle\Test\InMemorySheetsService;

// tests/Functional/AllocatorImportTest.php
protected function setUp(): void
{
    parent::setUp();
    self::getContainer()->set(
        'google_sheets.sheets_service.hubspot_allocators',
        new InMemorySheetsService([
            'Allocator List' => [
                ['Record ID - Contact', 'First Name', 'Email'],
                ['c-1', 'Alice', '[email protected]'],
            ],
        ], boundSheet: 'Allocator List'),
    );
}