英文:
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
这个答案有点晚:
- Go API现在可以在这里找到:
- 关于你的代码的具体细节,它应该可以工作。我在下面提供了一种逐步的方法。你首先需要创建结构体,然后将它们的指针分配给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:
- the go api is now available here:
- 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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论