Document Creation Method: documents.create
- Initiates the creation of a new blank document with the specified title. All other fields in the request, including any content provided, will be disregarded.
If you wish to add text to your newly created document, follow these steps:
1. Obtain the document id of the newly created document. Your create request should return a REST Resource:document where the document id can be retrieved
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. Utilize the documents.batchUpdate method to insert your text into the document using the document id.
Sample Request Body:
{
"requests": [
{
"insertText": {
"location": {
"index": 1
},
"text": "Hello World"
}
}
]
}
- In this illustration, I utilized InsertTextRequest to insert text into the document at a specific location of choice (index 1)
Output:
https://i.sstatic.net/PzBDT.png
Additional Notes:
To delve into the internal framework of a Google Docs document: understanding the elements that compose a document and their relationships, as well as indexing and positioning.
Refer to this link: https://developers.google.com/docs/api/concepts/structure
To explore request methods and responses further.
Refer to this link: https://developers.google.com/docs/api/concepts/request-response
For comprehensive REST Resources, visit: https://developers.google.com/docs/api/reference/rest