2

Currently I used My Trigger with Time-driven->Day timer->7am to 8am (GMT+08:00) and the result, my script run at 7:11:45 AM. It's to early.

My question is it's possible to trigger Google Sheets at exact time like 7:55:00 AM everyday?

enter image description here

Marios
  • 26,333
  • 8
  • 32
  • 52
user3546314
  • 124
  • 1
  • 13
  • 1
    Please check my answer in this post. It creates a daily scheduled event for a particular set of values. In your case , just use 7:55. https://stackoverflow.com/q/60773329/11225291 – Marios Sep 27 '20 at 01:40
  • @Marios thank you for your respond, I try but I this error get error at line 5."Syntax error. (line 5, file "script")"....."times.forEach(t_el => scheduledTrigger(t_el[0],t_el[1]));" – user3546314 Sep 27 '20 at 02:53
  • 2
    @user3546314 Upgrade to v8 – TheMaster Sep 27 '20 at 03:31
  • 1
    @TheMaster I agree with the duplicate. I didn't close it as such because he mentioned an **every day** scheduled trigger and the duplicate question is for one time scheduled trigger. Of course my answer there covers also that scenario, but I thought a dedicated answer to a question like this would be a better fit. – Marios Sep 27 '20 at 03:34
  • 1
    @Marios Duplicate question is for everyday. *Run a Gmail Google Apps Script "daily" at 8:00, 12:30, 17:00* Also, No need to explain. You can do what you think is fit. I'm sure We'll run into differences of opinion at times. Feel free to vote to reopen, if you believe it needs to be reopened. – TheMaster Sep 27 '20 at 03:37

1 Answers1

7

Explanation:

You need the following four functions:

  • setTrigger(): This function is responsible for deleting the previous triggers and creating a new trigger for function_Triggered. The only thing you need to do is to setup a daily trigger for that function before 7am, for example 6-7am. The following functions are just helpers, you don't execute them (manually).

  • scheduledTrigger(hours,minutes): this function accepts the time (hours and minutes) and it is responsible for creating the scheduled trigger for function_Triggered.

  • deleteTriggers(): This function deletes all the current triggers (in this case only one) for function_Triggered.

  • function_Triggered(): this function contains the code that you want to execute everyday at 7:55am.


Solution:

function setTrigger() {

deleteTriggers();  
scheduledTrigger(7,55);
}

function scheduledTrigger(hours,minutes){
  
var today_D = new Date();
var year = today_D.getFullYear();
var month = today_D.getMonth();
var day = today_D.getDate();
  
pars = [year,month,day,hours,minutes];
  
var scheduled_D = new Date(...pars);
var hours_remain=Math.abs(scheduled_D - today_D) / 36e5;
ScriptApp.newTrigger("function_Triggered")
.timeBased()
.after(hours_remain * 60 *60 * 1000)
.create()
}

function deleteTriggers() {
  
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
  if (   triggers[i].getHandlerFunction() == "function_Triggered") {
    ScriptApp.deleteTrigger(triggers[i]);
  }
}
}

function function_Triggered() {
 // your function code here
}

Please enable v8 runtime otherwise the solution won't work.

Related:

Run a Gmail Google Apps Script daily at 8:00, 12:30, 17:00

Marios
  • 26,333
  • 8
  • 32
  • 52
  • I notice some issue with this trigger, it's only work if I re-saved the trigger again. I try many times with setTrigger and it's work just to set date and time trigger only, but to run function_triggered it's not work. Hope you can check this sheet https://docs.google.com/spreadsheets/d/1tNtGVQaLMetu4-NFq6HYf10cqG-J1iYPoiqRG7xXfw8/edit?usp=sharing – user3546314 Sep 30 '20 at 08:57
  • 1
    Unfortunately, `work` and `does not work` will never provide me or you the right details of the issue. Check why the trigger failed. Go to the project triggers' dashboard and see why the trigger failed. Also the file you shared is in view mode and **I can't see** your code. But even if I could see it, the instructions in my answer have to do with enabling a trigger for `setTrigger()` only. Triggers are attached to the emails and therefore I can't see why the trigger failed in your case. Do your research before you unaccept an answer that helped you in the first place. Good luck :) @user3546314 – Marios Sep 30 '20 at 09:19
  • 1
    The goal of this solution is to see in your dashboard two triggers. One will be the trigger for the function: `function_Triggered` which will be set at the chosen time. The other one will be a manually created trigger by you for the `setTrigger()` that is set for everyday before the time you want to set the other trigger. So far so clear? If you follow the instructions I mentioned both in my answer and here and you go to your dashboard, do you see these two triggers? If yes, open the settings and send me two screenshot with the parameters that they show. @user3546314 – Marios Sep 30 '20 at 09:32
  • Yes I really understand your script will enabling first function setTrigger() then when the time come, second function "function_Triggered() should be run. But in my case, it's not run second function, even I wait for one and two hour.Then what I do is, change time and click save in Trigger page, and it's work.I already put screenshot inside Google Sheet and I open for edit https://docs.google.com/spreadsheets/d/1tNtGVQaLMetu4-NFq6HYf10cqG-J1iYPoiqRG7xXfw8/edit?usp=sharing – user3546314 Sep 30 '20 at 11:42
  • ..I also find someone get almost same problem here, need resaved https://stackoverflow.com/questions/59324950/installable-trigger-created-using-scriptapp-newtrigger-has-to-be-re-saved-befo – user3546314 Sep 30 '20 at 11:43
  • @user3546314 `1. I click play SetTringger and it's make new trigger for 19:30, but second function still not run` **ANSWER**: The second function will run at 19:30, **not** at the time you execute `SetTrigger`. I told you to read the instructions. There are two posts regarding exactly the same thing. Please read them carefully and redo the task. – Marios Sep 30 '20 at 11:46
  • Maybe because my language, I read your post and try for 2 day, that is why I reply back your comment after 2 day because I try and error many time.It's okay I will try find the solution, thank you for your script – user3546314 Sep 30 '20 at 12:08
  • @user3546314 yes but what is the **error** you are getting? you can ask this question 1 million times in stackoverflow and every single time it will be closed if you don't specify the exact error of the issue. Glad I helped you, I hope you will find what you are looking for :) – Marios Sep 30 '20 at 12:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222301/discussion-between-marios-and-user3546314). – Marios Sep 30 '20 at 12:13