Search code examples
google-sheetsgoogle-sheets-api

GoogleSheets API does not work. It does not let to strikethrough with API


One can reproduce the problem.

  1. Create new Spreadsheet.

  2. Take SpreadsheetId (https://docs.google.com/spreadsheets/d/1rp11nqj0t0x1111111111111111111111111137Wj4XU/edit#gid=0, here it is 1rp11nqj0t0x1111111111111111111111111137Wj4XU)

  3. Fill range A1:F15 with any content, let's use 'lorem ipsum' string, for instance.

  4. Visit https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate 4.1 Look at right side where one can find "Try this method" 4.2 Fill field "spreadsheetId" with one you've taken on step 2. 4.3 Fill "Request body" with

    { "requests": [ { "repeatCell": { "cell": { "userEnteredFormat": { "textFormat": { "strikethrough": true } } }, "fields": "*", "range": { "startRowIndex": 2, "endRowIndex": 2, "startColumnIndex": 1, "endColumnIndex": 3 } } } ] }

4.4 Press "Execute" button at the bottom of right side. 5. Check you spreadshet. 6. No error, nothing happened.

What am I doing wrong ?


Solution

  • Modification points:

    • In your following request body, the values of startRowIndex and endRowIndex are the same. By this, this request body cannot be correctly worked. I thought that this might be the reason for your current issue of No error, nothing happened..

      {
         "requests":[
            {
               "repeatCell":{
                  "cell":{
                     "userEnteredFormat":{
                        "textFormat":{
                           "strikethrough":true
                        }
                     }
                  },
                  "fields":"*",
                  "range":{
                     "startRowIndex":2,
                     "endRowIndex":2,
                     "startColumnIndex":1,
                     "endColumnIndex":3
                  }
               }
            }
         ]
      }
      
    • And also, in your request body, even when the above modification is reflected, the cell values are cleared by "fields":"*". Please be careful about this.

    When these points are reflected in your request body, how about the following modification?

    Modified request body:

    {
      "requests": [
        {
          "repeatCell": {
            "cell": {
              "userEnteredFormat": {
                "textFormat": {
                  "strikethrough": true
                }
              }
            },
            "fields": "userEnteredFormat.textFormat.strikethrough",
            "range": {
              "startRowIndex": 0,
              "endRowIndex": 15,
              "startColumnIndex": 0,
              "endColumnIndex": 6,
            }
          }
        }
      ]
    }
    
    • In this modified request body, from Fill range A1:F15 with any content, let's use 'lorem ipsum' string, for instance., the cells "A1:F15" are used. So, strikethrough is used for these cells.

    • In your request body, sheetId is not used. So, in this case, the 1st tab in Google Spreadsheet is used. Please be careful about this. If you want to use it for the specific sheet, please add the property of "sheetId": ### to range. ### is the sheet ID.

    References: