如何使用PHP API更新Google Spreadsheet单元格

时间:2013-10-14 14:34:26

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

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

我查看了Google的许多文档,但是,这些都没有达到我正在寻找的目标。

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

请原谅我,如果这是一个RTFM问题,但我确实花了超过2周的时间没有结果。 :/

4 个答案:

答案 0 :(得分:5)

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

  1. 通过composer安装库:

    "require": {
        "asimlqt/php-google-spreadsheet-client": "dev-master"
    }
    
  2. PHP文件中的Bootstrap composer:

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

答案 1 :(得分:2)

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

答案 2 :(得分:0)

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

Zend GData Library

Zend Gdata for Spreadsheet with the examples code

答案 3 :(得分:0)

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

@bp.route('/')
def start():
    return redirect(url_for('auth.start_page'))

使用类似PUT的方法传递值数据

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

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

您可以引用此https://developers.google.com/sheets/api/guides/migration