asimlqt/php-google-spreadsheet-client
Composer 安装命令:
composer require asimlqt/php-google-spreadsheet-client
包简介
Google Spreadsheet PHP Client
关键字:
README 文档
README
This library is no longer maintained. Please use the official Google PHP Client
Contents
Introduction
This library provides a simple interface to the Google Spreadsheet API v3.
There are a couple of important things to note.
- This library requires a valid OAuth access token to work but does not provide any means of generating one. The Google APIs Client Library for PHP has all the functionality required for generating and refreshing tokens so it would have been a waste of time duplicating the official Google library.
- You can not create spreadsheets using this library, as creating spreadsheets is not part of the Google Spreadsheet API, rather it's part of the Google Drive API. See the official Google APIs Client Library for PHP.
I strongly recommend you read through the official Google Spreadsheet API documentation to get a grasp of the concepts.
Usage
Installation
Using Composer is the recommended way to install it.
1 - Add "asimlqt/php-google-spreadsheet-client" as a dependency in your project's composer.json file.
{
"require": {
"asimlqt/php-google-spreadsheet-client": "3.0.*"
}
}
2 - Download and install Composer.
curl -sS https://getcomposer.org/installer | php
3 - Install your dependencies.
php composer.phar install
4 - Require Composer's autoloader.
require 'vendor/autoload.php';
Bootstrapping
The first thing you will need to do is initialize the service request factory:
use Google\Spreadsheet\DefaultServiceRequest; use Google\Spreadsheet\ServiceRequestFactory; $serviceRequest = new DefaultServiceRequest($accessToken); ServiceRequestFactory::setInstance($serviceRequest);
Note: For Windows users, you can disable the ssl verification by '$serviceRequest->setSslVerifyPeer(false)'
Spreadsheet
Retrieving a list of spreadsheets
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService(); $spreadsheetFeed = $spreadsheetService->getSpreadsheetFeed();
Once you have a SpreadsheetFeed you can iterate over the spreadsheets using a foreach loop by calling the 'getEntries()' method or you can retrieve a single spreadsheet by it's title.
$spreadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet');
Note: The 'getByTitle' method will return the first spreadsheet found with that title if you have more than one spreadsheet with the same name.
Retrieving a public spreadsheet
A public spreadsheet is one that has been "published to the web". This does not require authentication. e.g.
$serviceRequest = new DefaultServiceRequest(""); ServiceRequestFactory::setInstance($serviceRequest); $spreadsheetService = new Google\Spreadsheet\SpreadsheetService(); $worksheetFeed = $spreadsheetService->getPublicSpreadsheet("spreadsheet-id");
The spreadsheet id can be copied from the url of the actual spreadsheet in Google Drive.
Worksheet
Retrieving a list of worksheets
You can retrieve a list of worksheets from a spreadsheet by calling the getWorksheets() method.
$spreadsheet = $spreadsheetFeed->getByTitle('MySpreadsheet'); $worksheetFeed = $spreadsheet->getWorksheetFeed();
You can loop over each worksheet using 'getEntries()' or retrieve a single worksheet by it's title.
$worksheet = $worksheetFeed->getByTitle('Sheet1');
Adding a worksheet
To create a new worksheet simply use the 'addWorksheet()' method. This takes 3 arguments:
- Worksheet name
- Number of rows
- Number of columns
$spreadsheet->addWorksheet('New Worksheet', 50, 20);
Adding headers to a new worksheet
The Google Spreadsheet API does not allow you to update a list row if headers are not already assigned. So, when you create a new worksheet, before you can add data to a worksheet using the 'Adding/Updating a list row' methods above, you need to add headers.
To add headers to a worksheet, use the following:
$cellFeed = $worksheet->getCellFeed(); $cellFeed->editCell(1,1, "Row1Col1Header"); $cellFeed->editCell(1,2, "Row1Col2Header");
The only required parameter is the worksheet name, The row and column count are optional. The default value for rows is 100 and columns is 10.
Deleting a worksheet
It's also possible to delete a worksheet.
$worksheet->delete();
List feed
List feeds work at the row level. Each entry will contain the data for a specific row.
Note: You can not use formulas with the list feed. If you want to use formulas then you must use the cell feed (described below).
Retrieving a list feed
$listFeed = $worksheet->getListFeed();
Once you have a list feed you can loop over each entry.
foreach ($listFeed->getEntries() as $entry) { $values = $entry->getValues(); }
The getValues() method returns an associative array where the keys are the column names and the values are the cell content.
Note: The Google api converts the column headers to lower case so the column headings might not appear to be the same as what you see in Google Drive using your browser.
Note: If there is data for a particular row which does not have a column header then Google randomly generates a header and as far as I know it always begins with an underscore. Bear in mind that this is not generated by this library.
You can also sort and filter the data so you only retrieve what is required, this is expecially useful for large worksheets.
$listFeed = $worksheet->getListFeed(["sq" => "age > 45", "reverse" => "true"]);
To find out all the available options visit https://developers.google.com/google-apps/spreadsheets/#sorting_rows.
Adding a list row
$listFeed->insert(["name" => "Someone", "age" => 25]);
When adding or updating a row the column headers need to match exactly what was returned by the Google API, not what you see in Google Drive.
Updating a list row
$entries = $listFeed->getEntries(); $listEntry = $entries[0]; $values = $listEntry->getValues(); $values["name"] = "Joe"; $listEntry->update($values);
Cell feed
Cell feed deals with individual cells. A cell feed is a collection of cells (of type CellEntry)
Retrieving a cell feed
$cellFeed = $worksheet->getCellFeed();
Updating a cell
You can retrieve a single cell from the cell feed if you know the row and column numbers for that specific cell.
$cell = $cellFeed->getCell(10, 2);
You can then update the cell value using the 'update' method. The value can be a primitive value or a formula e.g.
$cell->update("=SUM(B2:B9)");
Updating multiple cells with a batch request
When attempting to insert data into multiple cells then consider using the batch request functionality to improve performance.
To use the batch request functionality you need access to a cell feed first. You can not use batch requests with list feeds.
$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);
asimlqt/php-google-spreadsheet-client 适用场景与选型建议
asimlqt/php-google-spreadsheet-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.79M 次下载、GitHub Stars 达 541, 最近一次更新时间为 2013 年 12 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spreadsheet」 「google」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 asimlqt/php-google-spreadsheet-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 asimlqt/php-google-spreadsheet-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 asimlqt/php-google-spreadsheet-client 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
PHPExcel - OpenXML - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
Adds support for dynamic translations from Google Spreadsheets.
统计信息
- 总下载量: 2.79M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 548
- 点击次数: 14
- 依赖项目数: 11
- 推荐数: 2
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2013-12-04