merceslab / spreadsheet-client
用于与 Google 在线电子表格交互的客户端抽象
Requires
- php: ^7.1.3
- google/apiclient: ^2.2
Requires (Dev)
- phpunit/phpunit: ^7.4
This package is auto-updated.
Last update: 2024-09-09 04:35:10 UTC
README
composer require merces-lab/spreadsheet-client
用法
您需要 Google 服务帐户才能使用此组件中的客户端。以下步骤的参考,请参阅 Google 文档: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
如果您还没有可以为您的应用程序使用的服务帐户
- 前往 Google 开发者控制台: https://console.developers.google.com
- 为您的应用程序创建一个项目(右上角靠近 Google APIs 标志)
- 选择您的项目
- 转到“启用 API 和服务”(屏幕顶部)并启用“Google Sheets API”
- 转到“凭证”(左侧菜单)
- 在“创建凭证”下拉菜单中,选择“服务帐户密钥”
- 为服务帐户选择一个名称。
- 服务帐户不需要角色即可访问 Google Sheets。
- 选择“JSON”作为“密钥类型”
- 您将需要下载凭证文件。将该文件保存到您的磁盘。
- 保留您应重定向到的凭证页面打开。您需要在下一步中复制您刚刚创建的服务帐户的 ID。
授权您的服务帐户
- 前往管理员控制台: http://admin.google.com/
- 转到安全 > 高级设置 > 管理API客户端访问
- 输入您的服务帐户 ID 作为客户端名称,并输入 https://www.googleapis.com/auth/spreadsheets API作用域
- 点击授权
您现在应该有一个经过授权的服务帐户可以访问 Google Sheets API。您需要将您想要从其导入数据或将数据导出到其中的文档与您刚刚创建的服务帐户共享。要授予您的服务帐户对文档的访问权限
- 转到开发者控制台中的凭证页面
- 转到“管理服务帐户”
- 复制您的服务帐户的电子邮件地址
- 转到您的 Google 电子表格文档
- 点击“共享”
- 在“邀请人员”字段中,输入您的服务帐户的电子邮件地址
- 取消选中“通知人员”
- 点击“发送”
Google_Service_Sheets
要实例化 \Google_Service_Sheets,您需要创建服务帐户步骤中提供的 JSON 文件的凭证。您可以将该文件放在您的服务器文件系统中的任何位置,或者将内容放在环境变量中。
\MercesLab\Component\SpreadsheetClient\ClientFactory 辅助类将为您创建 Google 服务。您需要传递凭证,无论是作为数组还是作为 JSON 字符串。
此组件中的类依赖于 \Google_Service_Sheets。
<?php $credentials = file_get_contents('/var/secrets/credentials.json'); // Get the content of the JSON credentials file generated by Google (either from a file or via an environment variable eg. $credentials = getenv('GOOGLE_CREDENTIALS')) $googleServiceSheets = \MercesLab\Component\SpreadsheetClient\Google\GoogleFactory::createServiceSheets($credentials);
客户端
客户端只依赖于 GoogleServiceSheets 并实现了 write() 和 read() 方法。如果您需要同时访问多个 Google 电子表格文档,则应使用客户端。
读取和写入操作需要文件 ID、可选的表名称和范围
<?php /** @var \Google_Service_Sheets $googleServiceSheets */ $client = \MercesLab\Component\SpreadsheetClient\Google\GoogleFactory::createClient($googleServiceSheets); $rows = $client->read( 'fileId', /* ID of the google document */ 'Sheet1', /* Name of the sheet where your table is (default Sheet1) */ 'A1:H1', /* Range of the table header row (default A1) */ 0, /* Offset - number of rows to be ignored at the start of the table (default 0) */ 100 /* Limit - maximum number of rows to return (default -1 = unlimited) */ ); foreach ($rows as $row) { $myData = $row[0]; // Process $row which is a sequential array } $data = ['foo', 'bar',]; $client->write($data, 'fileId', 'Sheet1', 'A1:H1'); // range is optional, and should match the headers of the table into which you want to export your data. Leaving it blank lets Google auto determine where your data should go (which is fine if you only have one table in the spreadsheet)
文件
该文件依赖于 GoogleServiceSheets 和 fileId。它还实现了 write() 和 read() 方法。如果您需要在多个服务中访问相同的电子表格文档,则应使用文件。
读写操作需要可选的工作表名称和表格范围
<?php /** @var \Google_Service_Sheets $googleServiceSheets */ $file = \MercesLab\Component\SpreadsheetClient\Google\GoogleFactory::createFile($googleServiceSheets, 'myFileId'); $rows = $file->read( 'Sheet1', /* Name of the sheet where your table is (default Sheet1) */ 'A1:H1', /* Range of the table header row (default A1) */ 0, /* Offset - number of rows to be ignored at the start of the table (default 0) */ 100 /* Limit - maximum number of rows to return (default -1 = unlimited) */ ); foreach ($rows as $row) { $myData = $row[0]; // Process $row which is a sequential array } $data = ['foo', 'bar',]; $file->write($data, 'Sheet1', 'A1:H1'); // range is optional, and should match the headers of the table into which you want to export your data. Leaving it blank lets Google auto determine where your data should go (which is fine if you only have one table in the spreadsheet)
工作表
工作表依赖于GoogleServiceSheets、fileId和可选的工作表名称。它还实现了write()和read()方法。如果您需要在多个服务中访问同一工作表,应使用该工作表。
读写操作需要可选的表格范围
<?php /** @var \Google_Service_Sheets $googleServiceSheets */ $sheet = \MercesLab\Component\SpreadsheetClient\Google\GoogleFactory::createSheet($googleServiceSheets, 'myFileId', 'mySheet'); $rows = $sheet->read( 'A1:H1', /* Range of the table header row (default A1) */ 0, /* Offset - number of rows to be ignored at the start of the table (default 0) */ 100 /* Limit - maximum number of rows to return (default -1 = unlimited) */ ); foreach ($rows as $row) { $myData = $row[0]; // Process $row which is a sequential array } $data = ['foo', 'bar',]; $sheet->write($data, 'A1:H1'); // range is optional, and should match the headers of the table into which you want to export your data. Leaving it blank lets Google auto determine where your data should go (which is fine if you only have one table in the spreadsheet)
文件
表格依赖于GoogleServiceSheets、fileId、可选的工作表名称和范围。它还实现了write()和read()方法。如果您需要在多个服务中访问同一表格,应使用该文件。
<?php /** @var \Google_Service_Sheets $googleServiceSheets */ $table = \MercesLab\Component\SpreadsheetClient\Google\GoogleFactory::createTable($googleServiceSheets, 'myFileId', 'mySheet', 'A1:H1'); $rows = $table->read( 0, /* Offset - number of rows to be ignored at the start of the table (default 0) */ 100 /* Limit - maximum number of rows to return (default -1 = unlimited) */ ); foreach ($rows as $row) { $myData = $row[0]; // Process $row which is a sequential array } $data = ['foo', 'bar',]; $table->write($data);