简体   繁体   English

通过 Python gspread Api 在 Google 表格中将特定部分加粗

[英]Make Specific Part Bold in Google Sheets via Python gspread Api

I have been trying to make a bold part of my cell using Gspread API. But I couldn't figure out how to do that.我一直在尝试使用 Gspread API 为我的手机制作一个大胆的部分。但我不知道该怎么做。 I found a way in that question but I couldn't integrate to Gspread API我在那个问题中找到了一种方法,但我无法集成到 Gspread API

That shows how I something wanted这表明我想要什么

I believe your goal is as follows.我相信你的目标如下。

  • You want to set the bold type to the text of bold specific part in I would like to make bold specific part of my cell .您想将粗体类型设置为bold specific part的文本I would like to make bold specific part of my cell
  • You want to achieve this using gspread for python.您想使用 gspread 实现此目的 python。
  • You have already been able to get and put values to Google Spreadsheet using Sheets API.您已经能够使用表格 API 获取值并将值放入 Google 电子表格。

In this case, how about the following sample script?在这种情况下,下面的示例脚本怎么样?

Sample script:示例脚本:

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})
  • In this sample script, a text of I would like to make bold specific part of my cell is put to a cell "A1" of "Sheet1", and the bold is set to the text of bold specific part in the cell "A1" using the batchUpdate method.在此示例脚本中, I would like to make bold specific part of my cell的文本放入“Sheet1”的单元格“A1”,并将粗体设置为单元格“A1”中的bold specific part的文本使用 batchUpdate 方法。

Result:结果:

When this script is run, the following result is obtained.运行此脚本时,将获得以下结果。

在此处输入图像描述

Note:笔记:

  • If your cell "A1" has already had the text of I would like to make bold specific part of my cell , you can also use the following request body.如果您的单元格“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" }} ]

References:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在gspread for Python中的特定Google表格电子表格中存在持续的SpreadsheetNotFound错误 - Persistent SpreadsheetNotFound errors on specific Google Sheets spreadsheets in gspread for Python 有没有办法使用 Python 的 gspread API 自动调整 Google 表格中的列? - Is there a way to autofit a column in Google Sheets using gspread API for Python? 通过Gspread将.csv上传到Google表格 - Uploading .csv to Google Sheets via Gspread 通过 gspread 和谷歌表 API 更改谷歌表中的列格式 - Changing column format in google sheets by gspread and google sheets API 如何将 gspread api 中的数据格式化为 Google 表格 - How can I format data from gspread api into Google Sheets 使用 gspread 和 gspread_dataframe 从 Google 表格导入数据时强制使用特定的单元格类型 - Force a specific cell type when importing data from Google sheets with gspread and gspread_dataframe 在 Google 表格的最后一个填充行之后添加数据,使用 gspread python - Add data after the last filled line of Google Sheets, with gspread python 使用 gspread python 发送到 Google 表格的图像看起来很小 - Image sent to Google Sheets with gspread python appears tiny 写入Google表格时,python gspread连接超时 - python gspread connection time out when writing to google sheets 有没有办法用 gspread 或其他一些 Python 库取消合并 Google 表格单元格? - Is there a way to unmerge Google Sheets cells with gspread or some other Python library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM