I am trying to use the same spreadsheet for multiple automated and semi-automated functions, one of which is allowing users to submit data GoogleForms.
My goal is to extract form responses, such that the next blank row on a Master Sheet (titled "Apps") is populated with the newly submitted data (in which the column names are identical with that of the "Form Responses" sheet).
I've been playing around with the below, with OnEdit(e)
set to "on Form Submit" but that's not quite getting me what I need.
function onEdit(e){
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = e.range;
var sheet = range.getSheet()
var newSheet = ss.getSheetByName("Apps")
var sheetName = sheet.getName()
if(sheetName != "Form"){
return //exit function
}
var columnOfCellEdited = e.range.getColumn();
if (columnOfCellEdited === 1)
var range2 = range.getRow();
var destrange = newSheet.getLastRow()
sheet = newSheet
// adds the formulas
var cell = sheet.getRange("C" + destrange);
cell.setFormula('=Form!$C' + destrange);
var cell = sheet.getRange("D" + destrange);
cell.setFormula('=Form!$D' + destrange);
var cell = sheet.getRange("E" + destrange);
cell.setFormula('=Form!$E' + destrange);
var cell = sheet.getRange("F" + destrange);
cell.setFormula('=Form!$F' + destrange);
var cell = sheet.getRange("G" + destrange);
cell.setFormula('=Form!$G' + destrange);
var cell = sheet.getRange("H" + destrange);
cell.setFormula('=Form!$H' + destrange);
}
Lastly, I found this SO answer which looks like it would be helpful if I knew where to put things.
EDIT: Below attempting to implement based on MrRebot suggestions, not producing any visible results. Should form still be linked to sheet?
function onEdit(e) {
var form = FormApp.openById('1YLnK8kw2dRMUUvL1l3s4qjz9fSRIe7kj92MlPV5WV5M');
var formResponses = form.getResponses();
for (var i = 0; i < formResponses.length; i++) {
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
Logger.log('Response #%s to the question "%s" was "%s"',
(i + 1).toString(),
itemResponse.getItem().getTitle(),
itemResponse.getResponse());
}
}
var ss = SpreadsheetApp.getActiveSheet();
ss.insertRowAfter(ss.getLastRow());
ss.getRange(ss.getLastRow(),1,1,ss.getLastColumn()).copyTo(ss.getRange(ss.getLastRow()+1,1));
}
On form submit, try using FormResponse to get the list of responses. See sample code:
Then try to follow this code to insert a new row:
Hope this helps.