Home > Software design >  Is there a way to add collaborators to a Google Sheet using the Golang Client Library or REST API?
Is there a way to add collaborators to a Google Sheet using the Golang Client Library or REST API?

Time:01-06

I am able to create a new spreadsheet with the gsheets client library, and my next step is to add editors to that newly created sheet so that the sheet can be accessed by the users of the application

Here is the code for creating the sheet:

    ctx := context.Background()

    srv, err := gsheets.NewService(ctx)
    if err != nil {
        log.Printf("Unable to retrieve Sheets client: %v", err)
    }
    sp := &gsheets.Spreadsheet{
        Properties: &gsheets.SpreadsheetProperties{
            Title: groupName,
        },
    }

    spreadsheet, err := srv.Spreadsheets.Create(sp).Do()
    if err != nil {
        return nil, err
    }

I have searched through the golang client library docs and the REST API docs, and I am unable to find anything related to adding collaborators

I was expecting there to be some request object that allows me to add a collaborator using their email and role:

    req := gsheets.Request{
        AddCollaborator: &gsheets.AddCollaboratorRequest{
            Email: "[email protected]",
            Role:  "editor",
        },
    }

    busr := &gsheets.BatchUpdateSpreadsheetRequest{
        Requests: []*gsheets.Request{&req},
    }

    res, err := srv.Spreadsheets.BatchUpdate(spreadsheetId, busr).Do()


or at the very least I was expecting there to be an API endpoint where I can achieve the same result

I am also curious if there is a way to create this new spreadsheet as read only to the public? This would at least allow me to continue developing

CodePudding user response:

It is possible to add editors with the google.golang.org/api/sheets/v4 library. You can simply create a spreadsheet with:

func (r *SpreadsheetsService) Create(spreadsheet *Spreadsheet) *SpreadsheetsCreateCall

and add editors with Editor type:

type Editors struct {
    ...
    // Users: The email addresses of users with edit access to the protected
    // range.
    Users []string `json:"users,omitempty"`
    ...
}

Check library docs for more details.

  • Related