Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Spreadsheet API v4

I am trying to create a new spreadsheet, which I understand is now possible in v4 of the API, but I don't see anything detailing how to create a sheet in the PHP documentation. I got:

$client->setApplicationName("Test");
$client->setScopes(['https://www.googleapis.com/auth/sheets']);
$service = new Google_Service_Sheets($client);

$service->spreadsheets->create();

But it expects parameter 1 to be an instance of Google_Service_Sheets_Spreadsheet, and I'm not sure what exactly I should provide.

like image 299
awestover89 Avatar asked Jan 06 '23 21:01

awestover89


1 Answers

Got it figured out. The sheet was created under the service account, not the primary account. Had to use the Drive API to add permissions to share with the primary account:

$client->setScopes(
        ['https://www.googleapis.com/auth/spreadsheets',
        'https://www.googleapis.com/auth/drive']);
$service = new Google_Service_Sheets($client);
$drive = new Google_Service_Drive($client);

$ss = $service->spreadsheets->create(new Google_Service_Sheets_Spreadsheet());
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setEmailAddress("[email protected]");
$newPermission->setType('user');
$newPermission->setRole('writer');
$optParams = array('sendNotificationEmail' => false);
$drive->permissions->create($ss->spreadsheetId,$newPermission,$optParams);
like image 95
awestover89 Avatar answered May 23 '23 19:05

awestover89