通过 Python gspread Api 在 Google 工作表中将特定部分加粗
Make Specific Part Bold in Google Sheets via Python gspread Api
我一直在尝试使用 Gspread API 在我的单元格中加粗部分。但我不知道该怎么做。我找到了一种方法 但我无法集成到 Gspread API
That shows how I something wanted
我相信你的目标如下。
- 您想将
I would like to make bold specific part of my cell
中 bold specific part
的文字设置为粗体。
- 您想使用 python 的 gspread 实现此目的。
- 您已经能够使用表格 API.
获取值并将值放入 Google 电子表格
在这种情况下,下面的示例脚本怎么样?
示例脚本:
spreadsheetId = "###" # Please set the Spreadsheet ID.
sheetName = "Sheet1" # Please set the sheet ID.
client = gspread.authorize(creds) # Please use your authorization script.
spreadsheet = client.open_by_key(spreadsheetId)
sheetId = spreadsheet.worksheet(sheetName).id
text = "I would like to make bold specific part of my cell"
gridRange = {"sheetId": sheetId, "startRowIndex": 0, "endRowIndex": 1, "startColumnIndex": 0, "endColumnIndex": 1}
reqs = [
{"updateCells": {
"range": gridRange,
"rows": [{"values": [{"userEnteredValue": {"stringValue": text}}]}],
"fields": "userEnteredValue"
}},
{"updateCells": {
"range": gridRange,
"rows": [{"values": [{"textFormatRuns": [
{"format": {"bold": True}, "startIndex": 21},
{"format": {"bold": False}, "startIndex": 39}
]}]}],
"fields": "textFormatRuns.format.bold"
}}
]
res = spreadsheet.batch_update({"requests": reqs})
- 在此示例脚本中,将
I would like to make bold specific part of my cell
的文本放入“Sheet1”的单元格“A1”,并将粗体设置为单元格“A1”中的 bold specific part
的文本" 使用 batchUpdate 方法。
结果:
当此脚本为运行时,得到如下结果
注:
如果你的单元格“A1”已经有I would like to make bold specific part of my cell
的文本,你也可以使用下面的请求正文。
reqs = [
{"updateCells": {
"range": gridRange,
"rows": [{"values": [{"textFormatRuns": [
{"format": {"bold": True}, "startIndex": 21},
{"format": {"bold": False}, "startIndex": 39}
]}]}],
"fields": "textFormatRuns.format.bold"
}}
]
参考文献:
我一直在尝试使用 Gspread API 在我的单元格中加粗部分。但我不知道该怎么做。我找到了一种方法
That shows how I something wanted
我相信你的目标如下。
- 您想将
I would like to make bold specific part of my cell
中bold specific part
的文字设置为粗体。 - 您想使用 python 的 gspread 实现此目的。
- 您已经能够使用表格 API. 获取值并将值放入 Google 电子表格
在这种情况下,下面的示例脚本怎么样?
示例脚本:
spreadsheetId = "###" # Please set the Spreadsheet ID.
sheetName = "Sheet1" # Please set the sheet ID.
client = gspread.authorize(creds) # Please use your authorization script.
spreadsheet = client.open_by_key(spreadsheetId)
sheetId = spreadsheet.worksheet(sheetName).id
text = "I would like to make bold specific part of my cell"
gridRange = {"sheetId": sheetId, "startRowIndex": 0, "endRowIndex": 1, "startColumnIndex": 0, "endColumnIndex": 1}
reqs = [
{"updateCells": {
"range": gridRange,
"rows": [{"values": [{"userEnteredValue": {"stringValue": text}}]}],
"fields": "userEnteredValue"
}},
{"updateCells": {
"range": gridRange,
"rows": [{"values": [{"textFormatRuns": [
{"format": {"bold": True}, "startIndex": 21},
{"format": {"bold": False}, "startIndex": 39}
]}]}],
"fields": "textFormatRuns.format.bold"
}}
]
res = spreadsheet.batch_update({"requests": reqs})
- 在此示例脚本中,将
I would like to make bold specific part of my cell
的文本放入“Sheet1”的单元格“A1”,并将粗体设置为单元格“A1”中的bold specific part
的文本" 使用 batchUpdate 方法。
结果:
当此脚本为运行时,得到如下结果
注:
如果你的单元格“A1”已经有
I would like to make bold specific part of my cell
的文本,你也可以使用下面的请求正文。reqs = [ {"updateCells": { "range": gridRange, "rows": [{"values": [{"textFormatRuns": [ {"format": {"bold": True}, "startIndex": 21}, {"format": {"bold": False}, "startIndex": 39} ]}]}], "fields": "textFormatRuns.format.bold" }} ]