如何使用 google 文档 api 在 google 文档中加粗文本
How to bold text in a google doc using the google docs api
我们使用以下代码来添加一个 table,它有 3 列,行数由 collection 中的项目数决定。 table 中的第一行有 header。我们想把它们加粗,但我不知道该怎么做。我正在通读 this 但对于如何获取 header 行中文本的开始和结束索引没有意义。
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);
}
根据要求,这里是请求示例 body。实际请求要长得多,但本质上是相同的,因为它只是一个相同类型请求的数组 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
}]
如果你想加粗一些文字。您需要提交类似如下的请求:
requests = [
{
'updateTextStyle': {
'range': {
'startIndex': 1,
'endIndex': 5
},
'textStyle': {
'bold': True,
},
'fields': 'bold'
}
},
]
range: {'startIndex': 1, 'endIndex':5}
应引用应为粗体的文本范围。
- 您想修改 table.
中文本的文本样式
- 您想修改 header 的文本样式,即 table 的第一行。
- 您已经能够使用 Google 文档 API.
放置和获取 Google 文档
如果我的理解是正确的,这个答案怎么样?请将此视为几个答案之一。
问题:
应您的要求body,发现插入了文字。为了实现上述目标,需要更新文本样式。但首先,需要检索header行的单元格索引。
解决方案:
在这里,我想用一个示例情况来解释这个流程。作为示例情况,它使用以下 Google 文件。作为您的情况的测试用例,header行的header1
、header2
和header3
的文本被修改为粗体。
流量:
使用文档API.documents.get的方法检索table。
- 到时候使用下面的"fields"参数时,可以增加数据的可读性
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))))))))
当使用上述端点请求documents.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
的第一个索引是 header 行。
检索header1
、header2
和header3
的索引。
- 从检索到的table数据中,发现
"startIndex":14,"endIndex":22
、"startIndex":23,"endIndex":31
和"startIndex":32,"endIndex":40
是header1
、[=15=的索引] 和 header3
。
- 使用documents.batchUpdate的方法,用这些值,可以将header行文本的文字样式修改为粗体。
终点如下
POST https://docs.googleapis.com/v1/documents/{documentId}:batchUpdate
请求body如下
{"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"}}
]}
请求body这个端点时,可以得到如下结果
结果:
参考文献:
我们使用以下代码来添加一个 table,它有 3 列,行数由 collection 中的项目数决定。 table 中的第一行有 header。我们想把它们加粗,但我不知道该怎么做。我正在通读 this 但对于如何获取 header 行中文本的开始和结束索引没有意义。
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);
}
根据要求,这里是请求示例 body。实际请求要长得多,但本质上是相同的,因为它只是一个相同类型请求的数组 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
}]
如果你想加粗一些文字。您需要提交类似如下的请求:
requests = [
{
'updateTextStyle': {
'range': {
'startIndex': 1,
'endIndex': 5
},
'textStyle': {
'bold': True,
},
'fields': 'bold'
}
},
]
range: {'startIndex': 1, 'endIndex':5}
应引用应为粗体的文本范围。
- 您想修改 table. 中文本的文本样式
- 您想修改 header 的文本样式,即 table 的第一行。
- 您已经能够使用 Google 文档 API. 放置和获取 Google 文档
如果我的理解是正确的,这个答案怎么样?请将此视为几个答案之一。
问题:
应您的要求body,发现插入了文字。为了实现上述目标,需要更新文本样式。但首先,需要检索header行的单元格索引。
解决方案:
在这里,我想用一个示例情况来解释这个流程。作为示例情况,它使用以下 Google 文件。作为您的情况的测试用例,header行的header1
、header2
和header3
的文本被修改为粗体。
流量:
使用文档API.documents.get的方法检索table。
- 到时候使用下面的"fields"参数时,可以增加数据的可读性
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))))))))
当使用上述端点请求documents.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
的第一个索引是 header 行。
检索
header1
、header2
和header3
的索引。- 从检索到的table数据中,发现
"startIndex":14,"endIndex":22
、"startIndex":23,"endIndex":31
和"startIndex":32,"endIndex":40
是header1
、[=15=的索引] 和header3
。 - 使用documents.batchUpdate的方法,用这些值,可以将header行文本的文字样式修改为粗体。
终点如下
POST https://docs.googleapis.com/v1/documents/{documentId}:batchUpdate
请求body如下
{"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"}} ]}
请求body这个端点时,可以得到如下结果
- 从检索到的table数据中,发现