简体   繁体   中英

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. I found a way in that question but I couldn't integrate to 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 .
  • You want to achieve this using gspread for python.
  • You have already been able to get and put values to Google Spreadsheet using Sheets API.

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.

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.

     reqs = [ {"updateCells": { "range": gridRange, "rows": [{"values": [{"textFormatRuns": [ {"format": {"bold": True}, "startIndex": 21}, {"format": {"bold": False}, "startIndex": 39} ]}]}], "fields": "textFormatRuns.format.bold" }} ]

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM