-
Notifications
You must be signed in to change notification settings - Fork 0
FAQ
Common questions and answers about the Google Sheets Helper library.
Google Sheets Helper is a PHP library that simplifies working with the Google Sheets API. It provides an easy-to-use interface for common operations like reading, writing, updating, and managing Google Spreadsheets.
The library requires PHP 7.4 or higher. This ensures compatibility with modern PHP features and the Google API client library.
Yes! The library is open source and released under the MIT License, which means you can use it freely in both personal and commercial projects.
The official Google API client provides low-level access to all Google APIs. This helper library wraps that client to provide:
- Simplified method names
- Built-in error handling
- Common operation shortcuts
- Better developer experience
You can still access the underlying Google client when you need advanced features.
composer require reandimo/google-sheets-helperMake sure you have:
- Run
composer installorcomposer update - Included the autoloader:
require __DIR__ . '/vendor/autoload.php'; - Used the correct namespace:
use reandimo\GoogleSheetsApi\Helper;
- Go to Google Cloud Console
- Create a new project or select existing one
- Enable the Google Sheets API
- Create credentials (Service Account recommended)
- Download the JSON file and rename it to
credentials.json
- Service Account: Best for server applications, automated scripts, and when you don't need user interaction
- OAuth 2.0: Best for user-facing applications where users need to authorize access to their own sheets
For most use cases, Service Account is recommended.
Check that:
- Your
credentials.jsonfile exists and is readable - The file path is correct (absolute or relative)
- File permissions allow PHP to read the file
You need to generate a token file first:
php ./vendor/reandimo/google-sheets-helper/firstauthFollow the interactive prompts to authenticate.
This means your token has expired. Run the firstauth script again:
php ./vendor/reandimo/google-sheets-helper/firstauthGoogle API tokens can expire. Try:
- Running
firstauthagain - Checking if your credentials file is still valid
- Verifying your Google Cloud project still has the API enabled
The spreadsheet ID is in the URL when you open your Google Sheet:
https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/edit#gid=0
The long string after /d/ and before /edit is your spreadsheet ID.
- RAW: Values are inserted exactly as provided (e.g., "=SUM(A1:A10)" stays as text)
- USER_ENTERED: Values are parsed as if typed by a user (e.g., "=SUM(A1:A10)" becomes a formula)
Use Google_Model::NULL_VALUE:
use Google_Model;
$sheets->appendSingleRow([
'John Doe',
'john@example.com',
Google_Model::NULL_VALUE, // Empty cell
'Developer'
]);Yes! Create multiple instances:
$sheet1 = new Helper();
$sheet1->setSpreadsheetId('spreadsheet-1-id');
$sheet2 = new Helper();
$sheet2->setSpreadsheetId('spreadsheet-2-id');Use the column notation:
$sheets->setSpreadsheetRange('A:A'); // Whole column A
$values = $sheets->get();Use the row notation:
$sheets->setSpreadsheetRange('1:1'); // Whole row 1
$values = $sheets->get();You need to set the spreadsheet ID first:
$sheets->setSpreadsheetId('your-spreadsheet-id');You need to set the range before operations:
$sheets->setSpreadsheetRange('A1:Z100');You need to set the worksheet name:
$sheets->setWorksheetName('Sheet1');Check that:
- The worksheet name is exactly correct (case-sensitive)
- The worksheet exists in your spreadsheet
- Your service account has access to the spreadsheet
This usually means:
- The range is empty
- The range is outside your data
- The worksheet name is incorrect
You've hit Google's API rate limits. Try:
- Implementing delays between requests
- Using batch operations
- Checking your Google Cloud quotas
- Waiting before making more requests
Always use try-catch blocks:
try {
$values = $sheets->get();
// Process data
} catch (Exception $e) {
// Handle error appropriately
error_log("Google Sheets error: " . $e->getMessage());
}-
Batch operations: Use
append()instead of multipleappendSingleRow()calls - Limit ranges: Only read/write the data you need
- Cache data: Store frequently accessed data locally
- Rate limiting: Add delays between API calls
Use environment variables:
putenv('credentialFilePath=path/to/credentials.json');
putenv('tokenPath=path/to/token.json');This keeps credentials out of your code and makes them easier to manage.
- Process in chunks: Read/write data in smaller ranges
- Use pagination: Process data page by page
- Background processing: Use queues for large operations
- Monitor quotas: Keep track of API usage
Yes! Use getService() or getClient():
$service = $sheets->getService();
$client = $sheets->getClient();
// Access advanced Google API methodsSet the value input option to "USER_ENTERED":
$sheets->setValueInputOption('USER_ENTERED');
$sheets->updateSingleCell('A1', '=SUM(B1:B10)');The helper library provides basic formatting. For advanced formatting, access the underlying service:
$service = $sheets->getService();
// Use Google's advanced formatting methodsThe library automatically handles:
- Strings
- Numbers
- Dates (as strings)
- Formulas (with USER_ENTERED option)
- Empty cells (with NULL_VALUE)
- GitHub Issues: Report bugs or request features
-
Stack Overflow: Tag questions with
google-sheets-apiandphp - Documentation: Check the Google Sheets API docs
- Check if it's already reported in GitHub Issues
- Create a new issue with:
- Clear description of the problem
- Steps to reproduce
- Error messages
- Your environment (PHP version, library version)
Absolutely! We welcome contributions:
- Fork the repository
- Make your changes
- Add tests if applicable
- Submit a pull request
Check the GitHub releases for version history and changes.
Still have questions? Open an issue on GitHub or ask on Stack Overflow with the tags google-sheets-api and php!