Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active June 12, 2023 02:35
Show Gist options
  • Save tanaikech/84bd91f2927d8c8463b494f712eb1800 to your computer and use it in GitHub Desktop.
Save tanaikech/84bd91f2927d8c8463b494f712eb1800 to your computer and use it in GitHub Desktop.
Opening and Closing Google Forms on Time using Google Apps Script

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();
}
  • When you use this script, please run init() function with the script editor.

  • By this, installTimeDrivenTrigger() is installed as a time-driven trigger. This trigger automatically runs the function installTimeDrivenTrigger() every day. When this function is run, start() and end() functions are installed as the time-driven trigger using your inputted times.

  • This cycle is run every day by the installed installTimeDrivenTrigger(). When this function is run, start() and end() functions are installed as the time-driven trigger.

  • In this sample, when the user accesses the Google Form from end time to start time, "Closed." is shown. If you want to change this message, please modify the above script.

Note

  • I believe that this sample script for using the time-driven trigger will be useful for other situations.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment