S
S
Sergey Bard2017-03-03 13:43:52
PHP
Sergey Bard, 2017-03-03 13:43:52

How to create a table via Google Docs API?

require_once __DIR__ . '/vendor/autoload.php';

      function initializeSheets(){
       $KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';

        $client = new Google_Client();
        $client->setApplicationName("Google Sheets API PHP Quickstart");
        $client->setAuthConfig($KEY_FILE_LOCATION);
        $sheets = $client->setScopes(implode(' ', array(Google_Service_Sheets::SPREADSHEETS_READONLY)));
        return $sheets;
      }
      $sheets = initializeSheets();
      
        $sfsef = new Google_Service_Sheets_GridProperties();
        $sfsef->setRowCount(8);
        $sfsef->setColumnCount(5);
        
        $sheet111 = new Google_Service_Sheets_SheetProperties();
        $sheet111->setSheetType('GRID');
        $sheet111->setSheetId(0);
        $sheet111->setTitle('название листа');
        $sheet111->setGridProperties($sfsef);
        
        $sheet = new Google_Service_Sheets_Sheet();
        $sheet->setProperties($sheet111);
        
        $properties = new Google_Service_Sheets_SpreadsheetProperties();
        $properties->setTitle('название документа');
        $properties->setLocale('ru_RU');
        
        $spreadsheets1 = new Google_Service_Sheets_Spreadsheet();
        $spreadsheets1->setProperties($properties);
        $spreadsheets1->setSheets($sheet);
        
        $result = $sheets->spreadsheets->create( $spreadsheets1 ) ;
        
        echo '<pre>';
          print_r($result);
        echo '</pre>';

Who can tell me where I messed up?), you need to create a table through the API.
I did everything according to the material that I found on the net, but at the output it gives an error:
148854491802_kiss_6kb.png
I think that something else is missing at the end, because did the example for analytics and there was a function at the end
Google_Service_AnalyticsReporting_GetReportsRequest
, but I can't find a similar function for tables.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Bard, 2017-03-03
@serg_small_developer

I leave the working code here, maybe the cat will need it, because I found only one good article, and then on python, no one wants to upload and share, but I'm not greedy)

require_once __DIR__ . '/vendor/autoload.php';

      function initializeSheets(){
       $KEY_FILE_LOCATION = __DIR__ . '/service-account-credentials.json';

        $client = new Google_Client();
        $client->setApplicationName("Google Sheets API PHP Quickstart");
        $client->setAuthConfig($KEY_FILE_LOCATION);
        $client->setScopes(['https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/spreadsheets']);
       return $client;
      }
      $sheets = new Google_Service_Sheets(initializeSheets());
      //создаем саму таблицу
        $sfsef = new Google_Service_Sheets_GridProperties();
        $sfsef->setRowCount(8);
        $sfsef->setColumnCount(5);
        
        $sheet111 = new Google_Service_Sheets_SheetProperties();
        $sheet111->setSheetType('GRID');
        $sheet111->setSheetId(0);
        $sheet111->setTitle('название листа');
        $sheet111->setGridProperties($sfsef);
        
        $sheet = new Google_Service_Sheets_Sheet();
        $sheet->setProperties($sheet111);
        
        $properties = new Google_Service_Sheets_SpreadsheetProperties();
        $properties->setTitle('название документа');
        $properties->setLocale('ru_RU');
        
        $spreadsheets1 = new Google_Service_Sheets_Spreadsheet();
        $spreadsheets1->setProperties($properties);
        $spreadsheets1->setSheets($sheet);
        
        $result = $sheets->spreadsheets->create( $spreadsheets1 ) ;
        
        //даем доступ для редактирование кому угодно
        $drive = new Google_Service_Drive(initializeSheets());
    
        $drive1 = new Google_Service_Drive_Permission();
        $drive1->setType('anyone');
        $drive1->setRole('writer');
        $drive1s = $result['spreadsheetId'];
        $resultqqq = $drive->permissions->create( $drive1s, $drive1 ) ;

        echo '<pre>';
          print_r($result);
        echo '</pre>';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question