【问题标题】:How to update Google Spreadsheet cell using PHP API如何使用 PHP API 更新 Google 电子表格单元格
【发布时间】:2013-10-14 14:34:26
【问题描述】:

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

我浏览了许多 Google 文档页面,但没有一个符合我的要求。

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

如果这是 RTFM 问题,请原谅我,但我确实花了 2 周多的时间来解决这个问题,但没有任何结果。 :/

【问题讨论】:

  • 你在哪里找?有很多使用电子表格提要 API 的示例。它都在那里。首先了解可以在 oauth2 操场上练习的 oauth。谷歌两者。
  • 你愿意给我一个工作的例子吗?我确实获得了 oAuth 密钥,甚至成功地使用以下方法获得了我的电子表格数据,例如标题和 mimeType:$file = $service->files->get($fileId);打印“标题:”。 $file->getTitle(); 但是,我不知道谁来编辑单元格
  • 全部在 api 文档中。或者使用 zend 作为 php 包装器。

标签: google-drive-api google-api-php-client


【解决方案1】:

您可以使用 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-(rather-than-user-based-access-refresh-tokens)

  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;
    }
    

【讨论】:

【解决方案2】:

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

【讨论】:

  • 感谢您的帮助,但我只是找到了 Zend GData 的解决方案,这是目前唯一的方法,因为 Google 只为 .NET、Java 和 Python 的新 APIS 提供官方支持!跨度>
【解决方案3】:

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

【讨论】:

    【解决方案4】:

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

    Zend GData Library

    Zend Gdata for Spreadsheet with the examples code

    【讨论】:

    • 这已经过时了。
    猜你喜欢
    • 1970-01-01
    • 2020-10-13
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多