Coeb
Coeb

Reputation: 25

Changing the permissions of an uploaded sheets

I'm running into an issue where I have a sheet which has been uploaded using the Sheets PHP API and now I need to change the permissions of that sheet. It doesn't matter to me if the permissions are set at upload or changed later. Here's what my upload code looks like

function createSheet(){
    $client = getClient();
    $service = new Google_Service_Sheets($client);
    $spreadsheet = new Google_Service_Sheets_Spreadsheet([
        'properties' => [
            'title' => "test_sheet3",
        ]
    ]);
    $spreadsheet = $service->spreadsheets->create($spreadsheet, [
        'fields' => 'spreadsheetId'
    ]);
    printf("Spreadsheet ID: %s\n", $spreadsheet->spreadsheetId);
    return $spreadsheet->spreadsheetID;
}

I've been poking around this google documentation but every time I try and include any of the settings in the properties JSON ex:

        'properties' => [
            'title' => "test_sheet3",
            'type' => 'group'
        ]
    ]);

I get the error

"Invalid JSON payload received. Unknown name \"type\" at 'spreadsheet.properties': Cannot find field."

So I'm not completely sure if my syntax is incorrect, or they should be added to a different JSON attachment that isn't the properties JSON.

Upvotes: 0

Views: 363

Answers (1)

Tanaike
Tanaike

Reputation: 201378

I would like to propose the following modification.

Modification points:

  • You can give the permissions using the method of "Permissions: create" in Drive API. This has already been mentioned in your question. In this case, the request parameters cannot be included in the method of "spreadsheets.create" in Sheets API. Please request it using the method of "Permissions: create" in Drive API.

When above points are reflected to your script, it becomes as follows.

Modified script:

$client = getClient();
$service = new Google_Service_Sheets($client);
$spreadsheet = new Google_Service_Sheets_Spreadsheet([
    'properties' => [
        'title' => "test_sheet3",
    ]
]);
$spreadsheet = $service->spreadsheets->create($spreadsheet, [
    'fields' => 'spreadsheetId'
]);
printf("Spreadsheet ID: %s\n", $spreadsheet->spreadsheetId);


// I added below script
$drive = new Google_Service_Drive($client);
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setEmailAddress('###@gmail.com');
$newPermission->setType('group');
$newPermission->setRole('writer');
$res = $drive->permissions->create($spreadsheet->spreadsheetId, $newPermission);
// print_r($res);


return $spreadsheet->spreadsheetID;

Note:

  • In this case, as a test, I used the scope of https://www.googleapis.com/auth/drive. For this, please use the following script. And when you modified the scopes, please the file including the refresh token and reauthorize the scopes. By this, the modified scopes can be reflected to the access token. Please be careful this.

      $client->setScopes(array(Google_Service_Sheets::SPREADSHEETS, Google_Service_Drive::DRIVE));
    
  • When you want to use group to type, please set the email address.

  • If you want to give the permissions to an user, please modify $newPermission->setType('group'); to $newPermission->setType('user'); and please use the email address.

Reference:

Upvotes: 2

Related Questions