-
Notifications
You must be signed in to change notification settings - Fork 0
Quick Start
Renan Diaz edited this page Aug 22, 2025
·
1 revision
Get up and running with Google Sheets Helper in under 10 minutes! This guide covers the essential concepts and shows you how to perform basic operations.
Before starting, ensure you have:
- β Installation completed
- β Google Cloud Platform credentials set up
- β Authentication token generated
- β A Google Sheet to work with
<?php
require __DIR__ . '/vendor/autoload.php';
use reandimo\GoogleSheetsApi\Helper;// Set environment variables
putenv('credentialFilePath=' . __DIR__ . '/credentials.json');
putenv('tokenPath=' . __DIR__ . '/token.json');
// Create instance
$sheets = new Helper();$sheets = new Helper(
__DIR__ . '/credentials.json',
__DIR__ . '/token.json'
);// Set the spreadsheet ID (from the URL)
$sheets->setSpreadsheetId('1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms');
// Set the worksheet name
$sheets->setWorksheetName('Sheet1');
// Set the range to work with
$sheets->setSpreadsheetRange('A1:Z1000');<?php
try {
// Set up your sheet
$sheets->setSpreadsheetId('your-spreadsheet-id');
$sheets->setWorksheetName('Sheet1');
$sheets->setSpreadsheetRange('A1:C10');
// Get the values
$values = $sheets->get();
// Display the data
foreach ($values as $row) {
echo implode(' | ', $row) . "\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}<?php
try {
$value = $sheets->getSingleCellValue('B2');
echo "Value in B2: " . ($value ?: 'empty') . "\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}<?php
try {
$sheets->setSpreadsheetRange('A1:C1');
$newRow = ['John Doe', 'john@example.com', 'Developer'];
$result = $sheets->appendSingleRow($newRow);
if ($result >= 1) {
echo "Row added successfully!\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}<?php
try {
$sheets->setSpreadsheetRange('A1:C');
$newRows = [
['Jane Smith', 'jane@example.com', 'Designer'],
['Bob Johnson', 'bob@example.com', 'Manager'],
['Alice Brown', 'alice@example.com', 'Analyst']
];
$rowsAdded = $sheets->append($newRows);
echo "Added {$rowsAdded} rows!\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}<?php
try {
$update = $sheets->updateSingleCell('B5', 'Updated Value');
if ($update->getUpdatedCells() >= 1) {
echo "Cell updated successfully!\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}<?php
try {
$sheets->setSpreadsheetRange('A1:F5');
$newData = [
['Header1', 'Header2', 'Header3', 'Header4', 'Header5', 'Header6'],
['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6'],
['Data7', 'Data8', 'Data9', 'Data10', 'Data11', 'Data12'],
['Data13', 'Data14', 'Data15', 'Data16', 'Data17', 'Data18'],
['Data19', 'Data20', 'Data21', 'Data22', 'Data23', 'Data24']
];
$update = $sheets->update($newData);
echo "Updated {$update->getUpdatedCells()} cells!\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}<?php
try {
$sheets->setSpreadsheetRange('A1:Z100');
$result = $sheets->findCellByValue('John Doe');
if ($result) {
echo "Found 'John Doe' at cell: {$result['cell']}\n";
echo "Row: {$result['row']}, Column: {$result['column']}\n";
} else {
echo "Value not found.\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}<?php
try {
$worksheets = $sheets->getSpreadsheetWorksheets();
foreach ($worksheets as $worksheet) {
echo "Sheet: {$worksheet['title']} (ID: {$worksheet['id']})\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}<?php
try {
$newSheetId = $sheets->addWorksheet('New Sheet', 1000, 26);
echo "Created new worksheet with ID: {$newSheetId}\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}<?php
try {
$sheets->setSpreadsheetRange('A1:Z10');
// RGB values (Red, Green, Blue)
$sheets->colorRange([142, 68, 173]); // Purple
echo "Background color applied!\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}<?php
try {
$sheets->setSpreadsheetRange('A1:Z100');
$cleared = $sheets->clearRange();
if ($cleared) {
echo "Range cleared successfully!\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}Here's a complete working example that demonstrates the basic workflow:
<?php
require __DIR__ . '/vendor/autoload.php';
use reandimo\GoogleSheetsApi\Helper;
try {
// Setup
putenv('credentialFilePath=' . __DIR__ . '/credentials.json');
putenv('tokenPath=' . __DIR__ . '/token.json');
$sheets = new Helper();
$sheets->setSpreadsheetId('your-spreadsheet-id');
$sheets->setWorksheetName('Sheet1');
// Read existing data
$sheets->setSpreadsheetRange('A1:C10');
$existingData = $sheets->get();
echo "Found " . count($existingData) . " rows of existing data.\n";
// Add new data
$sheets->setSpreadsheetRange('A1:C1');
$newRow = ['New User', 'newuser@example.com', 'Tester'];
$sheets->appendSingleRow($newRow);
echo "Added new row.\n";
// Update a cell
$sheets->updateSingleCell('B2', 'Updated Email');
echo "Updated cell B2.\n";
// Search for data
$sheets->setSpreadsheetRange('A1:C100');
$found = $sheets->findCellByValue('New User');
if ($found) {
echo "Found 'New User' at: {$found['cell']}\n";
}
echo "β
All operations completed successfully!\n";
} catch (Exception $e) {
echo "β Error: " . $e->getMessage() . "\n";
}Before any operation, ensure you've set:
$sheets->setSpreadsheetId('your-id');
$sheets->setSpreadsheetRange('A1:Z100');
$sheets->setWorksheetName('Sheet1');Always wrap your code in try-catch blocks:
try {
// Your code here
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}Control how values are interpreted:
// RAW = insert data as-is
$sheets->setValueInputOption('RAW');
// USER_ENTERED = parse as if user typed
$sheets->setValueInputOption('USER_ENTERED');Now that you have the basics down:
- Configuration - Learn about advanced configuration options
- Data Operations - Deep dive into reading and writing data
- Worksheet Management - Advanced worksheet operations
- Examples - More complex examples and use cases
- API Reference - Complete method documentation
- Start Simple: Begin with basic operations before moving to complex ones
- Test Small: Test with small ranges before working with large datasets
- Handle Errors: Always implement proper error handling
- Use Environment Variables: Keep credentials secure with environment variables
- Check Permissions: Ensure your service account has proper access to sheets