你可以通过Docs API向Google Docs文档添加文本。

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

How can I add text to a Google Docs document via Docs API?

问题

我正在尝试通过Google Docs API向现有的Google Docs文件添加文本,并且我正在使用Golang进行操作。我按照https://developers.google.com/docs/api/quickstart/go中的步骤进行操作,我可以访问文件,但是我不知道如何编辑该文件。

我找到了如何向文件添加文本的方法,以下是我代码的相关部分:

b := &docs.BatchUpdateDocumentRequest{
    Requests: []*docs.Request{
        {
            InsertText: &docs.InsertTextRequest{
                Text: "texttoadd",
                Location: &docs.Location{
                    Index: md.Body.Content[len(md.Body.Content)-1].EndIndex - 1,
                },
            },
        },
    },
}
_, err = srv.Documents.BatchUpdate("your_document_id", b).Do()
if err != nil {
    fmt.Println(err)
}
英文:

I'm trying to add text to an existing Google Docs file via Google Docs API and I'm using Golang for this. I followed steps in https://developers.google.com/docs/api/quickstart/go I can reach but I don't know how can I edit this file?

I found how to add text to file here is related part of my code :

b := &docs.BatchUpdateDocumentRequest{
		Requests: []*docs.Request{
			{
				InsertText: &docs.InsertTextRequest{
					Text: "texttoadd",
					Location: &docs.Location{
						Index: md.Body.Content[len(md.Body.Content)-1].EndIndex - 1,
					},
				},
			},
		},
	}
	_, err = srv.Documents.BatchUpdate("your_document_id", b).Do()
	if err != nil {
		fmt.Println(err)
	}

答案1

得分: 1

这个答案有点晚:

  1. Go API现在可以在这里找到:
  2. 关于你的代码的具体细节,它应该可以工作。我在下面提供了一种逐步的方法。你首先需要创建结构体,然后将它们的指针分配给Request结构体。

冗长的代码示例:

var batchreqs docs.BatchUpdateDocumentRequest    

var instxtreq docs.InsertTextRequest     

var txtloc docs.Location   

var breq docs.Request    

txtloc.Index = {你的索引}    

txtloc.SegmentID = "" // 有点多余    

instxtreq.Location = &txtloc   

instxtreq.Text = "你的新文本"    

breq.InsertText =&instxtreq   

var breqarray [1](*docs.Request)    

breqarray[0].InsertText = instxtreq   

breqslice = breqarray[:]

batchreqs.Request = &breqslice    

// 现在你可以执行BatchUpdate

_, err = srv.Documents.BatchUpdate("你的文档ID", &batchreqs).Do()

// batchreqs必须是一个指向现有batchupdaterequest结构体的指针
英文:

this answer is a bit late:

  1. the go api is now available here:
  2. regarding the specifics of your code, it should work. I added a step for step approach below. You have to create the structures first and then assign their pointers to the Request structure.

The long way code:

var batchreqs docs.BatchUpdateDocumentRequest    

var instxtreq docs.InsertTextRequest     

var txtloc docs.Location   

var breq docs.Request    

txtloc.Index = {your index}    

txtloc.SegmentID = "" // a bit superfluous    

instxtreq.Location = &txtloc   

instxtreq.Text = "your new text"    

breq.InsertText =&instxtreq   

var breqarray [1](*docs.Request)    

breqarray[0].InsertText = instxtreq   

breqslice = breqarray[:]

 batchreqs.Request = &breqslice    

// now you can do the BatchUpdate

 _, err = srv.Documents.BatchUpdate("your_document_id", &batchreqs).Do()

// the batch reqs must be a pointer to an existing batchupdaterequest structure

答案2

得分: 0

通过查看他们的文档,你可以看到InsertTextRequest,这可能是你要找的内容。

尽管文档中没有给出Go的示例,但在其他语言中,API的使用方式是相似的:https://developers.google.com/docs/api/how-tos/move-text

(我无法评论,所以这是一个回答)。

英文:

Looking at their documentation, you can see the InsertTextRequestand that is probably what you are looking for.

Although the documentation doesn't give you any Go example, the API is similar in other languages: https://developers.google.com/docs/api/how-tos/move-text

(I can't comment, that is why this is an answer).

huangapple
  • 本文由 发表于 2021年5月20日 20:36:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/67620488.html
匿名

发表评论

匿名网友

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

确定