Daily time limitations from Google Scripts in Google Forms

こ雲淡風輕ζ 提交于 2019-12-11 07:34:47

问题


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_();
}

回答1:


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_();
}

If I misunderstand your question, I'm sorry.



来源:https://stackoverflow.com/questions/45526838/daily-time-limitations-from-google-scripts-in-google-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!