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

我正在尝试通过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)

    }


眼眸繁星
浏览 153回答 2
2回答

小唯快跑啊

查看他们的文档,您可以看到InsertTextRequest,这可能就是您正在寻找的。虽然文档没有给你任何Go示例,但API在其他语言中是相似的:https://developers.google.com/docs/api/how-tos/move-text(我不能评论,这就是为什么这是一个答案)。

饮歌长啸

这个答案有点晚了:go api 现在可以在这里找到:关于代码的细节,它应该有效。我在下面添加了一个步骤对步骤的方法。您必须首先创建结构,然后将其指针分配给请求结构。长路代码: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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go