menu

Questions & Answers

Creating new googlesheet with googlesheet API
<?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();
}    

I was creating googlesheet with help of php client library and google sheet API but Don't know !! What is wrong with this code my new google sheet is not even created and the error is also not returned.

Comments:
2023-01-21 23:01:16
About What is wrong with this code my new google sheet is not even created and the error is also not returned., in your script, it seems that when a new Spreadsheet is created, the spreadsheet ID of the created Spreadsheet is shown. How about this?
2023-01-21 23:01:16
yes but nothing is returning even the sheet id is not returning and when I check my drive no new sheet is created.
2023-01-21 23:01:16
Thank you for replying. From nothing is returning even the sheet id, in this case, I think that a new Spreadsheet is not created. But, when I tested your script using my access token, the spreadsheet ID is returned and I can confirm the created Spreadsheet. So, how about confirming your access token again?
2023-01-21 23:01:16
Finally, i have created thank you so much :)
2023-01-21 23:01:16
Thank you for replying. I'm glad your issue was resolved. When your issue was resolved, how about posting it as an answer? By this, it will be useful for other users who have the same issue.
Answers(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();
}