1

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

Tanaike
  • 181,128
  • 11
  • 97
  • 165
Okan
  • 11
  • 3
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 23 '22 at 11:10
  • First, I deeply apologize that my answer was not useful for your situation. From `I found a way in that question but I couldn't integrate to Gspread API`, I proposed a sample script for gspread. Could you please confirm it? When my proposed script is used, I could confirm that your expected result is obtained. So, please test it. – Tanaike Mar 23 '22 at 13:03

1 Answers1

1

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.

enter image description here

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165