6

When manually editing a Google Sheet, I notice that you can make part of a cell bold by simply highlighting a portion of the text and clicking the 'bold' button.

Is there way to achieve this same behavior using the API v4? I cannot seem to find anything in the documentation https://developers.google.com/sheets/api

pic of partially bolded cell in imgur

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
travis
  • 63
  • 1
  • 7
  • Did my answer show you the result what you want? Would you please tell me about it? That is also useful for me to study. If this works, other people who have the same issue with you can also base your question as a question which can be solved. If you have issues for my answer yet, I apologize. At that time, can I ask you about your current situation? I would like to study to solve your issues. – Tanaike Dec 11 '19 at 00:41

1 Answers1

6

I think that "TextFormatRun" with spreadsheets.batchUpdate in Sheets API can be achieved your goal.

As the sample situation, it supposes that the cell "A1" on the Spreadsheet has the value of hi i'm bold - and I'm not and you want to do the bold type hi i'm bold of hi i'm bold - and I'm not.

In this case, the following endpoint and request body can achieve this.

Endpoint:

https://sheets.googleapis.com/v4/spreadsheets/###spreadsheetId###:batchUpdate

Request body:

{"requests": [
    {"updateCells": {
        "range": {"sheetId": "###sheetId###", "startRowIndex": 0, "endRowIndex": 1, "startColumnIndex": 0, "endColumnIndex": 1},
        "rows": [{"values": [{"textFormatRuns":[
            {"format": {"bold": true}, "startIndex": 0},
            {"format": {"bold": false}, "startIndex": 11}
        ]}]}],
        "fields": "textFormatRuns.format.bold"
    }}
]}
  • When you use this, please set the Spreadsheet ID and sheet ID.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Hi Tanaike, thanks for the answer! At first I unintentionally updated an empty cell. This returned a 400 error: `"Invalid requests[0].updateCells: Can only set textFormatRuns on non-computed string values"`. – travis Dec 11 '19 at 01:36
  • Also, I noticed that if you try and update a range with more than one cell, it will only affect the first cell in the range. – travis Dec 11 '19 at 01:39
  • @travis Thank you for replying. I'm glad your issue was resolved. This answer is for one cell from your question. When you want to modify the several cells, please set the object to `values` and `rows`. [Ref](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#updatecellsrequest) – Tanaike Dec 11 '19 at 02:37
  • @travis Or if you want to modify the several cells with the same format, please use [RepeatCellRequest](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#repeatcellrequest). If you want to know the detail information, can you post it by including the detail situation you expect as new question? By this, it will help users including me think of the solution. I would like to support you. – Tanaike Dec 11 '19 at 02:37