简体   繁体   English

谷歌表单中谷歌脚本的每日时间限制

[英]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).我准备了一个谷歌表单,但我想设置一个时间限制,让它每天从早上 8 点(它开始接受回复)到下午 5 点(它停止接受回复)上线。 I managed to find something close to what I wanted here https://www.labnol.org/internet/schedule-google-forms/20707/ .我设法在这里找到了接近我想要的东西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).我不是一个不了解 JS 的程序员,但对 C++(大学必修单元)有一点了解。 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() .为了启动oc() ,请首先运行initTrigger() By this, oc() is launched at AM 0:00 every day.这样, oc()每天早上 0:00 启动。 Please at first run this only one time.首先请只运行一次。
    • After initTrigger() was run, you can confirm this trigger on your script editor.运行initTrigger()后,您可以在脚本编辑器上确认此触发器。
  2. When oc() is launched at AM 0:00, existing triggers for launching openForm() and closeForm() are removed.oc()在上午 0:00 启动时,用于启动openForm()closeForm()现有触发器将被删除。
  3. And then, the triggers for launching openForm() and closeForm() are installed as new trigger.然后,用于启动openForm()closeForm()的触发器被安装为新的触发器。 At this time, the trigger days for openForm() and closeForm() are today's AM 8:00 and PM 5:00, respectively.此时, openForm()closeForm()的触发日期分别是今天的 AM 8:00 和 PM 5:00。

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.我需要为一周中的每一天编写不同的时间表,我想这可以用一个脚本完成,但我只能用 7 个不同的脚本和 7 个触发器来完成。 Any clue about this?关于这个的任何线索?

appreciated!赞赏!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM