我们正在使用以下代码添加一个表,该表具有3列,并且行的数量由集合中的项目数确定。表中的第一行具有标题。我们想要加粗它们,但我不知道该怎么做。我正在阅读this,但如何获取标题行中文本的开始和结束索引没有意义。
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);
}
根据要求,以下是请求正文的示例。实际的请求要长得多,但实际上只是一个相同类型的请求对象的数组。
[{
"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
}]
答案 0 :(得分:0)
如果要加粗一些文本。您需要提交类似于以下内容的请求:
requests = [
{
'updateTextStyle': {
'range': {
'startIndex': 1,
'endIndex': 5
},
'textStyle': {
'bold': True,
},
'fields': 'bold'
}
},
]
range: {'startIndex': 1, 'endIndex':5}
应该引用应为粗体的文本范围。
答案 1 :(得分:0)
如果我的理解正确,那么这个答案如何?请认为这只是几个答案之一。
在您的请求正文中,发现插入了文本。为了达到上述目的,需要更新文本样式。但是首先,需要检索标题行的单元格索引。
在这里,我想用一个样本情况来解释这个流程。作为示例情况,它使用以下Google文档。作为您的情况的测试用例,将标题行的header1
,header2
和header3
的文本修改为粗体样式。
使用Docs API的documents.get方法检索表。
body(content(table(tableRows(tableCells(content(paragraph(elements(endIndex,startIndex,textRun/content))))))))
端点如下。
GET https://docs.googleapis.com/v1/documents/{documentId}?fields=body(content(table(tableRows(tableCells(content(paragraph(elements(endIndex%2CstartIndex%2CtextRun%2Fcontent))))))))
当在上述端点处请求document.get方法时,将返回以下值。
{"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"}}]}}]}
]}
]
}},
{},{},{},{}]}}
tableRows
的第一个索引是标题行。
检索header1
,header2
和header3
的索引。
"startIndex":14,"endIndex":22
,"startIndex":23,"endIndex":31
和"startIndex":32,"endIndex":40
是header1
,header2
和{{1}的索引}。端点如下。
header3
请求正文如下。
POST https://docs.googleapis.com/v1/documents/{documentId}:batchUpdate
当请求带有请求主体的此端点时,可以获得以下结果。