Home > OS >  Google Api create accessible by link SpeadSheet
Google Api create accessible by link SpeadSheet

Time:11-03

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?

CodePudding user response:

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:

  • Related