Search code examples
javascriptgoogle-docs-api

How to add text to a Word document with Google Docs API?


I'm trying to create a Word document that contains text and images but I can't even make the text appear, I always have a blank document and it doesn't hurt my code.

If someone can tell me what I'm doing wrong, I would appreciate it.

gapi.client.docs.documents.create({
  resource: {
    title: "probando api de google docs",
    body: {
      content: [{
        paragraph: {
          elements: [{
            textRun: {
              content: "hi \n",
            },
          }, ],
        },
      }, ],
    },
  },
})

Solution

  • Method: documents.create

    • creates a blank document using the title given in the request. Other fields in the request, including any provided content, are ignored.

    If you want to insert a text in your newly created document, you can do the following:

    1. Get the document id of the newly created document. Your create request should return a REST Resource:document where you can obtain the document id

    Sample Response Body:

    {
      "title": "Test1",
      "body": {
        "content": [
          {
            "endIndex": 1,
            "sectionBreak": {
              "sectionStyle": {
                "columnSeparatorStyle": "NONE",
                "contentDirection": "LEFT_TO_RIGHT",
                "sectionType": "CONTINUOUS"
              }
            }
          },
          {
            "startIndex": 1,
            "endIndex": 2,
            "paragraph": {
              "elements": [
                {
                  "startIndex": 1,
                  "endIndex": 2,
                  "textRun": {
                    "content": "\n",
                    "textStyle": {}
                  }
                }
              ],
              "paragraphStyle": {
                "namedStyleType": "NORMAL_TEXT",
                "direction": "LEFT_TO_RIGHT"
              }
            }
          }
        ]
      },
    
    ....
    
    
      "revisionId": "xxxxxxxxxsamplerevisionid",
      "suggestionsViewMode": "SUGGESTIONS_INLINE",
      "documentId": "xxxxxxxxxsampledocumentid"
    }
    

    2. Use documents.batchUpdate method to insert your text in the document using your document id.

    Sample Request Body:

    {
      "requests": [
        {
          "insertText": {
            "location": {
              "index": 1
            },
            "text": "Hello World"
          }
        }
      ]
    }
    
    • In this example, I used InsertTextRequest to insert a text inside the document at a specific location that I want (which is in index 1)

    Output:

    enter image description here

    Additional Notes: To learn more about the internal structure of a Google Docs document: the elements that make up a document and the relationship between these elements, indexing and positioning. Refer to this link: https://developers.google.com/docs/api/concepts/structure

    To learn more about request methods and responses. Refer to this link: https://developers.google.com/docs/api/concepts/request-response

    For complete REST Resources, refer here: https://developers.google.com/docs/api/reference/rest