0
votes

I have a Google Form attached to a spreadsheet. Everytime the data is submitted via the form into the spreadsheet. Then the spreadsheet should automatically send an email to several individuals but, it's not working.

Here is my code:

function myFunction(row) {
  var sheets = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = sheets.getSheets()[0]; // Form Responses 1
  var columnName = sheet.getRange("A1:S1").getValues();
  var dataRange = sheet.getRange(row);
  var data = dataRange.getValues();

  var responseObj = {};
  var carId = "";

  for (var i = 2; i < columnName[0].length; i++) {
    var key = columnName[0][i];
    var value = dateFormat(data[0][i],key);
    responseObj[key] = value;
    if(key === "Car ID"){
      carId = value;
    }
  }

  var htmlBodyLoc = doGet(responseObj);

  MailApp.sendEmail({
    to: '[email protected],[email protected],[email protected]',
    subject:'Car Inspection - ' + outletId + ' '  + new Date(), 
    htmlBody: htmlBodyLoc,
  });
}

I am actually getting an error on this line: var dataRange = sheet.getRange(row);

Thanks.

1
What's the error you're getting? - Jonny
If you just run the function, row has no value--it is undefined. So you are not calling your function properly. - tehhowch
Here is the error I'm getting: Range not found (file "Code"). - tnlewis

1 Answers

0
votes

You have 4 ways to call sheet.getRange()

getRange(row, column) It is for single cells ranges. If you are from VBA, this is the most approximate to worksheet.Cells

getRange(row, column, numRows) This way you can make a range of singles columns and multiple rows, useful sometimes

getRange(row, column, numRows, numColumns) This is the way to go, programmatically. E.g sheet.getRange(1,1,2,2) will get you the range A1:B2

And finally there is getRange(a1notation) which you can use like sheet.getRange("A1:B2")

If you are trying to get a single cell of data, maybe you can just change var dataRange = sheet.getRange(row); To var dataRange = sheet.getRange(row, yourFixedColumn); yourFixedColumn being an Integral with index 1 for the column E.g column A = 1 Otherwise you must change the passed argument row to something that complies with the methods listed above.