Home > Backend Development > Golang > Is there a way to add collaborators to Google Sheet using Golang client library or REST API?

Is there a way to add collaborators to Google Sheet using Golang client library or REST API?

WBOY
Release: 2024-02-09 11:54:21
forward
624 people have browsed it

有没有办法使用 Golang 客户端库或 REST API 将协作者添加到 Google Sheet?

php editor Zimo is here to tell you that to add collaborators to Google Sheet, there are two methods to choose from: using the Golang client library or REST API. Using the Golang client library, you can add collaborators by calling the Google Sheets API. The method of using the REST API is to send an HTTP request to the Google Sheets API. No matter which method you choose, you can easily add collaborators to your Google Sheet to share and edit the sheet with others.

Question content

I can create a new spreadsheet using the gsheets client library, the next step is to add the editor to the newly created worksheet so that it is accessible to users of the application This worksheet

The following is the code to create the worksheet:

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
    }
Copy after login

I searched the golang client library documentation and rest api documentation but couldn't find anything related to adding collaborators

I expect to have some request objects that allow me to add collaborators using their emails and roles:

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()
Copy after login

Or at least I expected there to be an api endpoint where I could achieve the same result

I'm also curious if there is a way to create this new spreadsheet as read-only public? This would at least allow me to continue development

Workaround

The editor can be added using the google.golang.org/api/sheets/v4 library. You can simply create a spreadsheet:

func (r *spreadsheetsservice) create(spreadsheet *spreadsheet) *spreadsheetscreatecall
Copy after login

and add an editor with editor type:

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

See library documentation for more details.

The above is the detailed content of Is there a way to add collaborators to Google Sheet using Golang client library or REST API?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template