ksenonda / my-g-s
Google 电子表格客户端
dev-main
2021-06-01 08:51 UTC
Requires
- google/apiclient: 2.*
This package is auto-updated.
Last update: 2024-09-29 06:08:55 UTC
README
PHP 的 Google 电子表格客户端。此客户端需要 'google/apiclient' 包。
开始使用
1. 获取密钥文件
- 登录 Google 开发者控制台
- 创建新项目
- 在项目中创建 服务帐户 凭据
- 下载 JSON 格式的密钥文件
2. 创建电子表格
- 在 Google Drive 中创建新的电子表格
- 授权密钥文件中找到的 'client_email' 电子邮件地址以读取和编辑。
- 保存地址栏中的 文件 ID。
3. 使用 PHP 访问
$client = Google_Spreadsheet::getClient('the/path/to/credential.json'); // Get the sheet instance by sheets_id and sheet name $sheet = $client->file('XXXxxxXXXXxxxXXXX')->sheet('Sheet1'); // Fetch data from remote (or cache) $sheet->fetch(); // Flush all rows in the sheet var_dump($sheet->items);
用法
初始化工作表
目标工作表必须是空的
$sheet->init(array( 'id', 'name', 'age', 'email', 'note' ));
选择行
// Array $items = $sheet->select(array( 'id' => '1' )); // Closure $items = $sheet->select(function($row){ return (int) $row['age'] < 30; });
插入新行
// Insert a new row $sheet->insert(array( 'name' => 'John', 'age' => 23, 'email' => 'john@example.com' )); // Get up-to-date items $items = $sheet->fetch(true)->items;
更新行
// Update rows selected by array $sheet->update( array( 'email' => 'tom@example.com' ), array( 'name' => 'Tom' ) ); // Update rows selected by closure $sheet->update( array( 'email' => 'tom@example.com' ), function($row){ return $row['name'] === 'Tom'; } ); // Get up-to-date items $items = $sheet->fetch(true)->items;
删除行
// delete rows selected by array $sheet->delete( array( 'name' => 'Tom' ) ); //delete rows selected by closure $sheet->delete( function($row){ return $row['name'] === 'Tom'; } ); // Get up-to-date items $items = $sheet->fetch(true)->items;
更新单元格
edit 方法允许您手动更新单元格的值
// Update `B2` cell $sheet->edit(2, 2, 'Tom'); // Update `C1:C4` cells $sheet->edit(3, 1, array(1, 'John', 23, 'john@example.com'));
获取最新的表格数据
// Pass `true` to ignore cache $items = $sheet->fetch(true)->items;
保存缓存选项
$sheet->config(array( 'cache' => true, 'cache_dir' => __DIR__ . '/cache', 'cache_expires' => 360 ));
需求
- google/apiclient (Apache License v2.0)