3

i want to Open google form every day at 00:01 and closed at 12:00

this is my Code

   function createDailyTrigger() {
  ScriptApp.newTrigger("openCloseFormTrigger")
  .timeBased()
  .everyDays(1)
  .atHour(18)
  .create();
}

function openCloseFormTrigger() {
  const now = new Date();
  const FORM_OPEN_DATE = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 0, 1);
  const FORM_CLOSE_DATE = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 12);

  ScriptApp.newTrigger("openForm")
  .timeBased()
  .at(FORM_OPEN_DATE)
  .create();  
  ScriptApp.newTrigger("closeForm")
  .timeBased()
  .at(FORM_CLOSE_DATE)
  .create();
}


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

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

enter image description here

Here My Execution Tab

enter image description here enter image description here

if i save and run now, variable Vday is work fine but at 00:01 i must save and run again. if i don't save and run at 00:01 , i cannot open google form ( according to the script I made, it can only be opened from 00:01 to 12:00 )
can this script run automatically ? without save and run at 00:01 ?

3 Answers3

1

You have two options, depending on the precision of the time in which you want the form to get opened and closed.

Running exactly at 00:01 and 12:00:

If you need this to happen at the exact minute (00:01 and 12:00), create a daily trigger (fire createDailyTrigger in the sample below, or alternatively follow these steps) that fires a function every day (openCloseFormTrigger) which every day will create two other triggers for the next day, at 00:01 and 12:00, as desired:

function createDailyTrigger() {
  ScriptApp.newTrigger("openCloseFormTrigger")
  .timeBased()
  .everyDays(1)
  .atHour(18)
  .create();
}

function openCloseFormTrigger() {
  const now = new Date();
  const FORM_OPEN_DATE = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 0, 1);
  const FORM_CLOSE_DATE = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1, 12);
  ScriptApp.newTrigger("openForm")
  .timeBased()
  .at(FORM_OPEN_DATE)
  .create();  
  ScriptApp.newTrigger("closeForm")
  .timeBased()
  .at(FORM_CLOSE_DATE)
  .create();
}

Not so precise:

If you can cope with +-15 minutes imprecision, you could create two periodic triggers to open and close the form every day:

function createPeriodicTriggers() {
  ScriptApp.newTrigger("openForm")
  .timeBased()
  .atHour(0)
  .nearMinute(1)
  .everyDays(1)
  .create();
  ScriptApp.newTrigger("closeForm")
  .timeBased()
  .atHour(12)
  .nearMinute(0)
  .everyDays(1)
  .create();
}

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27
  • Hi Lamblichus, sorry for late replay. if i want use your function. where i put in my code ? i want to try function createDailyTrigger and openCloseFormTrigger – Agus Kalyana Jul 24 '21 at 11:02
  • @AgusKalyana Just copy it to the script editor, to the same project where `openForm` and `closeForm` are located. And then execute `createDailyTrigger` once in order to install the daily trigger. – Iamblichus Jul 26 '21 at 07:03
  • @lamblichus i already change ScriptApp.newTrigger("openCloseFormTrigger") to ScriptApp.newTrigger("my Function") but still fail , i must save and run again to open my google form if u want to see my simple test google form in here https://docs.google.com/forms/d/e/1FAIpQLSf2T8vlOgKe7gg1jX0AaxWaDj5CO0l4d-DFrT67LSJ052i0tg/closedform but today i will try to copy paste your script , hope your script works. – Agus Kalyana Jul 28 '21 at 02:19
  • @AgusKalyana Not sure what you mean by `i already change ScriptApp.newTrigger("openCloseFormTrigger") to ScriptApp.newTrigger("my Function") but still fail`. Why are you doing that? And how is this failing? – Iamblichus Jul 28 '21 at 07:14
  • @lamblichus if u create new google form u will find automatic My Function, its default by google. so i change name your new trigger. btw i already Paste your function but its now work. i still need to save and run script to open and close google form. i will capture my script in new post coment. please review my code – Agus Kalyana Jul 31 '21 at 06:06
  • @AgusKalyana Just, in a new script, delete all the existing code and add the functions `createDailyTrigger`, `openCloseFormTrigger`, `openForm`, `closeForm`. Then run `createDailyTrigger` once. You don't have to do anything else. – Iamblichus Aug 02 '21 at 07:09
  • @Lamblichus you can see and review my code and picture above. i have done what u suggest before but its not work. the script run for tomorrow its work , but the day after tomorrow google form its closed all the day until i run it again. example : 2-Aug-21 i run and 3-Aug-21 its work but in 4-Aug-21 its closed all day. if 4-Aug-21 its work, google form will ask * What is your Name * you can see tomorrow at 4-Aug-21 ( 12:00 AM - 12:00 PM -> GMT+7 / UTC+7 ) https://docs.google.com/forms/d/e/1FAIpQLSf2T8vlOgKe7gg1jX0AaxWaDj5CO0l4d-DFrT67LSJ052i0tg/closedform – Agus Kalyana Aug 03 '21 at 06:11
  • @AgusKalyana Remove the line `deleteTriggers();` from `closeForm()`. If you delete the script triggers, of course it will stop working! Also, remove the line `createDailyTrigger();` from `openCloseFormTrigger`, otherwise you will end up with duplicate triggers. – Iamblichus Aug 03 '21 at 07:43
  • @lamblichus , i think if i run CreateDailyTrigger its not work, cause at 08:44 GTM+7 already not open. – Agus Kalyana Aug 04 '21 at 01:45
  • @AgusKalyana Can you check the `Executions` tab and see if the `openForm` function is running at 00:01 AM? – Iamblichus Aug 04 '21 at 07:07
  • @lamblichus , u can see executions tab above. i think openForm not running, if u see 2 aug 2021 openForm is running. if i google for new trigger the sequence after timeBased() is atHour (), next everyDays() and last is Create() , its that problem about sequence ?? here my reference https://developers.google.com/apps-script/reference/script/clock-trigger-builder – Agus Kalyana Aug 05 '21 at 02:50
  • @AgusKalyana It looks like you executed `createDailyTrigger` many times, so you have many daily triggers (maybe you are reaching the trigger quantity limit because of it? Just an hypothesis). Please remove all of these duplicate triggers (check the `Triggers` tab) and then execute `createDailyTrigger`, **only once**. About `its that problem about sequence`, no, this sequence doesn't matter (you can check in tab `Triggers` that the right kind of trigger is created). Also, I see `openForm` running in your screenshots, what makes you think it's not running? – Iamblichus Aug 05 '21 at 07:34
  • @lamblichus ,i already delete all remove trigger. openForm running because i run method openCloseFormTrigger() not createDailyTrigger() i hope with remove all triggers its work. i see u in day after tomorrow. – Agus Kalyana Aug 06 '21 at 01:29
  • @Lamblichus i think we must remove trigger OpenForm and CloseFrom because the trigger run duplicate. any idea for my problem ? – Agus Kalyana Aug 10 '21 at 01:38
0

Yes, it's possible.

One way to achieve this

  • Create a time-driven trigger to run every day to run between 10:00 pm and 11:00 pm to create a time driven trigger to run at 12:00 am for the next day.
  • Create a time-driven trigger to run every day to run between 10:00 am and 11:00 am to create a time driven trigger to run at 12:00 pm.

The above is necessary because time-driven triggers set to run every day at certain hour, could be executed at any minute of that hour, i.e. if the trigger is set to run at 8 am it might run between 8:00 am and 9:00 am.

For more details see this answer to Is it possible to run a function everyday with time-driven trigger without having an active spreadsheet?.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
0

If you want your form to open on the exact second then you will need to trigger a helper function early. The helper function then sleeps until the time when you want your form to open.

When you set a function to run at a certain time it runs at plus or minus a minute from the minute that you set it to run at. e.g. Set the function to trigger at 12:15 and it will run anywhere from 12:14 to 12:16. I have observed this through experimentation.

If you want your form to open at exactly 12:15 then trigger a helper function at 12:13 (to be safe). In the helper function, calculate how much time remains until 12:15 and sleep for that long, and then open your form.

Example typescript code:

function createTimeTrigger() {
    let triggerTime: Date = new Date(2023, 4, 28, 12, 13)
    ScriptApp.newTrigger("myOpenFormFunction")
        .timeBased()
        .at(triggerTime)
        .create();
}

function myOpenFormFunction() {
    let openFormTime: Date = new Date(2023, 4, 28, 12, 15)
    let now: Date = new Date()
    let difference: number = openFormTime.valueOf() - now.valueOf()
    // Difference will be negative if the trigger is created for a time in the past. 
    if (difference > 0) {
        Utilities.sleep(difference)
    }
    let form: GoogleAppsScript.Forms.Form = FormApp.getActiveForm()
    form.setAcceptingResponses(true)
}


In this example the open form time is hard-coded in the helper function. You could 'pass' the open form time to the function using this technique.

One more thing to note is that triggers once created hang around (even after they have triggered) until they are deleted. Once the number of triggers reaches 20 you won't be able to create anymore. So you should tidy up your triggers periodically.

Joman68
  • 2,248
  • 3
  • 34
  • 36