Search code examples
javagoogle-apigoogle-docsgoogle-docs-api

How to set body for google docs while creating a document through google docs API?


I am able to access my google docs for content, for a use case I need to access a document get it's content and paste this content multiple times in another google doc. But I am unable to create a google doc with initialized content

        Document response = service.documents().get(DOCUMENT_ID).execute();

This is what I am using to retrieve the document that I need to copy.

response.getBody();

Gives me the content that I now want to replicate and paste in the new Document created by

 Document doc = new Document()
            .setTitle("Not Working!").setBody(response.getBody());
    doc = service.documents().create(doc)
            .execute();

setBody reference : Link Is there anything that I am doing wrong?


Solution

  • Answer:

    You can't do this.

    More Information:

    As per the documentation on the documents.create method (emphasis my own):

    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.

    This is why creating a new document with content is not working. You need to use the documents.create method to create a document, which will return the cretaed document, and then use documents.batchUpdate to update this document with the desired content.

    References: