简体   繁体   中英

Daily time limitations from Google Scripts in Google Forms

I have a google form ready but I wish to set a time limit on it so that it goes live everyday from 8am (it starts accepting responses) to 5pm (it stops accepting responses). I managed to find something close to what I wanted here https://www.labnol.org/internet/schedule-google-forms/20707/ .

I am not a programmer with no knowledge of JS but have a little knowledge on C++ (mandatory university unit). I have tried to tweak the sourced code to my wishes by researching online. here is my script in google scripts.

function oc() {

  ScriptApp.newTrigger('openForm')
     .timeBased()
     .everyDays(1)
     .atHour(8)
     .create();

  ScriptApp.newTrigger('closeForm')
     .timeBased()
     .everyDays(1)
     .atHour(16)
     .create();
}

function openForm() {
  var form = FormApp.getActiveForm();
  form.setAcceptingResponses(true);
}

function closeForm() {  
  var form = FormApp.getActiveForm();
  form.setAcceptingResponses(false);
  deleteTriggers_();
}

How about this sample script?

Flow :

  1. In order to launch oc() , please at first, run initTrigger() . By this, oc() is launched at AM 0:00 every day. Please at first run this only one time.
    • After initTrigger() was run, you can confirm this trigger on your script editor.
  2. When oc() is launched at AM 0:00, existing triggers for launching openForm() and closeForm() are removed.
  3. And then, the triggers for launching openForm() and closeForm() are installed as new trigger. At this time, the trigger days for openForm() and closeForm() are today's AM 8:00 and PM 5:00, respectively.

Scripts :

function initTrigger(){
  ScriptApp.newTrigger('oc').timeBased().atHour(0).everyDays(1).create();
}

function oc() {
  ScriptApp.getProjectTriggers().forEach(function(e){
    if(e.getHandlerFunction() == "openForm" || e.getHandlerFunction() == "closeForm") {
      ScriptApp.deleteTrigger(e)
    }
  });

  var time = new Date()
  time.setHours(8);
  time.setMinutes(0);
  ScriptApp.newTrigger("openForm").timeBased().at(time).create();

  time.setHours(17);
  time.setMinutes(0);
  ScriptApp.newTrigger("closeForm").timeBased().at(time).create();
}

function openForm() {
  var form = FormApp.getActiveForm();
  form.setAcceptingResponses(true);
}

function closeForm() {  
  var form = FormApp.getActiveForm();
  form.setAcceptingResponses(false);
  // deleteTriggers_();
}

it worked! thanks a lot

I would need to program different schedules for each day of the week, I guess it could be done in one script but I could only do it with 7 diferent scripts and 7 triggers. Any clue about this?

appreciated!

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