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

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)
    }


Solution 1:[1]

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).

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2