Search code examples
pythongoogle-drive-apigspread

Share to multiple users using Gspread in Python


I'm working on an automation script which automates the Google Spread Sheets using python and gspread library. Using this I'm able to share the created spread sheet using share() method to a single user. But how to share it with more than one users ? Please suggest me the way to do that !

Code I used

spreadSheet.share("[email protected]", perm_type='user', role='writer')

Also I tried to add the recipients in a list of strings but not working

spreadSheet.share(["[email protected]", "[email protected]"], perm_type='user', role='writer')

@Tanaike

Tried the Pattern 2

from oauth2client.service_account import ServiceAccountCredentials
import gspread
from googleapiclient.discovery import build

scopes=[scope1, scope2]
cred=ServiceAccountCredentials.from_json_keyfile_name('myjsonfilepath', scopes)
driveClient = build("drive", "v3", credentials=cred)
emailRecepients = ['xxx', 'yyy', ,,,]
batch = driveClient.new_batch_http_request()
if len(emailRecepients) > 0 and len(emailRecepients)<=100:
    for e in emailRecepients:             
       batch.add(driveClient.permissions().create(fileId='id', body={"role": "reader", "type": "user", "emailAddress": e}))
    a = batch.execute()
    print(a)

Solution

  • I believe your goal is as follows.

    • You want to share a Google Spreadsheet with multiple users.
    • You want to achieve this using gspread for python.

    In order to share a file on Google Drive with a user, in the current stage, "Permissions: create" of Drive API is used. At "Permissions: create", one user is added by one API call. So, in this case, I thought that the following patterns can be considered.

    Pattern 1:

    In this pattern, only gspread is used. In this case, the API calls of the number of email addresses are used.

    import gspread
    from googleapiclient.discovery import build
    
    gc = gspread.oauth(credentials_filename="###", authorized_user_filename="###") # Please use your gspread client.
    spreadSheet = gc.open_by_key(spreadsheetId) # Please use your Class Spreadsheet.
    
    emails = ["###", "###",,,] # Please set email addresses you want to share.
    for e in emails:
        spreadSheet.share(e, perm_type="user", role="writer")
    

    Pattern 2:

    In this pattern, gspread and googleapis for python are used. In this case, the batch request is used. By this, each request is run by the asynchronous process. And, one API call is used. But, in the case of batch requests, one batch request can be included 100 requests. So, please be careful about this.

    import gspread
    from googleapiclient.discovery import build
    
    gc = gspread.oauth(credentials_filename="###", authorized_user_filename="###") # Please use your gspread client.
    
    spreadsheetId = "###" # Please set Spreadsheet ID.
    emails = ["###", "###",,,] # Please set email addresses you want to share.
    
    service = build("drive", "v3", credentials=gc.auth)
    batch = service.new_batch_http_request()
    for e in emails:
        batch.add(service.permissions().create(fileId=spreadsheetId, body={"role": "writer", "type": "user", "emailAddress": e}))
    batch.execute()
    

    References: