Google Form – Using Apps Script to Populate Google Sheet

In this post, I take a Google Form and populate a Google Sheet by using Google Apps Script. The Apps Script adds versatility in populating a Google Sheet.

How to Video:

Code in Video:

function onFormSubmit(event) {

  record_array = []

  var form = FormApp.openById(''); // Form ID
  var formResponses = form.getResponses();
  var formCount = formResponses.length;

  var formResponse = formResponses[formCount - 1];
  var itemResponses = formResponse.getItemResponses();

  for (var j = 0; j < itemResponses.length; j++) {
  var itemResponse = itemResponses[j];
    var title = itemResponse.getItem().getTitle();
    var answer = itemResponse.getResponse();

    Logger.log(title);
    Logger.log(answer);

    record_array.push(answer);
  }
   
  AddRecord(record_array[0], record_array[1], record_array[2]);

}

function AddRecord(first_name, last_name, color) {
  var url = '';   //URL OF GOOGLE SHEET;
  var ss= SpreadsheetApp.openByUrl(url);
  var dataSheet = ss.getSheetByName("Sheet1");
  dataSheet.appendRow([first_name, last_name, color, new Date()]);
}

Leave a Reply