Request Failed for Https: //Docs. Google. Com Returned Code 400. Truncated Server Response:

Request Failed for Https: //Docs. Google. Com Returned Code 400. Truncated Server Response:

So i try to make an script on to automate data entry from google spreadsheet to google forms but i always receiving this code error

"Request failed for  returned code 400. Truncated server response: <!DOCTYPE html><html lang="en" class="m2"><head><link rel="shortcut icon" sizes="16x16" href=" (use muteHttpExceptions option to examine full response) (line 25, file "Code")"

Can somebody help me?

Here My code

function auto_data_entry() {

  var formURL="";

  var wrkBk = SpreadsheetApp.getActiveSpreadsheet();
  var wrkSht= wrkBk.getSheetByName("Sheet1");

  var Transaction1 = wrkSht.getRange("A1").getValue();
  var Transaction2 = wrkSht.getRange("A2").getValue();
  var Transaction3 = wrkSht.getRange("A3").getValue();
  var Transaction4 = wrkSht.getRange("A4").getValue();
  var Transaction5 = wrkSht.getRange("A5").getValue();

  var datamap={"entry.663378019":Transaction1,
   "entry.978525821":Transaction2,
   "entry.1636658367":Transaction3,
   "entry.1490939339":Transaction4,
   "entry.2066528728":Transaction5};

  var options = {
    "method": "post",
    "payload": datamap
  };

  UrlFetchApp.fetch(formURL, options);
}

And here the sheet

And here was the form

Thanks!

2

1 Answer

Your main issue here is that you are trying to use the fetch method for a google form. Fetch works for making HTTP requests to websites so it will be overcomplicated to try to do this to a form as you would need to create a custom response on that side.

Automating data entries

An easier way of achieving what you want is to use the Form Response methods from Apps Script Google Form´s documentation for pre filling the form. This piece of code will automate data entries from google´s spreadsheet:

function auto_data_entry(){

  var url='YOUR FORM URL';
  
  var wrkBk = SpreadsheetApp.getActiveSpreadsheet();
  var wrkSht= wrkBk.getSheetByName("Sheet1")
  
  var Transaction1 = wrkSht.getRange("A1").getValue();
  var Transaction2 = wrkSht.getRange("A2").getValue();
  var Transaction3 = wrkSht.getRange("A3").getValue();
  var Transaction4 = wrkSht.getRange("A4").getValue();
  var Transaction5 = wrkSht.getRange("A5").getValue();
 
  var data = [Transaction1,Transaction2,Transaction3,Transaction4,Transaction5];


  var form = FormApp.openByUrl(url);
  var questions = form.getItems(); //get the different questions you want to pre fill
  var FormResponse = form.createResponse();
  //set a response from your response data array to the corresponding question until you fill the whole form
  for(i=0;i<5;i++){
    var qt = questions[i].asTextItem(); 
    var qr = qt.createResponse(data[i]);
    FormResponse.withItemResponse( qr ); 
  }
  //create a prefilled form
  var preurl = FormResponse.toPrefilledUrl();
  Logger.log(preurl);
  
}

NOTE: your pre filled form url is in the log.

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.