使用应用脚本在特定时间自动禁用谷歌表单

huangapple go评论65阅读模式
英文:

Automatically disable a google form at certain point in time using apps script

问题

我有一个谷歌表单,每周都会开放响应, 请帮助我使用应用脚本,如何禁用该表单,使其在每周五下午6点至周一上午9点之间无法访问,

然后从周一上午9点开始,整个周都可以访问,直到周五下午6点。

并且这应该每周都会重复。

英文:

I have a google form which is open for responses through the week, please help me using apps script how can I disable the form that it is not accessible on every Friday 6 PM upto Monday 9 AM

and again be accessible back from Monday 9 AM through the whole week until Friday 6 PM.

and this should repeat every week.

答案1

得分: 1

以下是翻译好的部分:

  • 您想要在周一的 09:00 打开您的 Google 表单。
  • 您想要在周五的 18:00 关闭您的 Google 表单。
  • 您的 Google 表单在周一到周五开放。
  • 您想要运行这个操作,除了周六和周日。

在您的情况下,我记得这个帖子可能会有用。参考(作者:我) 但是,在那种情况下,脚本会每天运行。我认为为您预期的情况修改这个脚本可能有点复杂。因此,我想介绍修改后的脚本作为答案。当将该脚本修改为您的情况时,如何使用以下示例脚本?

示例脚本:

请将以下脚本复制并粘贴到您的 Google 表单的脚本编辑器中,并保存脚本。

当您在脚本编辑器中运行 init() 时,将安装一个时间驱动触发器。此触发器会每天自动运行 installTimeDrivenTrigger() 函数。但在这种情况下,脚本将使用 if (["Saturday", "Sunday"].includes(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "EEEE"))) return; 来排除周六和周日之外的日期。当运行此函数时,start()end() 函数将被安装为时间驱动触发器。

start() 在 09:00 启用 Google 表单。然后,end() 在 18:00 禁用 Google 表单。在接下来的一天之后,将自动运行 installTimeDrivenTrigger() 函数。

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

function installTimeDrivenTrigger() {
  const d = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "EEEE");
  if (["Saturday", "Sunday"].includes(d)) return;
  deleteTriggers_(["start", "end"]);
  if (d == "Monday") {
    const time1 = new Date();
    time1.setHours(9, 0, 0);
    ScriptApp.newTrigger("start").timeBased().at(time1).create();
  } else if (d == "Friday") {
    const time2 = new Date();
    time2.setHours(18, 0, 0);
    ScriptApp.newTrigger("end").timeBased().at(time2).create();
  }
}

// 请运行此脚本。这样,installTimeDrivenTrigger() 将在每天的 00:00 - 01:00 运行。
function init() {
  deleteTriggers_(["installTimeDrivenTrigger"]);
  ScriptApp.newTrigger("installTimeDrivenTrigger").timeBased().everyDays(1).atHour(0).create();
  // end(); // 如果您想要按默认情况关闭,请使用此选项。
}
  • 在此示例中,当用户从周五的 18:00 到周一的 09:00 访问 Google 表单时,将显示 "已关闭。"。如果您想更改此消息,请修改上述脚本。

注意:

  • 在这种修改中,如果更改函数名称,当脚本中的函数名称未更改时,脚本可能无法使用。请注意这一点。

  • 我认为这个脚本可以实现您的目标。但是,在这个脚本中,installTimeDrivenTrigger() 通过 ScriptApp.newTrigger("installTimeDrivenTrigger").timeBased().everyDays(1).atHour(0).create(); 每天在 00:00 - 01:00 运行。如果您只想在周一和周五运行,请按照以下方式进行修改。这样,installTimeDrivenTrigger() 将在周一和周五运行。

    • ScriptApp.newTrigger("installTimeDrivenTrigger").timeBased().everyDays(1).atHour(0).create();
      
    • ScriptApp.newTrigger("installTimeDrivenTrigger").timeBased().everyWeeks(1).onWeekDay(ScriptApp.WeekDay.MONDAY).atHour(0).create();
      ScriptApp.newTrigger("installTimeDrivenTrigger").timeBased().everyWeeks(1).onWeekDay(ScriptApp.WeekDay.FRIDAY).atHour(0).create();
      

参考:

英文:

I believe your goal is as follows.

  • You want to open your Google Form at 09:00 on Monday.
  • You want to close your Google Form at 18:00 on Friday.
  • Your Google Form is opening Monday to Friday.
  • You want to run this day except for Saturday and Sunday.

In your situation, I remembered that this thread might be useful. Ref (Author: me) But, in that case, the script is run every day. I thought that modifying this script for your expected situation might be a bit complicated. So, I would like to introduce the modified script as an answer. When that script is modified for your situation, how about the following sample script?

Sample script:

Please copy and paste the following script to the script editor of your Google Form, and save the script.

When you run init() with the script editor, a time-driven trigger is installed. This trigger automatically runs the function installTimeDrivenTrigger() every day. But, in this case, the script is run on the days except for Saturday and Sunday by if (["Saturday", "Sunday"].includes(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "EEEE"))) return;. And, when this function is run, start() and end() functions are installed as the time-driven trigger.

start() enable Google Form at 09:00. And, end() disable Google Form at 18:00. And, after the next day, the function installTimeDrivenTrigger() is automatically run.

const deleteTriggers_ = e => ScriptApp.getScriptTriggers().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() {
  const d = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "EEEE");
  if (["Saturday", "Sunday"].includes(d)) return;
  deleteTriggers_(["start", "end"]);
  if (d == "Monday") {
    const time1 = new Date();
    time1.setHours(9, 0, 0);
    ScriptApp.newTrigger("start").timeBased().at(time1).create();
  } else if (d == "Friday") {
    const time2 = new Date();
    time2.setHours(18, 0, 0);
    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();
  // end(); // If you want to close as the default situation, please use this.
}
  • In this sample, when the user accesses the Google Form from 18:00 on Friday to 09:00 on Monday, "Closed." is shown. If you want to change this message, please modify the above script.

Note:

  • In this modification, when the function name is changed, the script might not be able to be used when the function names in the script are not changed. Please be careful about this.

  • I think that this script can be worked for your goal. But, in this script, installTimeDrivenTrigger() is run 00:00 - 01:00 every day by ScriptApp.newTrigger("installTimeDrivenTrigger").timeBased().everyDays(1).atHour(0).create();. If you want to run only Monday and Friday, please modify it as follows. By this, installTimeDrivenTrigger() is run on Monday and Friday.

    • From

      ScriptApp.newTrigger("installTimeDrivenTrigger").timeBased().everyDays(1).atHour(0).create();
      
    • To

      ScriptApp.newTrigger("installTimeDrivenTrigger").timeBased().everyWeeks(1).onWeekDay(ScriptApp.WeekDay.MONDAY).atHour(0).create();
      ScriptApp.newTrigger("installTimeDrivenTrigger").timeBased().everyWeeks(1).onWeekDay(ScriptApp.WeekDay.FRIDAY).atHour(0).create();
      

References:

huangapple
  • 本文由 发表于 2023年2月23日 19:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定