I'm trying to append a heading and paragraph into an existing google docs document, I'm able to insert text at the end of document using:
{
'insertText': {
'endOfSegmentLocation': {},
'text': '\n' + HEADING + '\n'
}
}
But when we want to apply styling with insert text we need a start_index and end_index, the problem is I don't have start_index for this as I'm appending it to the end of document.
Complete code:
requests = [
{
'insertText': {
'endOfSegmentLocation': {},
'text': '\n' + HEADING + '\n'
}
},
{
'updateParagraphStyle': {
'range': {
'startIndex': HOW TO FIND START INDEX?,
'endIndex': len(HEADING)
},
'paragraphStyle': {
'namedStyleType': 'HEADING_1',
'spaceAbove': {
'magnitude': 10.0,
'unit': 'PT'
},
'spaceBelow': {
'magnitude': 10.0,
'unit': 'PT'
}
},
'fields': 'namedStyleType,spaceAbove,spaceBelow'
}
},
{
'insertText': {
'endOfSegmentLocation': {},
'text': PARAGRAPHS + '\n'
}
},
]
result = service.documents().batchUpdate(
documentId=DOCUMENT_ID, body={'requests': requests}).execute()