1

I use script from here to create spread sheet, but it always creates as closed one. I need to create spread sheet that accessible by link, but I cannot find a way to do it.

Here what I mean:

$spreadsheet = new Google_Service_Sheets_Spreadsheet([
            'properties' => [
                'title' => $title,
            ]
        ]);

(taken from link I proposed before) I creating new spreadsheet, and I assume that I need to do something like that

$spreadsheet = new Google_Service_Sheets_Spreadsheet([
            'properties' => [
                'title' => $title,
                'access' => 'by_link' //pseudo code
            ]
        ]);

How to do it?

Tanaike
  • 181,128
  • 11
  • 97
  • 165
Kemsikov
  • 452
  • 3
  • 13

1 Answers1

3

I believe your goal is as follows.

  • From it always creates as closed one. I need to create spread sheet that accessible by link, you want to retrieve the URL of the created Spreadsheet.
  • In this case, I thought that you might want to publicly share the created Spreadsheet.
  • From your script, you want to achieve your goal using googleapis with PHP.

In this case, how about the following modified script?

Modified script:

$service = new Google_Service_Sheets($client); // Please use your script here.

$spreadsheet = new Google_Service_Sheets_Spreadsheet([
    'properties' => [
        'title' => $title,
    ]
]);
$spreadsheet = $service->spreadsheets->create($spreadsheet, ['fields' => 'spreadsheetId']);

// Here, you can see the URL of the created Spreadsheet.
$url = "https://docs.google.com/spreadsheets/d/" . $spreadsheet->spreadsheetId . "/edit";
printf("URL: %s\n", $url);

// Below script publicly shares the created Spreadsheet.
$drive = new Google_Service_Drive($client);
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('reader');  // or writer
$drive->permissions->create($spreadsheet->spreadsheetId, $newPermission);
  • If you don't want to publicly share the created Spreadsheet, please remove the above script below $drive = new Google_Service_Drive($client);.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • When I try to use it I get this: ``` { "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission: Request had insufficient authentication scopes." } ], "code": 403, "message": "Insufficient Permission: Request had insufficient authentication scopes." } } ``` – Kemsikov Nov 02 '21 at 08:13
  • I think I need to change scopes for my client – Kemsikov Nov 02 '21 at 08:16
  • @Kemsikov Thank you for replying. I apologize for the inconvenience. Unfortunately, although I cannot know your whole script, from the error message of `Request had insufficient authentication scopes.`, I'm worried that your scopes cannot be able to use the Drive API. If it's so, for example, how about adding the scope of `https://www.googleapis.com/auth/drive` for using Drive API? By the way, what scopes are you using now? – Tanaike Nov 02 '21 at 08:17
  • @Kemsikov For example, how about `$client->setScopes(array(Google_Service_Drive::DRIVE, Google_Service_Sheets::SPREADSHEETS));`? In that case, please delete the file including the access token and refresh token and reauthorize the scopes. By this, the access token for using both Drive API and Sheets API can be retrieved. – Tanaike Nov 02 '21 at 08:18
  • THANK YOU VERY MUCH! I added drive api to project and updated api keys and now it works as intended! – Kemsikov Nov 02 '21 at 08:37