mach3 / google-spreadsheet
Google电子表格客户端
v1.1.0
2019-11-29 11:36 UTC
Requires
- google/apiclient: 2.*
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);
用法
初始化表单(>=1.1.0)
目标表单必须是空的
$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;
更新单元格(>=1.1.0)
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)