Search code examples
arraysformsgoogle-apps-scriptrangesetvalue

google apps script: setValues from array, taken from html form onSubmit in google sheets


In Google Sheets, I have a sidebar using html, with a form which runs processForm(this) upon submission. The form was created based on the headings in the sheet, which is why I am using the headings to retrieve the values from the form. The code seems to work fine until I try to use setValues(). There is no error, but nothing seems to happen at that line. Please let me know what I might be doing wrong. Thanks.

function processForm(formObject) {
  var headers = getHeaders();
  var newRow = [];
  for (var i = 0; i < headers.length; i++) {
    newRow.push(formObject["" + headers[i]]); // TODO: convert objects to appropriate formats
  }
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange(parseInt(formObject.row)+1, 1, 1, headers.length)
  Logger.log(JSON.stringify(newRow)); // example output: ["John","Smith","male","6615554109","","example_email@yahoo.com"]
  range.setValues(newRow); // values not getting set
}

Solution

  • Change last line:

    range.setValues([newRow]);
    

    (thanks for the solution, Serge insas!)