【问题标题】:Creating new googlesheet with googlesheet API使用 google sheet API 创建新的 google sheet
【发布时间】:2023-01-22 01:18:24
【问题描述】:
<?php
        
            require_once __DIR__ . '/vendor/autoload.php';


        $client_id = '*********.apps.googleusercontent.com';
        $client_secret = '**************************';
        $redirect_uri = 'https://{site}/wp-admin/plugins.php/oauth';
        $client = new Google_Client();
        $client->setClientId( $client_id );
        $client->setClientSecret( $client_secret );
        $client->setRedirectUri( $redirect_uri );
        $client->setScopes( array('https://www.googleapis.com/auth/drive') );
        if ( isset( $_GET['code'] ) ) {
            $client->authenticate( $_GET['code'] );
            $_SESSION['access_token'] = $client->getAccessToken();
            header( 'Location: ' . filter_var( $redirect_uri, FILTER_SANITIZE_URL ) );
            exit;
        }
        if ( ! isset( $_SESSION['access_token'] ) ) {
            $auth_url = $client->createAuthUrl();
            header( 'Location: ' . filter_var( $auth_url, FILTER_SANITIZE_URL ) );
            exit;
        }
        $client->setAccessToken( $_SESSION['access_token'] );
        $service = new Google_Service_Sheets($client);
        $spreadsheet = new Google_Service_Sheets_Spreadsheet(array(
            'properties' => array(
                'title' => 'My New Spreadsheet'
            ),
            'sheets' => array(
                new Google_Service_Sheets_Sheet(array(
                    'properties' => array(
                        'title' => 'Sheet1',
                        'gridProperties' => array(
                            'rowCount' => 20,
                            'columnCount' => 12
                        )
                    )
                ))
            )
        ));
        
        $spreadsheet = $service->spreadsheets->create($spreadsheet, array('fields' => 'spreadsheetId'));

        // Print the new spreadsheet's ID
        echo 'Spreadsheet ID: ' . $spreadsheet->getSpreadsheetId();
}    

我在 php 客户端库和 google sheet API 的帮助下创建了 googlesheet 但不知道!!这段代码有什么问题,我什至没有创建新的谷歌表格,也没有返回错误。

【问题讨论】:

  • 关于What is wrong with this code my new google sheet is not even created and the error is also not returned.,在您的脚本中,似乎在创建新电子表格时,会显示所创建电子表格的电子表格ID。这个怎么样?
  • 是的,但即使工作表 ID 没有返回,也没有任何返回,当我检查我的驱动器时,没有创建新的工作表。
  • 谢谢你的回复。来自nothing is returning even the sheet id,在这种情况下,我认为没有创建新的电子表格。但是,当我使用我的访问令牌测试您的脚本时,返回了电子表格 ID,我可以确认创建的电子表格。那么,再次确认您的访问令牌怎么样?
  • 最后,我创造了非常感谢你:)
  • 谢谢你的回复。很高兴您的问题得到解决。当您的问题得到解决后,将其作为答案发布怎么样?这样,对于遇到相同问题的其他用户将很有用。

标签: php google-sheets


【解决方案1】:
<?php
        
            require_once __DIR__ . '/vendor/autoload.php';


        $client_id = '*********.apps.googleusercontent.com';
        $client_secret = '**************************';
        $redirect_uri = 'https://{site}/wp-admin/plugins.php/oauth';
        $client = new Google_Client();
        $client->setClientId( $client_id );
        $client->setClientSecret( $client_secret );
        $client->setRedirectUri( $redirect_uri );
        $client->setScopes( array('https://www.googleapis.com/auth/drive') );
        if ( isset( $_GET['code'] ) ) {
            $client->authenticate( $_GET['code'] );
            $_SESSION['access_token'] = $client->getAccessToken();
            header( 'Location: ' . filter_var( $redirect_uri, FILTER_SANITIZE_URL ) );
            exit;
        }
        if ( ! isset( $_SESSION['access_token'] ) ) {
            $auth_url = $client->createAuthUrl();
            header( 'Location: ' . filter_var( $auth_url, FILTER_SANITIZE_URL ) );
            exit;
        }
        $client->setAccessToken( $_SESSION['access_token'] );
        $service = new Google_Service_Sheets($client);
        $spreadsheet = new Google_Service_Sheets_Spreadsheet(array(
            'properties' => array(
                'title' => 'My New Spreadsheet'
            ),
            'sheets' => array(
                new Google_Service_Sheets_Sheet(array(
                    'properties' => array(
                        'title' => 'Sheet1',
                        'gridProperties' => array(
                            'rowCount' => 20,
                            'columnCount' => 12
                        )
                    )
                ))
            )
        ));
        
        $spreadsheet = $service->spreadsheets->create($spreadsheet, array('fields' => 'spreadsheetId'));

        // Print the new spreadsheet's ID
        echo 'Spreadsheet ID: ' . $spreadsheet->getSpreadsheetId();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多