【问题标题】:How can I add text to a Google Docs document via Docs API?如何通过 Docs API 向 Google Docs 文档添加文本?
【发布时间】:2021-05-20 12:36:06
【问题描述】:

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

【问题讨论】:

  • 请问but I don't know how can I edit this fileI still couldn't solve the problem的详细情况?如果出现错误,请出示。

标签: go google-docs google-docs-api


【解决方案1】:

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

虽然文档没有提供任何 Go 示例,但其他语言的 API 类似:https://developers.google.com/docs/api/how-tos/move-text

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

【讨论】:

  • 感谢您的回复,我仍然无法解决问题,我什至找不到 Golang 的代码示例,而且我是 Golang 的新手。
  • @pangea,就像我说的,看other语言的例子,A​​PI不会有太大变化。
【解决方案2】:

这个答案有点晚了:

  1. go api 现已推出 here:
  2. 关于代码的细节,它应该可以工作。我在下面添加了一步一步的方法。您必须先创建结构,然后将它们的指针分配给 Request 结构。

漫长的代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多