Script to populate GoogleForms response in multipl

2019-09-15 14:33发布

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));
}

1条回答
走好不送
2楼-- · 2019-09-15 15:14

On form submit, try using FormResponse to get the list of responses. See sample code:

 // Open a form by ID and log the responses to each question.
 var form = FormApp.openById('1234567890abcdefghijklmnopqrstuvwxyz');
 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());
   }
 }

Then try to follow this code to insert a new row:

var ss = SpreadsheetApp.getActiveSheet();
  ss.insertRowAfter(ss.getLastRow());
  ss.getRange(ss.getLastRow(),1,1,ss.getLastColumn()).copyTo(ss.getRange(ss.getLastRow()+1,1));

Hope this helps.

查看更多
登录 后发表回答