Script to populate GoogleForms response in multiple locations

扶醉桌前 提交于 2020-01-06 17:58:21

问题


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:


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.



来源:https://stackoverflow.com/questions/43429303/script-to-populate-googleforms-response-in-multiple-locations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!