i have a functional PHP script to add plain text into Google Document, but I'm struggling to insert paragraphs with styles. What I want to achieve is
There is insertText
request but it's not possible to style this text directly in request. And there is not insertParagraph, so everytime when i insert text i cant even make a new line. https://developers.google.com/docs/api/reference/rest/v1/documents/request
It's frustrating how difficult is to work with Google Docs API, it should be much easier to do such basic tasks, please help.
I believe your goal is as follows.
In this case, how about the following sample request body?
In order to achieve your goal, when the batchUpdate method is used, as a sample flow, the following flow is used.
Sample1\n
and Sample2\n
are inserted.Sample1\n
and Sample2\n
, respectively.https://www.google.com
is set to the text of Sample2\n
.When this flow is reflected in a request body, it becomes as follows. Of course, for example, you can change this flow like "1. Insert Sample1\n
. 2. Set paragraph style. 3. Insert Sample2\n
. 4. Set paragraph style. 5. Set text style.".
{
"requests": [
{
"insertText": {
"text": "Sample1\n",
"location": {
"index": 1
}
}
},
{
"insertText": {
"text": "sample2\n",
"location": {
"index": 9
}
}
},
{
"updateParagraphStyle": {
"range": {
"startIndex": 1,
"endIndex": 8
},
"paragraphStyle": {
"namedStyleType": "HEADING_1"
},
"fields": "namedStyleType"
}
},
{
"updateParagraphStyle": {
"range": {
"startIndex": 9,
"endIndex": 17
},
"paragraphStyle": {
"namedStyleType": "NORMAL_TEXT"
},
"fields": "namedStyleType"
}
},
{
"updateTextStyle": {
"range": {
"startIndex": 9,
"endIndex": 16
},
"textStyle": {
"link": {
"url": "https://www.google.com"
}
},
"fields": "link"
}
}
]
}
When you test this request body with "Try this API" of batchUpdate method of Docs API, you can do it at this link. In this link, the request body has already been set. Please prepare a sample Google Document and set your Google Document ID.
When this request body is used for a Google Document, the following result is obtained.