javascriptgoogle-sheetsgoogle-apps-scriptgoogle-formsurlfetch

Failed to submit form responses from a spreadsheet


I am trying to submit a lot of responses using data from a google spreadsheet. I have used the following code:

function FillForm() {
  var wrkSht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  for(var i = 2; i <= wrkSht.getLastRow(); i++)
  {
    try{
      UrlFetchApp.fetch("https://docs.google.com/forms/u/0/d/e/SomeID/formResponse", {"method" : "post",
                                                                                                                                    "payload" : {
                                                                                                                                      "entry.1320223480" : wrkSht.getRange("A" + i).getValue(),
                                                                                                                                      "entry.1469028961_sentinel" : wrkSht.getRange("B" + i).getValue(),
                                                                                                                                    "entry.379679049" : wrkSht.getRange("C" + i).getValue()
                                                                                                                                   }});
    }
    catch(ex)
    {
      MailApp.sendEmail("MyEmail@gmail.com", "error at spreedsheet", "name: " + wrkSht.getRange("A" + i).getValue() + " had the error: " + ex)
      throw ex;
    }
  }
}

but everytime I try to run it I get the following error:

Exception: Request failed for https://docs.google.com returned code 400. Truncated server response: 

<!DOCTYPE html><html lang="en" class="m2"><head><link rel="shortcut icon" sizes="16x16"

 href="https://ssl.gstatic.com/docs/spreadsheets/forms/favic... (use muteHttpExceptions option to examine full response) (line 16, file "Code")

I suspect that my problem is in the entry number: one question in the form is multi-choice, and another one is one choice, but multiple options to choose from. for those tow questions, I couldn't find the entry number, so I entered the input type: hidden entry number that I think is for developers that want to submit responses via code' but I am not sure.

can you find any problem?

Edit:

the two questions that have options and not text are the problem because the values I am trying to get in them are strings? this is accurate string that represents one of the options, but maybe there is other ways to enter a value to an option quations?


Solution

  • We can try using createResponse() to create and submit form responses.

    1. Creates a new response to the form using createResponse().
    2. To answer a question item, create an ItemResponse from the item.
    3. Then attach it to this form response by calling FormResponse.withItemResponse(response).
    4. To save the assembled response, call FormResponse.submit().

    https://developers.google.com/apps-script/reference/forms/form.html#createresponse