gekkone / table-data-accessors
访问表格数据的通用接口。包含csv和Google表格的实现
1.1.1
2024-01-24 10:11 UTC
Requires
- ext-mbstring: *
- google/apiclient: ^2.7
- onnov/detect-encoding: ^2.0
Requires (Dev)
- guzzlehttp/psr7: ^2.6
- phpunit/phpunit: ^9.6
README
PHP中访问表格数据的通用接口。包含csv和Google表格的实现
详细信息
提供扩展迭代器接口以访问表格数据。
src/TableIteratorInterface.php 行 7:11
interface TableIteratorInterface extends Iterator { public function fetchRowCount(bool $skipEmpty = false): int; public function currentField(string $field, $default = null); }
⚠️ 注意: 第一行的数据用作返回每个行关联数组中的键
代码示例
从CSV文件简单读取
use Gekkone\TdaLib\Accessor\Csv; use GuzzleHttp\Psr7\Stream; $accessor = new Csv( Csv\TableOptions::new(new Stream(fopen(__DIR__ . '/filename.csv', 'r'))) ); foreach ($accessor as $row => $fields) { // $fields = ['columnHeader' => mixed, ...] or [(int) columnIndex => mixed, ...] }
从Google表格简单读取
use Gekkone\TdaLib\Accessor\GoogleSheet; use Google\Client; use Google\Service\Sheets; $googleCline = new Client([ 'scopes' => Sheets::SPREADSHEETS_READONLY ]); // for read first sheet $accessor = new GoogleSheet( GoogleSheet\TableOptions::new(new Sheets($googleCline), 'spreadsheetId') ); // for read concreate sheet set sheetId, find it in url after '#gid=', // example https://docs.google.com/spreadsheets/d/<spreadsheetID>/edit#gid=1737163713) $accessor = new GoogleSheet( GoogleSheet\TableOptions::new(new Sheets($googleCline), 'spreadsheetId', 1737163713) ); foreach ($accessor as $row => $fields) { // $fields = ['columnHeader' => mixed, ...] or [(int) columnIndex => mixed, ...] }
本作品许可协议为 Creative Commons Attribution 4.0 International License