Directly Submitting Answers to Google Form using Google Apps Script

Gists

This is a sample script for directly submitting answers to Google Form using Google Apps Script.

The sample Google Form is as follows.

For this Google Form, this sample script submits the values of sample text, option2 and option1, option2, sample option to Google Form.

Sample script

For the multiple answers, it seems that it is required to send the values as the query parameter. I thought that the same key is used.

// This is from https://gist.github.com/tanaikech/70503e0ea6998083fcb05c6d2a857107
String.prototype.addQuery = function (obj) {
  return (
    this +
    Object.keys(obj).reduce(function (p, e, i) {
      return (
        p +
        (i == 0 ? "?" : "&") +
        (Array.isArray(obj[e])
          ? obj[e].reduce(function (str, f, j) {
              return (
                str +
                e +
                "=" +
                encodeURIComponent(f) +
                (j != obj[e].length - 1 ? "&" : "")
              );
            }, "")
          : e + "=" + encodeURIComponent(obj[e]))
      );
    }, "")
  );
};

function sample1() {
  const url = "https://docs.google.com/forms/d/###GoogleFormId###/formResponse";
  const query = {
    "entry.###key3###": ["option1", "option2", "__other_option__"],
  };
  const payload = {
    "entry.###key1###": "sample text",
    "entry.###key2###": "option2",
    "entry.###key3###.other_option_response": "sample option",
  };
  const endpoint = url.addQuery(query);
  const res = UrlFetchApp.fetch(endpoint, { method: "post", payload });
}

When you want to use “other option” at the question 3, it seems that it is required to send __other_option__ for the key and also send the value to the key of entry.###key3###.other_option_response. And also, you can use all answers with the query parameters. The sample script is as follows.

// This is from https://gist.github.com/tanaikech/70503e0ea6998083fcb05c6d2a857107
String.prototype.addQuery = function (obj) {
  return (
    this +
    Object.keys(obj).reduce(function (p, e, i) {
      return (
        p +
        (i == 0 ? "?" : "&") +
        (Array.isArray(obj[e])
          ? obj[e].reduce(function (str, f, j) {
              return (
                str +
                e +
                "=" +
                encodeURIComponent(f) +
                (j != obj[e].length - 1 ? "&" : "")
              );
            }, "")
          : e + "=" + encodeURIComponent(obj[e]))
      );
    }, "")
  );
};

function sample2() {
  const url = "https://docs.google.com/forms/d/###GoogleFormId###/formResponse";
  const query = {
    "entry.###key1###": "sample text",
    "entry.###key2###": "option2",
    "entry.###key3###": ["option1", "option2", "__other_option__"],
    "entry.###key3###.other_option_response": "sample option",
  };
  const endpoint = url.addQuery(query);
  const res = UrlFetchApp.fetch(endpoint, { method: "post" });
}
  • In above both scripts, the values of "9/1/2021 00:00:00", "sample text", "option2", "option1, option2, sample option" are put to the Spreadsheet.

Reference

 Share!