为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过文档 API 向 Google 文档添加文本?

如何通过文档 API 向 Google 文档添加文本?

Go
眼眸繁星 2022-08-30 21:59:17
我正在尝试通过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)    }
查看完整描述

2 回答

?
小唯快跑啊

TA贡献1863条经验 获得超2个赞

查看他们的文档,您可以看到InsertTextRequest,这可能就是您正在寻找的。

虽然文档没有给你任何Go示例,但API在其他语言中是相似的:https://developers.google.com/docs/api/how-tos/move-text

(我不能评论,这就是为什么这是一个答案)。


查看完整回答
反对 回复 2022-08-30
?
饮歌长啸

TA贡献1951条经验 获得超3个赞

这个答案有点晚了:

  1. go api 现在可以在这里找到:

  2. 关于代码的细节,它应该有效。我在下面添加了一个步骤对步骤的方法。您必须首先创建结构,然后将其指针分配给请求结构。

长路代码:

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


查看完整回答
反对 回复 2022-08-30
  • 2 回答
  • 0 关注
  • 107 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号