nebbo.o / laravel-sheet-parser
A Laravel package to parse data from public Google Sheets and CSV files using a unified interface.
Package info
github.com/nebojsha-mitikj/laravel-sheet-parser
pkg:composer/nebbo.o/laravel-sheet-parser
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.9
- illuminate/support: ^10.0 || ^11.0 || ^12.0
Requires (Dev)
- orchestra/testbench: ^8.0 || ^9.0
- phpunit/phpunit: ^10.0
README
A Laravel package to parse data from CSV files and public Google Sheets using a unified interface.
Features
- Parse CSV files or public Google Sheets seamlessly.
- Consistent API for both data sources.
- Convert to arrays, JSON, or Laravel collections.
- Retrieve rows, columns, headers, or specific data points.
Installation
Require the package via Composer:
composer require nebbo.o/laravel-sheet-parser
Usage
1. Parsing CSV files
use NebboO\LaravelSheetParser\SheetParser; $parser = SheetParser::fromCsv(storage_path('data/example.csv')); $data = $parser->toArray(); // Convert to array $json = $parser->toJson(); // Convert to JSON $collection = $parser->toCollection(); // Convert to collection
2. Parsing Google Sheets
Make sure the sheet is publicly accessible (Anyone with the link can view).
use NebboO\LaravelSheetParser\SheetParser; $url = 'https://docs.google.com/spreadsheets/d/your-sheet-id/edit?usp=sharing'; $parser = SheetParser::fromGoogleSheet($url); $data = $parser->toArray(); // Convert to array $json = $parser->toJson(); // Convert to JSON $collection = $parser->toCollection(); // Convert to collection
API Reference
Both CsvParser and GoogleSheetParser implement the same ParserInterface.
Methods
| Method | Description |
|---|---|
toArray(): array |
Returns the data as an array of associative arrays (using headers as keys). |
toJson(): string |
Returns the data as JSON. |
toCollection(): \Illuminate\Support\Collection |
Returns the data as a Laravel collection. |
headers(): array |
Returns the headers row. |
count(): int |
Returns the number of data rows (excluding headers). |
row(int $index): ?array |
Returns a specific row by index. |
column(string $header): array |
Returns all values from a specific column. |
first(): ?array |
Returns the first row of data. |
last(): ?array |
Returns the last row of data. |
hasHeader(string $header): bool |
Checks if a given header exists. |
Error Handling
The package provides custom exceptions you can catch to handle errors gracefully:
-
FileNotFoundException– CSV file does not exist. -
FileNotReadableException– CSV file cannot be read. -
InvalidFileTypeException– File is not a CSV. -
InvalidGoogleSheetUrlException– Invalid or missing Google Sheet URL. -
GoogleSheetDownloadException– Failed to fetch or parse Google Sheet data.
Requirements
PHP8.1+Laravel10, 11, or 12
License
This package is open-sourced software licensed under the MIT license