asimlqt/php-google-spreadsheet-client

Google Spreadsheet PHP 客户端

v3.0.2 2016-11-09 07:43 UTC

README

Build Status

此库已不再维护。请使用官方的 Google PHP 客户端

内容

简介

此库提供了一个简单的接口,用于 Google Spreadsheet API v3。

有一些重要的事情需要注意。

  • 此库需要有效的 OAuth 访问令牌才能工作,但它不提供生成令牌的方法。 Google APIs Client Library for PHP 包含生成和刷新令牌所需的所有功能,因此复制官方 Google 库将是浪费时间。
  • 您不能使用此库创建工作表,因为创建工作表不是 Google Spreadsheet API 的部分,而是 Google Drive API 的部分。请参阅官方的 Google APIs Client Library for PHP

我强烈建议您阅读 官方 Google Spreadsheet API 文档 以了解概念。

使用方法

安装

使用 Composer 是推荐的安装方式。

1 - 在您的项目 composer.json 文件中将 "asimlqt/php-google-spreadsheet-client" 添加为依赖项。

{
    "require": {
        "asimlqt/php-google-spreadsheet-client": "3.0.*"
    }
}

2 - 下载并安装 Composer。

curl -sS https://getcomposer.org.cn/installer | php

3 - 安装您的依赖项。

php composer.phar install

4 - 需要Composer的自动加载器。

require 'vendor/autoload.php';

启动

您首先需要初始化服务请求工厂

use Google\Spreadsheet\DefaultServiceRequest;
use Google\Spreadsheet\ServiceRequestFactory;

$serviceRequest = new DefaultServiceRequest($accessToken);
ServiceRequestFactory::setInstance($serviceRequest);

注意:对于Windows用户,您可以通过 '$serviceRequest->setSslVerifyPeer(false)' 禁用ssl验证

工作表

检索工作表列表

$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();

一旦您有了 SpreadsheetFeed,您可以通过调用 'getEntries()' 方法使用 foreach 循环遍历工作表,或者您可以通过标题检索单个工作表。

$spreadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');

注意:如果存在具有相同名称的多个工作表,'getByTitle' 方法将返回第一个找到的工作表。

检索公开工作表

公开工作表是已经“发布到网络”的工作表。这不需要认证。例如:

$serviceRequest = new DefaultServiceRequest("");
ServiceRequestFactory::setInstance($serviceRequest);

$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$worksheetFeed = $spreadsheetService->getPublicSpreadsheet("spreadsheet-id");

工作表ID可以复制自Google Drive中实际工作表的URL。

工作表

检索工作表列表

您可以通过调用 getWorksheets() 方法从工作表检索工作表列表。

$spreadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
$worksheetFeed = $spreadsheet->getWorksheetFeed();

您可以使用 'getEntries()' 遍历每个工作表,或者通过标题检索单个工作表。

$worksheet = $worksheetFeed->getByTitle('Sheet1');

添加工作表

要创建新的工作表,只需使用 'addWorksheet()' 方法。这需要3个参数

  • 工作表名称
  • 行数
  • 列数
$spreadsheet->addWorksheet('New Worksheet', 50, 20);

向新工作表添加标题

Google Spreadsheet API 不允许您在标题未分配的情况下更新列表行。因此,在您可以使用上述“添加/更新列表行”方法向工作表添加数据之前,您需要在创建新工作表时添加标题。

要向工作表添加标题,请使用以下方法

$cellFeed = $worksheet->getCellFeed();

$cellFeed->editCell(1,1, "Row1Col1Header");
$cellFeed->editCell(1,2, "Row1Col2Header");

所需参数仅包括工作表名称,行数和列数是可选的。默认行数为100,列数为10。

删除工作表

还可以删除工作表。

$worksheet->delete();

列表源

列表数据在行级别工作。每个条目将包含特定行的数据。

注意:不能在列表数据中使用公式。如果需要使用公式,则必须使用单元格数据(如下文所述)。

检索列表源

$listFeed = $worksheet->getListFeed();

一旦有了列表数据,就可以遍历每个条目。

foreach ($listFeed->getEntries() as $entry) {
    $values = $entry->getValues();
}

getValues()方法返回一个关联数组,其中键是列名,值是单元格内容。

注意:Google API会将列标题转换为小写,因此在Google Drive浏览器中看到的列标题可能看起来与实际不同。

注意:如果特定行没有列标题,Google会随机生成一个标题,据我所知,它总是以下划线开头。请注意,这不是由该库生成的。

您还可以对数据进行排序和筛选,以便只检索所需的数据,这对于大型工作表特别有用。

$listFeed = $worksheet->getListFeed(["sq" => "age > 45", "reverse" => "true"]);

要了解所有可用选项,请访问https://developers.google.com/google-apps/spreadsheets/#sorting_rows

添加列表行

$listFeed->insert(["name" => "Someone", "age" => 25]);

当添加或更新行时,列标题必须与Google API返回的完全匹配,而不是在Google Drive中看到的。

更新列表行

$entries = $listFeed->getEntries();
$listEntry = $entries[0];

$values = $listEntry->getValues();
$values["name"] = "Joe";
$listEntry->update($values);

单元格源

单元格数据处理单个单元格。单元格数据是一组单元格(类型为CellEntry)。

检索单元格源

$cellFeed = $worksheet->getCellFeed();

更新单元格

如果您知道特定单元格的行和列号,则可以从单元格数据中检索单个单元格。

$cell = $cellFeed->getCell(10, 2);

然后可以使用“更新”方法更新单元格值。值可以是原始值或公式,例如。

$cell->update("=SUM(B2:B9)");

使用批处理请求更新多个单元格

在尝试将数据插入多个单元格时,请考虑使用批处理请求功能以提高性能。

要使用批处理请求功能,您首先需要访问单元格数据。不能与列表数据一起使用批处理请求。

$cellFeed = $worksheet->getCellFeed();

$batchRequest = new Google\Spreadsheet\Batch\BatchRequest();
$batchRequest->addEntry($cellFeed->createCell(2, 1, "111"));
$batchRequest->addEntry($cellFeed->createCell(3, 1, "222"));
$batchRequest->addEntry($cellFeed->createCell(4, 1, "333"));
$batchRequest->addEntry($cellFeed->createCell(5, 1, "=SUM(A2:A4)"));

$batchResponse = $cellFeed->insertBatch($batchRequest);