This is what I have written in Script editor so as to auto-apply a formula to a new added row when a new response is submitted on the Google Forms linked to my spreadsheet. First I was using onFormSubmit(e)
function without creating an event object, resulting the error:TypeError: Cannot read property 'range' of undefined at onFormSubmit
. then, as suggested, I created the event object onFormSubmitEvent()
but still the very error is occurring.As you can see in photo, the first column and the first 5 rows are not used for the Form responses. the 4th row shows form questions, 5th row just column numbers, from 6th row the responses are collected(may this also be one of the reasons?). Please help to solve the problem.
function onFormSubmitEvent() {
var dataRange = SpreadsheetApp.getActiveSheet().getDataRange();
var data = dataRange.getValues();
var headers = data[0];
for (var row=1; row < data.length; row++) {
var e = {};
e.values = data[row].filter(Boolean);
e.range = dataRange.offset(row,0,1,data[0].length);
e.namedValues = {};
for (var col=0; col<headers.length; col++) {
e.namedValues[headers[col]] = [data[row][col]];
}
onFormSubmit(e);
}
}
function onFormSubmit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = e.range;
var row1 = range.getRow();
var sheet = range.getSheet();
sheet.getRange(row,17).setFormula("=if($O"+row+"=\"UZS\",index(googlefinance(\"currency:UZSUSD\",\"price\",$B"+row+"),2,2)*$P"+row+",if($O"+row+"=\"EUR\",index(googlefinance(\"currency:EURUSD\",\"price\",$B"+row+"),2,2)*$P"+row+",$P"+row+"))");