sem-soft/google-sheets

通过 Google 客户端库使用官方 Google Sheets API 的包装器

1.0.0 2019-05-25 14:16 UTC

This package is not auto-updated.

Last update: 2024-09-23 16:21:44 UTC


README

此库通过服务 Google 账户实现了写入 Google Sheets 文档的基本功能。

通过 composer.json 安装

"sem-soft/google-sheets": "~1.0.0"

用法

此库使用官方的 Google 客户端包

通过服务账户进行认证

在使用此库之前,我们必须配置基本 Google Auth 客户端对象,以便使用服务账户凭证文件。

$auth = new \Sem\GoogleSheets\Auth\ServiceAccountAuthenticator();
$client = $auth->setAuth(new \Google_Client(), '/path/to/Credentials-5c2688ed460a.json');

中间件对象

对于写入过程,我们必须实例化一些基本对象。Book 对象 - Google 电子表格的实体。Sheet 对象 - Google 电子表格工作表层的实体。这些对象实现了写入过程当前的工作表。

$book = new \Sem\GoogleSheets\Book('193J0l6pRREaQa5632PdD2sCioAJw5AxTV0TWnK0SNH7');
$sheet = new \Sem\GoogleSheets\Sheet('Лист 1');

写入器对象

写入器对象实现了将数据写入单元格、行和清除工作表中的范围的功能。此对象需要书籍和工作表对象。

$writer = new \Sem\GoogleSheets\Writer(
    $client,
    $book,
    $sheet
);

将数据写入书籍工作表的示例

$auth = new ServiceAccountAuthenticator();
$client = $auth->setAuth(new \Google_Client(), '/opt/Example-5c1111ed600a.json'));

$writer = new Writer(
    $client,
    new Book('193J0l6pRREaQa5632PdD2sCioAJw5AxTV0TWnK0SNH7'),
    new Sheet('Лист 1')
);
// Clear range
$writer->clearRange('A1:E100');
// Insert row of cells from A2
$writer->insertRow('A2', [
    'Раз',
    'Два',
    'Три'
]);
// Insert row of cells from A3
$writer->insertRow('A3', [
    'Четыри',
    'Пять',
    'Шесть'
]);
// Insert rows of cells from D column and 4 row
$writer->insertRows('D', 4, [
    [
        'Раз',
        'Два',
        'Три'
    ],
    [
        'Четыри',
        'Пять',
        'Шесть'
    ]
]);
// Set cell value
$writer->cell('D12', 'Привет!');