Reputation: 21
So i try to make an script on https://script.google.com/ to automate data entry from google spreadsheet to google forms but i always receiving this code error
"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 25, file "Code")"
Can somebody help me?
Here My code
function auto_data_entry() {
var formURL="https://docs.google.com/forms/u/0/d/e/1FAIpQLSduXRTFWkeq1ZLRaiGzhmT58oUZSp20FHsT73_qMj8ZpobuqA/formResponse";
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 https://docs.google.com/spreadsheets/d/1-sLoKy6npftapr3QxoRIoYozBN6izei0E9uqF4yLg-8/edit?usp=sharing
And here was the form https://docs.google.com/forms/d/e/1FAIpQLSduXRTFWkeq1ZLRaiGzhmT58oUZSp20FHsT73_qMj8ZpobuqA/viewform
Thanks!
Upvotes: 2
Views: 6551
Reputation: 2930
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.
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.
Also, if you wish to automate form submissions from your spreadsheet data you can use this code:
function auto_submission() {
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 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 );
}
//submit the form
FormResponse.submit();
}
I hope this has helped you, let me know if you found any issues or if you do not understand anything.
Upvotes: 2