4

我想知道如何使用 PHP 与 Google 电子表格进行交互。

我浏览了谷歌文档的许多页面,但是,这些都不是我想要的。

我的目标是能够使用 oAuth(不是电子邮件/通行证)更改单元格的内容。

如果这是 RTFM 问题,请原谅我,但我确实花了两个多星期没有结果。:/

4

4 回答 4

5

您可以使用asimlqt/php-google-spreadsheet-client库来执行此操作。

  1. 通过composer安装库:

    "require": {
        "asimlqt/php-google-spreadsheet-client": "dev-master"
    }
    
  2. 您的 PHP 文件中的引导作曲家:

    require('vendor/autoload.php');
    
  3. 按照以下步骤获取 Google API 客户端 ID、客户端电子邮件和 P12 访问密钥,如下所述:
    https ://github.com/asimlqt/php-google-spreadsheet-client/wiki/How-to-use-% 22Service-account%22-authorization-(而不是基于用户的访问刷新令牌)

  4. 使用以下代码:

    $accessToken = getGoogleTokenFromKeyFile(CLIENT_ID_HERE, CLIENT_EMAIL_HERE, P12_FILE_PATH_HERE);
    use Google\Spreadsheet\DefaultServiceRequest;
    use Google\Spreadsheet\ServiceRequestFactory;
    ServiceRequestFactory::setInstance(new DefaultServiceRequest($accessToken));
    
    // Load spreadsheet and worksheet
    $worksheet = (new Google\Spreadsheet\SpreadsheetService())
        ->getSpreadsheets()
        ->getByTitle('xxxxxxxxx')       // Spreadsheet name
        ->getWorksheets()
        ->getByTitle('xxxxxxxxx');      // Worksheet name
    $listFeed = $worksheet->getListFeed();
    
    // Uncomment this to find out what Google calls your column names
    // print_r($listFeed->getEntries()[0]->getValues());
    
    // Add a new blank row to the spreadsheet, using the column headings
    $listFeed->insert(['name' => 'Simon', 'age' => 25, 'gender' => 'male']);
    
    /**
     * Retrieves a Google API access token by using a P12 key file,
     * client ID and email address
     *
     * These three things may be obtained from 
     * https://console.developers.google.com/
     * by creating a new "Service account"
     */
    function getGoogleTokenFromKeyFile($clientId, $clientEmail, $pathToP12File) {
        $client = new Google_Client();
        $client->setClientId($clientId);
    
        $cred = new Google_Auth_AssertionCredentials(
            $clientEmail,
            array('https://spreadsheets.google.com/feeds'),
            file_get_contents($pathToP12File)
        );
    
        $client->setAssertionCredentials($cred);
    
        if ($client->getAuth()->isAccessTokenExpired()) {
            $client->getAuth()->refreshTokenWithAssertion($cred);
        }
    
        $service_token = json_decode($client->getAccessToken());
        return $service_token->access_token;
    }
    
于 2015-03-17T04:36:49.367 回答
2

Drive API 不提供任何编辑电子表格的方法,Spreadsheets API包含低级单元格修改方法。请注意,您不能使用 Google APIs PHP 客户端库来使用电子表格 API。

于 2013-10-15T01:26:15.810 回答
2

API v4,您可以使用 curl。获取访问令牌,然后使用 curl。您可以更新单行或多行或者您可以更新单列或多列。如果您要更新单列或单行,请使用https://sheets.googleapis.com/v4/spreadsheets/sheetID/values/RowORColumnRange/?valueInputOption=RAW&includeValuesInResponse=true

sheetID = FileID
RowORColumnRange = Sheet!A1 or Sheet!B2

使用 PUT 方法传递值数据,例如

$update['values'] = [[1234]];

对于批量更新而不是在 URL 中使用方法值,您可以更改为 values:batchUpdate。然后在数组中传递其他数据并使用方法POST;

您可以参考此https://developers.google.com/sheets/api/guides/migration

于 2019-01-02T21:17:09.783 回答
0

这是我为我的问题得到的解决方案:

Zend GData 库

带有示例代码的 Zend Gdata for Spreadsheet

于 2013-10-15T11:47:42.940 回答