ondram/simple-google-reader

简单的 Google Spreadsheets 和 Google Docs 阅读器

1.0.0 2024-03-14 19:05 UTC

This package is auto-updated.

Last update: 2024-09-20 13:38:56 UTC


README

Latest Stable Version Coverage Status GitHub Actions Build Status

PHP 库,提供了一种简单的方法来从 Google Spreadsheets 和 Google Docs 加载数据。

目标是提供通用的低级数据访问,不包含任何额外的功能,如数据处理或格式化。这意味着您可以在应用程序中实现任何领域对象映射或数据处理。

这个库是对 google/apiclient 的封装,具有最小的额外依赖。它旨在轻松集成到任何框架或纯 PHP。

安装

使用 Composer 安装

$ composer require ondram/simple-google-reader

使用方法

  1. 获取您的项目服务帐户凭证
  2. 在 IAM 管理控制台中打开服务帐户详细信息,在密钥设置中添加 JSON 密钥。下载生成的带有凭证的 JSON 文件(例如,保存为 google_client.json)。
  3. 可选:您可以为服务帐户设置域范围委派访问。这是在 Google Workspace 管理中完成的。在这种情况下,服务帐户可以代表任何域用户。
  4. Google Cloud Console 中启用您项目所需的 API
  5. 与您的服务帐户(或如果您使用域范围委派,则与某个用户帐户)共享要读取的文档,复制文档 ID(从 URL 中获取)
  6. 确保安装任何实现 PSR-6 缓存的包 implementing PSR-6 caching
  7. 准备缓存并初始化 Google 客户端
<?php declare(strict_types=1);

use Cache\Adapter\Filesystem\FilesystemCachePool;
use Cocur\Slugify\Slugify;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use OndraM\SimpleGoogleReader\Spreadsheets\SpreadsheetsReader;

require_once __DIR__ . '/vendor/autoload.php';

// Create instance of Cache, in this case we use FilesystemCache
$cache = new FilesystemCachePool(new Filesystem(new Local(__DIR__ . '/data')));

// Instantiate the Google Client with your credentials
$client = new \Google\Client();
$client->setAuthConfig(__DIR__ . '/data/google_client.json');
// If you service account has domain-wide delegation access, you need to use setSubject to set the name of the user
// which will the service account impersonate. This user also must have right to access the spreadsheet.
$client->setSubject('foo@bar.cz');

// see below for spreadsheets and docs usage

读取电子表格

在 Google Cloud Console 中,不要忘记启用 Google Sheets API

// $client is the instance from above example

$client->addScope(\Google\Service\Sheets::SPREADSHEETS);

// Create instance of Slugify, needed for spreadsheets
$slugify = new Slugify(['rulesets' => ['default', 'czech']]);

$reader = new SpreadsheetsReader($client, $slugify, $cache);

$rows = $reader->readById('pasteHereGoogleSpreadsheedId', '[optional sheet name]'/*, optional cache TTL*/);
foreach ($rows as $row) {
    echo $row['first_column'];
    echo $row['second_column'];
}

对于电子表格,必须要求第一行包含列名。库将使用这些名称(转换为 slug)作为关联数组的键。考虑以下表格

这将按以下方式读取

[
    ['first_column' => 'Value 1', 'second_column' => 'Foo'],
    ['first_column' => 'Value 2', 'second_column' => 'Bar'],
]

空行将被跳过。目前(有意)限制为只能读取 A:Z 列。

读取文档

作为纯文本

在 Google Cloud Console 中,不要忘记启用 Google Docs API

// $client is the instance from above example

$client->addScope(\Google\Service\Docs::DOCUMENTS_READONLY);

$docsReader = new DocsReader($client, $cache);

$document = $docsReader->readAsPlaintext('pasteHereGoogleDocsId'/*, optional cache TTL*/);

这将读取整个文档作为纯文本。只包括文本元素,其他元素(如表格)将被忽略。此外,任何文档格式也将被忽略。

作为 HTML

要读取文档作为 HTML,不要忘记在 Google Cloud Console 中启用 Google Drive API

$client->addScope(\Google\Service\Docs::DRIVE_READONLY);

$html = $docsReader->readAsHtml('pasteHereGoogleDocsId'/*, optional cache TTL*/);

请注意输出将相当庞大 - HTML,具有许多内联样式和嵌套元素。可能需要应用一些 HTML 清洁器,如 symfony/html-sanitizer,以删除不需要的元素和属性。

测试

该库中的测试主要是集成测试,这意味着它们需要真实的Google API访问。要运行它们,您必须下载并存储您的服务帐户的JSON凭证到tests/google_client.json文件。

然后,测试使用此表格此文档来读取示例数据。

$ composer test

变更日志

要查看最新更改,请参阅CHANGELOG.md文件。本项目遵循语义版本控制