问题
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 :
- In order to launch
oc()
, please at first, runinitTrigger()
. 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.
- After
- When
oc()
is launched at AM 0:00, existing triggers for launchingopenForm()
andcloseForm()
are removed. - And then, the triggers for launching
openForm()
andcloseForm()
are installed as new trigger. At this time, the trigger days foropenForm()
andcloseForm()
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