We're using the following code to add a table with 3 columns and a number of rows determined by the number of items in a collection. The first row in the table has headers. We want to bold them but I can't figure out how to do that. I was reading through this but it wasn't making sense on how to get the start and end index of the text in the header row.
var body = new BatchUpdateDocumentRequest {Requests = new List<Request>()
{
new Request()
{
InsertTable = new InsertTableRequest()
{
EndOfSegmentLocation = new EndOfSegmentLocation
{
SegmentId = ""
},
Columns = 3,
Rows = contractAddendums.Items.Count
}
}
}};
docService.Documents.BatchUpdate(body, docId).Execute();
var doc = docService.Documents.Get(newDocId).Execute();
var table = doc.Body.Content.FirstOrDefault(x => x.Table != null).Table;
var requests = new List<Request>();
for (var i = contractAddendums.Items.Count - 1; i > -1; i--){
var row = contractAddendums.Items[i];
var r1 = new Request()
{
InsertText = new InsertTextRequest()
{
Text = row.Text,
Location = new Location()
{
Index = table.TableRows[i].TableCells[2].Content[0].StartIndex
}
}
};
var r2 = new Request()
{
InsertText = new InsertTextRequest()
{
Text = row.Variable,
Location = new Location()
{
Index = table.TableRows[i].TableCells[1].Content[0].StartIndex
}
}
};
var r3 = new Request()
{
InsertText = new InsertTextRequest()
{
Text = row.Title,
Location = new Location()
{
Index = table.TableRows[i].TableCells[0].Content[0].StartIndex
}
}
};
requests.Add(r1);
requests.Add(r2);
requests.Add(r3);
}
As requested here's a sample of the request body. The actual request is much longer but essentially the same as it's simply an array of the same type of request objects.
[{
"createNamedRange": null,
"createParagraphBullets": null,
"deleteContentRange": null,
"deleteNamedRange": null,
"deleteParagraphBullets": null,
"deletePositionedObject": null,
"deleteTableColumn": null,
"deleteTableRow": null,
"insertInlineImage": null,
"insertPageBreak": null,
"insertTable": null,
"insertTableColumn": null,
"insertTableRow": null,
"insertText": {
"endOfSegmentLocation": null,
"location": {
"index": 15806,
"segmentId": null,
"ETag": null
},
"text": "asdfasdfad",
"ETag": null
},
"replaceAllText": null,
"updateParagraphStyle": null,
"updateTableColumnProperties": null,
"updateTableRowStyle": null,
"updateTextStyle": null,
"ETag": null
}, {
"createNamedRange": null,
"createParagraphBullets": null,
"deleteContentRange": null,
"deleteNamedRange": null,
"deleteParagraphBullets": null,
"deletePositionedObject": null,
"deleteTableColumn": null,
"deleteTableRow": null,
"insertInlineImage": null,
"insertPageBreak": null,
"insertTable": null,
"insertTableColumn": null,
"insertTableRow": null,
"insertText": {
"endOfSegmentLocation": null,
"location": {
"index": 15804,
"segmentId": null,
"ETag": null
},
"text": "asdfasdf",
"ETag": null
},
"replaceAllText": null,
"updateParagraphStyle": null,
"updateTableColumnProperties": null,
"updateTableRowStyle": null,
"updateTextStyle": null,
"ETag": null
}]
If my understanding is correct, how about this answer? Please think of this as just one of several answers.
At your request body, it is found that the text is inserted. In order to achieve above, it is required to update the text style. But at first, it is required to retrieve the indexes of cells of the header row.
Here, I would like to explain this flow using a sample situation. As the sample situation, it uses the following Google Document. As a test case for your situation, the texts of header1
, header2
and header3
of the header row are modified as the bold style.
Retrieve the table using the method of documents.get of Docs API.
body(content(table(tableRows(tableCells(content(paragraph(elements(endIndex,startIndex,textRun/content))))))))
The endpoint is as follows.
GET https://docs.googleapis.com/v1/documents/{documentId}?fields=body(content(table(tableRows(tableCells(content(paragraph(elements(endIndex%2CstartIndex%2CtextRun%2Fcontent))))))))
When the method of documents.get is requested with above endpoint, the following value is returned.
{"body":{"content":[{},{},{},{},
{"table":{
"tableRows":[
{"tableCells":[
{"content":[{"paragraph":{"elements":[{"startIndex":14,"endIndex":22,"textRun":{"content":"header1\n"}}]}}]},
{"content":[{"paragraph":{"elements":[{"startIndex":23,"endIndex":31,"textRun":{"content":"header2\n"}}]}}]},
{"content":[{"paragraph":{"elements":[{"startIndex":32,"endIndex":40,"textRun":{"content":"header3\n"}}]}}]}
]},
{"tableCells":[
{"content":[{"paragraph":{"elements":[{"startIndex":42,"endIndex":49,"textRun":{"content":"value1\n"}}]}}]},
{"content":[{"paragraph":{"elements":[{"startIndex":50,"endIndex":57,"textRun":{"content":"value2\n"}}]}}]},
{"content":[{"paragraph":{"elements":[{"startIndex":58,"endIndex":65,"textRun":{"content":"value3\n"}}]}}]}
]},
{"tableCells":[
{"content":[{"paragraph":{"elements":[{"startIndex":67,"endIndex":74,"textRun":{"content":"value4\n"}}]}}]},
{"content":[{"paragraph":{"elements":[{"startIndex":75,"endIndex":82,"textRun":{"content":"value5\n"}}]}}]},
{"content":[{"paragraph":{"elements":[{"startIndex":83,"endIndex":90,"textRun":{"content":"value6\n"}}]}}]}
]}
]
}},
{},{},{},{}]}}
The first index of tableRows
is the header row.
Retrieve the indexes of header1
, header2
and header3
.
"startIndex":14,"endIndex":22
, "startIndex":23,"endIndex":31
and "startIndex":32,"endIndex":40
are the indexes for header1
, header2
and header3
, respectively.The endpoint is as follows.
POST https://docs.googleapis.com/v1/documents/{documentId}:batchUpdate
The request body is as follows.
{"requests":[
{"updateTextStyle":{"range":{"startIndex":14,"endIndex":22},"textStyle":{"bold":true},"fields":"bold"}},
{"updateTextStyle":{"range":{"startIndex":23,"endIndex":31},"textStyle":{"bold":true},"fields":"bold"}},
{"updateTextStyle":{"range":{"startIndex":32,"endIndex":40},"textStyle":{"bold":true},"fields":"bold"}}
]}
When this endpoint with the request body is requested, the following result can be obtained.