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

huangapple go评论90阅读模式
英文:

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

问题

我能够使用gsheets客户端库创建一个新的电子表格,接下来我要做的是将编辑者添加到这个新创建的表格中,以便应用程序的用户可以访问该表格。

以下是创建表格的代码:

ctx := context.Background()

srv, err := gsheets.NewService(ctx)
if err != nil {
    log.Printf("无法获取Sheets客户端:%v", err)
}
sp := &gsheets.Spreadsheet{
    Properties: &gsheets.SpreadsheetProperties{
        Title: groupName,
    },
}

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

我已经在golang客户端库文档REST API文档中搜索过,但是找不到任何与添加协作者相关的内容。

我原本期望有一些请求对象,可以使用协作者的电子邮件和角色来添加协作者:

req := gsheets.Request{
    AddCollaborator: &gsheets.AddCollaboratorRequest{
        Email: "person@gmail.com",
        Role:  "editor",
    },
}

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

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

或者至少我期望有一个API端点可以实现相同的结果。

我还想知道是否有办法将这个新创建的电子表格设置为对公众只读?这样至少可以让我继续开发。

英文:

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: "person@gmail.com",
            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

答案1

得分: 0

可以使用google.golang.org/api/sheets/v4库来添加编辑器。

你可以简单地使用以下代码创建一个电子表格:

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

然后,可以使用Editor类型来添加编辑器:

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

更多详细信息,请参阅库文档

英文:

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.

huangapple
  • 本文由 发表于 2023年1月4日 13:27:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75001279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定