Opening and Closing Google Forms on Time using Google Apps Script

Gists

Opening and Closing Google Forms on Time using Google Apps Script

This is a sample script for opening and closing Google Forms on time using Google Apps Script.

In order to test this sample script, please do the following flow.

Usage

1. Create a new Google Form.

Please create a new Google Form and set your sample questions. And, please open the script editor of Google Form.

2. Prepare sample script.

Please copy and paste the following script to the script editor of Google Form. And, please set the values of start and end times you want.

// Please set start and end time you want.
// In this case, Google Form is opened and closed at the start time and the end time, respectively.
// The script is run every day.
const obj = { start: "09:00", end: "17:00" };

const deleteTriggers_ = (e) =>
  ScriptApp.getProjectTriggers().forEach((t) => {
    if (e.includes(t.getHandlerFunction())) ScriptApp.deleteTrigger(t);
  });
const start = (_) => FormApp.getActiveForm().setAcceptingResponses(true);
const end = (_) =>
  FormApp.getActiveForm()
    .setAcceptingResponses(false)
    .setCustomClosedFormMessage("Closed.");

function installTimeDrivenTrigger() {
  deleteTriggers_(["start", "end"]);
  const time1 = new Date();
  time1.setHours(...obj.start.split(":").map((e) => Number(e)), 0);
  const time2 = new Date();
  time2.setHours(...obj.end.split(":").map((e) => Number(e)), 0);
  ScriptApp.newTrigger("start").timeBased().at(time1).create();
  ScriptApp.newTrigger("end").timeBased().at(time2).create();
}

// Please run this script. By this, installTimeDrivenTrigger() is run 00:00 - 01:00 every day.
function init() {
  deleteTriggers_(["installTimeDrivenTrigger"]);
  ScriptApp.newTrigger("installTimeDrivenTrigger")
    .timeBased()
    .everyDays(1)
    .atHour(0)
    .create();
}

Note

References

 Share!