1

I am trying to import data from a Google Spreadsheet into a Google Form as responses. I have about 1000 rows of data that need to be submitted as separate Form responses. All of the columns correspond to different types of questions (such as multiple-choice, text, dropdown menu, date, etc.). I have searched all over the internet and here on Stackoverflow but none of the answers to similar questions seem to fit what I am trying to do. I found several people using form.createResponse(), but I am very new to programming so maybe that is why I couldn't fit them to my needs...

Nevertheless, I would really appreciate it if someone could help me write some Google Apps Script code that is able to cycle through each line of the spreadsheet and submit them as separate responses through the form.

This is what the form which needs to be answered looks like: This is what the form which needs to be answered looks like

These are the corresponding headings and data that need to be added as responses: These are the corresponding headings and data that need to be added as responses

The code below is what I believe to be closest to what I am looking for:

function ApendResponses() {
  var form = FormApp.openByUrl('https://docs.google.com/forms/d/e/1FAIpQLSfVz48wSVwri7cPxWsAHszaF7eqfQi4xdpU2seXiZrgTwaWTA/viewform');
  var sheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1Se_74kAX6zEX71I_kFcKxScnuAslKCPZdE3yHptX-2Q/edit#gid=1047564688");
  var rows = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();

  for (var x = 0; x < values.length; x++) {

    var formResponse = form.createResponse();
    var items = form.getItems();

    var row = values[x];

    var formItem = items[0.0].asTextItem;   
    var response = form.createResponse(column[0]);     
    formResponse.withItemResponse(response);

    var formItem = items[1.0].asDateItem();   
    var response = formItem.createResponse(column[1]);     
    formResponse.withItemResponse(response);

    var formItem = items[2.0].asDateItem();   
    var response = formItem.createResponse(column[2]);     
    formResponse.withItemResponse(response);

    var formItem = items[3.0].asDateItem();   
    var response = formItem.createResponse(column[3]);     
    formResponse.withItemResponse(response);

    var formItem = items[4.0].asMultipleChoiceItem();   
    var response = formItem.createResponse(column[4]);     
    formResponse.withItemResponse(response);

    var formItem = items[5.0].asMultipleChoiceItem();   
    var response = formItem.createResponse(column[5]);     
    formResponse.withItemResponse(response);

    var formItem = items[6.0].asTextItem();   
    var response = formItem.createResponse(column[6]);     
    formResponse.withItemResponse(response);

    var formItem = items[7.0].asTextItem();   
    var response = formItem.createResponse(column[7]);     
    formResponse.withItemResponse(response);

    var formItem = items[8.0].asMultipleChoiceItem();   
    var response = formItem.createResponse(column[8]);     
    formResponse.withItemResponse(response);

    var formItem = items[9.0].asTextItem();   
    var response = formItem.createResponse(column[9]);     
    formResponse.withItemResponse(response);

    var formItem = items[10.0].asMultipleChoiceItem();   
    var response = formItem.createResponse(column[10]);     
    formResponse.withItemResponse(response);

    var formItem = items[11.0].asMultipleChoiceItem();   
    var response = formItem.createResponse(column[11]);     
    formResponse.withItemResponse(response);

    formResponse.submit();
    Utilities.sleep(500);

  }

};

Although, this code has not worked.

Daniel H.
  • 621
  • 4
  • 20
  • You can also check on this one and I hope you can find the resolution that fits you: https://stackoverflow.com/questions/20888097/import-google-spreadsheet-data-into-google-forms-with-app-script – Monique G. Jan 26 '21 at 21:28
  • Sadly, I have looked at both of those previously and neither of them helps me. I do not need to make questions, but answers... – Daniel H. Jan 27 '21 at 07:34
  • @Cooper, thank you for the comment, although the link you suggested is for creating new questions. I have updated the question with a better explanation. Could you possibly take another look? – Daniel H. Jan 27 '21 at 12:35
  • 1
    @Cooper this is the same one as the previous one you linked. I've looked through it but no luck. ....probably a side effect of my weak programming skillsű – Daniel H. Jan 27 '21 at 15:41
  • Oh sorry. It's not a script for creating a form. It's a script for populating possible reponses to a form that already exists. In fact, my example was a form that I created manually not from a script. – Cooper Jan 27 '21 at 15:49
  • @Cooper Yes, this is what I am looking for: "a script for populating possible responses to a form that already exists". – Daniel H. Jan 27 '21 at 18:18
  • Yes I know that's why I gave you that example because that's what it does. And the code is very well done. If you're new to coding you may find some of the code very challenging. – Cooper Jan 27 '21 at 19:12
  • 1
    @Cooper oh okay, I see where we misunderstood each other. It was my mistake: what I am looking for a script that takes the data in the spreadsheet and answers the form, not adding possible responses. The possible responses are already there, I just need the script to answer the same form about 1000 times so that the data appears as if the form were sent to someone and they answered the questions in it. – Daniel H. Jan 27 '21 at 19:57
  • I don't really know for sure but I don't think that's possible. I'm under the impression that once the form launches that programmatic access stops. Considering that access to an html page requires that the user perform an action first before the server responds so how do you get access without the user. – Cooper Jan 27 '21 at 20:04
  • @Cooper I have no idea tbh. But the code I found (the on in the description) has apparently worked for someone. [Here](https://webapps.stackexchange.com/a/54773) is the link to that answer. – Daniel H. Jan 27 '21 at 20:14
  • I think I was wrong, evidently you can programmatic submit responses to forms so that when users return to fill in the form it already has some pre answered questions. Honestly, I don't use form very much I usually use html myself. I was correct in the sense that once a form it activated by a user there is no more programmatic access but evidently you can create responses that can be stored in the response which can prefill answers. Well good luck with that. – Cooper Jan 27 '21 at 21:05

1 Answers1

2

Finally, I have managed to make it work. If anyone is looking for a way to populate an existing Google Form with data from a Google Spreadsheet, use the code below:

function readSpreadsheet() {
  var sheet = SpreadsheetApp.openById("[THE ID OF THE SPREADSHEET]");
  var range = sheet.getDataRange();
  var numRows = range.getNumRows();
  var values = range.getValues();
  var form = FormApp.getActiveForm();
  var items = form.getItems();
  for (var i = 1; i < numRows; i++) {
    var value = values[i];
    var formResponse = form.createResponse();
    var k = 0;
    for (var j = 0; j < items.length; j++) {
      var item;
      switch (items[j].getType()) {
        case FormApp.ItemType.CHECKBOX:
          item = items[j].asCheckboxItem();
          Logger.log("Item number " + j + " is titled: " + item.getTitle() );
          formResponse.withItemResponse(item.createResponse(value[k++]));
          break;  
        case FormApp.ItemType.LIST:
          item = items[j].asListItem();
          Logger.log("Item number " + j + " is titled: " + item.getTitle() );
          formResponse.withItemResponse(item.createResponse(value[k++]));
          break; 
        case FormApp.ItemType.MULTIPLE_CHOICE:
          item = items[j].asMultipleChoiceItem();
          Logger.log("Item number " + j + " is titled: " + item.getTitle() );
          formResponse.withItemResponse(item.createResponse(value[k++]));
          break; 
        case FormApp.ItemType.PARAGRAPH_TEXT:
          item = items[j].asParagraphTextItem();
          Logger.log("Item number " + j + " is titled: " + item.getTitle() );
          formResponse.withItemResponse(item.createResponse(value[k++]));
          break; 
        case FormApp.ItemType.TEXT:
          item = items[j].asTextItem();
          Logger.log("Item number " + j + " is titled: " + item.getTitle() );
          formResponse.withItemResponse(item.createResponse(value[k++]));
          break;
        case FormApp.ItemType.CHECKBOX:
          item = items[j].asCheckboxItem();
          Logger.log("Item number " + j + " is titled: " + item.getTitle() );
          formResponse.withItemResponse(item.createResponse(value[k++]));
          break;                 
        default:
          Logger.log("#" + (i + 1) + ":Do nothing for item " + j + " of type " + items[j].getType());
          continue;
      } 
      if(j==0){
        Logger.log("This item is the FORM NUMBER");        
      };
    }
    formResponse.submit();
  }
}

Credits, and thank you to bachmeb from GitHub for writing this code.

Daniel H.
  • 621
  • 4
  • 20