简体   繁体   中英

Creating a google script to automate google form submission, using values from a google sheet

I am trying to create a google script that helps me to automate the creation of google form responses, using values from a google sheet.

Here is the script that I am using.

function auto_data() {

  var formURL="https://docs.google.com/forms/d/e/1FAIpQLSfmepj661gmkUgleFCLPrqeB0z9mPyI8DLZ4kTBI0Y3eByOCQ/viewform";

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

  var numb = wrkSht.getRange("G11").getValue();
  var time = wrkSht.getRange("H11").getValue();
  var eaten = wrkSht.getRange("I11").getValue();
  var randnumb = wrkSht.getRange("J11").getValue();

  var datamap={
    "entry.1108902288" :numb,
    "entry.1493000579" :randnumb,
    "entry.1582850009" :time,
    "entry.908606572" :eaten
    };

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

  UrlFetchApp.fetch(formURL, options);

}

Unfortunately, I got this error:

Exception: Request failed for https://docs.google.com returned code 405. Truncated server response: <!DOCTYPE html><html lang="en"><head><meta name="description" content="Web word processing, presentations and spreadsheets"><meta name="viewport" c... (use muteHttpExceptions option to examine full response). (line 25, file "Code")

Can anyone help me? Thanks!

  • 405 means that the request is not allowed

  • You cannot just make a post request to a Google Form.

  • What you can do is to create programmatically a prefilled form Url - all you have to do then is open this link in your browser and click on Submit .

  • To do so, pass the answers to the respective entries as e.parameters .

Sample:

function auto_data() {

  var formURL="https://docs.google.com/forms/d/e/1FAIpQLSfmepj661gmkUgleFCLPrqeB0z9mPyI8DLZ4kTBI0Y3eByOCQ/viewform";

  var wrkBk = SpreadsheetApp.getActiveSpreadsheet();
  var wrkSht = wrkBk.getSheetByName("Data");
  var numb = wrkSht.getRange("G11").getValue();
  var time = wrkSht.getRange("H11").getValue();
  var eaten = wrkSht.getRange("I11").getValue();
  var randnumb = wrkSht.getRange("J11").getValue();  

  var datamap=[
    "entry.1108902288=" + numb,
    "entry.1493000579=" + randnumb,
    "entry.1582850009=" + time,
    "entry.908606572=" + eaten
    ];
 var prefillUrl = formURL+"?"+ datamap[0] + "&" + datamap[1] + "&" + datamap[2] + "&" + datamap[3];
  Logger.log(prefillUrl);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM