nebbo.o/laravel-sheet-parser
最新稳定版本:v2.0.1
Composer 安装命令:
composer require nebbo.o/laravel-sheet-parser
包简介
A Laravel package to parse data from public Google Sheets and CSV files using a unified interface.
README 文档
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
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-24