在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):asimlqt/php-google-spreadsheet-client开源软件地址(OpenSource Url):https://github.com/asimlqt/php-google-spreadsheet-client开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):
ContentsIntroductionThis library provides a simple interface to the Google Spreadsheet API v3. There are a couple of important things to note.
I strongly recommend you read through the official Google Spreadsheet API documentation to get a grasp of the concepts. UsageInstallationUsing 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.
3 - Install your dependencies.
4 - Require Composer's autoloader.
BootstrappingThe 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);
SpreadsheetRetrieving 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');
Retrieving a public spreadsheetA 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. WorksheetRetrieving a list of worksheetsYou 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 worksheetTo create a new worksheet simply use the 'addWorksheet()' method. This takes 3 arguments:
$spreadsheet->addWorksheet('New Worksheet', 50, 20); Adding headers to a new worksheetThe 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 worksheetIt's also possible to delete a worksheet. $worksheet->delete(); List feedList feeds work at the row level. Each entry will contain the data for a specific row.
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.
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]);
Updating a list row$entries = $listFeed->getEntries();
$listEntry = $entries[0];
$values = $listEntry->getValues();
$values["name"] = "Joe";
$listEntry->update($values); Cell feedCell 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 cellYou 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 requestWhen 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); |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论