Search code examples
google-sheetsgoogle-apps-scriptgoogle-sheets-api

Google Sheets API - Append Request not working


Having some difficulty with forming the request to append data on a Google Sheet using the API v4:

Referring to this link, I have attempted to use the append method in my Google App Script, but I am getting an error message, which does not make sense, as I have tried every single combination?

Code Snippet

var request = {
    spreadsheetId: spreadsheetId,
    range: sheetName + "!A:D",
    insertDataOption: "INSERT_ROWS",
    responseDateTimeRenderOption: "SERIAL_NUMBER",
    responseValueRenderOption: "FORMATTED_VALUE",
    valueInputOption: "USER_ENTERED",
    includeValuesInResponse: true,
    resource: {
        range: sheetName + "!A:D",
        majorDimension: "ROWS",
        values: [["2018-01-01", "2", "3", "4"]]
    }
};

Sheets.Spreadsheets.Values.append(request);

Error

Exception: Invalid number of arguments provided. Expected 3-4 only

I wish there were better samples and documentation to help here?

Thanks in Advance


Solution

  • Try this:

    function appendDataToSheet() {
      var spreadsheetId = "sheetID";
      var sheetName = "sheetName";  
      var resource = {
        "values": [["2018-01-01", "2", "3", "4"]]
      };
      var options = {
        insertDataOption: "INSERT_ROWS",
        responseDateTimeRenderOption: "SERIAL_NUMBER",
        responseValueRenderOption: "FORMATTED_VALUE",
        valueInputOption: "USER_ENTERED",
        includeValuesInResponse: true
        }
      Sheets.Spreadsheets.Values.append(resource, spreadsheetId, sheetName + "!A:D", options);
    }