Unable to add a line chart with 3 series into a Google sheet using API

48 Views Asked by At

I have a project to monitorize some parameters with ESP32 (Arduino IDE sketch) and I save result into a gsheets. Every new day generate new sheet to save dates. After new sheet creation, I want to insert into sheet a line chart with 3 series using requests addChart. With one series is ok, but is more explicit evolution of 3 parameters so when I define 3 series in JSON I receive next error.

"error": {
        "code": 400,
        "message": "Invalid requests[0].addChart: If specifying more than one sourceRange within a domain or series, each sourceRange across the domain & series must be in order and contiguous.",
        "status": "INVALID_ARGUMENT"

Succinctly what I tried:

domain sources: A1:A1000 (time of event) on axis x
series1 sources: B1:B1000 (values of parameter A)
series2 sources: C1:C1000 (values of parameter B)
series2 sources: D1:D1000 (values of parameter C)

Using https://developers.google.com/sheets/api/samples/charts this is result:

{
  "requests": [
{
    "addChart": {
        "chart": {
            "spec": {
                "basicChart": {
                    "chartType": "LINE",
                    "domains": {
                        "domain": {
                            "sourceRange": {
                                "sources": [
                                    {
                                        "startRowIndex": 1,
                                        "endRowIndex": 1000,
                                        "startColumnIndex": 0,
                                        "endColumnIndex": 1,
                                        "sheetId": 1439439696
                                    },
                  {
                                        "startRowIndex": 1,
                                        "endRowIndex": 1000,
                                        "startColumnIndex": 0,
                                        "endColumnIndex": 1,
                                        "sheetId": 1439439696
                                    },
                  {
                                        "startRowIndex": 1,
                                        "endRowIndex": 1000,
                                        "startColumnIndex": 0,
                                        "endColumnIndex": 1,
                                        "sheetId": 1439439696
                                    }
                ]
                            }
                        }
                    },
                    "series": {
                        "series": {
                            "sourceRange": {
                                "sources": [
                  {
                                        "startRowIndex": 1,
                                        "endRowIndex": 1000,
                                        "startColumnIndex": 5,
                                        "endColumnIndex": 6,
                                        "sheetId": 1439439696
                                    },
                  {
                                        "startRowIndex": 1,
                                        "endRowIndex": 1000,
                                        "startColumnIndex": 6,
                                        "endColumnIndex": 7,
                                        "sheetId": 1439439696
                                    },
                                    {
                                        "startRowIndex": 1,
                                        "endRowIndex": 1000,
                                        "startColumnIndex": 7,
                                        "endColumnIndex": 8,
                                        "sheetId": 1439439696
                                    }
                  ]
                            }
                        }
                    }
                }
            },
            "position": {
                "overlayPosition": {
                    "anchorCell": {
                        "sheetId": 1439439696,
                        "rowIndex": 2,
                        "columnIndex": 2
                    }
       }
            }
        }
    }
}
}

Where am I wrong?

What I expected:

1

There are 1 best solutions below

1
On

From your following condition,

domain sources: A1:A1000 (time of event) on axis x series1 sources: B1:B1000 (values of parameter A) series2 sources: C1:C1000 (values of parameter B) series2 sources: D1:D1000 (values of parameter C)

In your showing condition, it seems that the columns "A" to "D" are used. But, when I saw your showing request body, it seems that the columns "F" to "H" are used. And also, unfortunately, it seems that your request body is an invalid structure for using the batchRequest. When these points are reflected in a sample request body, it become as follows.

Modified request body:

{
  "requests": [
    {
      "addChart": {
        "chart": {
          "spec": {
            "basicChart": {
              "chartType": "LINE",
              "domains": [
                {
                  "domain": {
                    "sourceRange": {
                      "sources": [
                        {
                          "sheetId": 1439439696,
                          "startRowIndex": 0,
                          "endRowIndex": 1000,
                          "startColumnIndex": 0,
                          "endColumnIndex": 1
                        }
                      ]
                    }
                  }
                }
              ],
              "series": [
                {
                  "series": {
                    "sourceRange": {
                      "sources": [
                        {
                          "sheetId": 1439439696,
                          "startRowIndex": 0,
                          "endRowIndex": 1000,
                          "startColumnIndex": 1,
                          "endColumnIndex": 2
                        }
                      ]
                    }
                  }
                },
                {
                  "series": {
                    "sourceRange": {
                      "sources": [
                        {
                          "sheetId": 1439439696,
                          "startRowIndex": 0,
                          "endRowIndex": 1000,
                          "startColumnIndex": 2,
                          "endColumnIndex": 3
                        }
                      ]
                    }
                  }
                },
                {
                  "series": {
                    "sourceRange": {
                      "sources": [
                        {
                          "sheetId": 1439439696,
                          "startRowIndex": 0,
                          "endRowIndex": 1000,
                          "startColumnIndex": 3,
                          "endColumnIndex": 4
                        }
                      ]
                    }
                  }
                }
              ],
              "headerCount": 1
            }
          },
          "position": {
            "overlayPosition": {
              "anchorCell": {
                "sheetId": 1439439696,
                "rowIndex": 2,
                "columnIndex": 2
              }
            }
          }
        }
      }
    }
  ]
}
  • In this request body, columns "A" to "D" are used.

Note:

  • Unfortunately, I do not know your actual Spreadsheet. But, when I tested this request body using a sample Spreadsheet and the sample values, no error occurred, and a new chart was inserted. If an error occurs, please confirm your sheet ID and values again.

References: