Trigger an alert at a specific time on Google Script

杀马特。学长 韩版系。学妹 提交于 2021-02-11 05:54:12

问题


I'm very new to scripting so if anyone knows how to do this it'd be great help!

I have a user that logs in on business days at ~9 am, and has to do something before 9.30 am. To remind and insist that it's done I want to have an alert that says to do it, if a certain cell is not empty. Ideally it would check at 9, 9.10, 9.20 and 9.30, and pop up each time until it's done.

I tried to set up a trigger every 10 minutes, but I read that you can't use triggers with UI.

function startCustomTrigger()
{
  ScriptApp.newTrigger('alerta').timeBased().everyMinutes(10).create();
}

So I thought of making it an onEdit script when new date().getMinutes() is 0/10/20/30, and it worked, but then it keeps popping up when you continue working on the sheet for the whole minute. Adding new date().getSeconds() doesn't work because it's too short of a time window and easy to miss.

This is my current code:

function alerta() {
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var s = ss.getActiveSheet();
  var motosS = ss.getSheetByName("Motos - mañana");
  var motosRange = motosS.getRange(3, 1)
  var ui = SpreadsheetApp.getUi();
  var date = new Date();  
  var day = date.getDay();
  var hrs = date.getHours();
  var min = date.getMinutes()
  var sec = date.getSeconds()

  Logger.log(date)
  Logger.log(day)
  Logger.log(hrs)
  Logger.log(min)
  Logger.log(sec)

  if (motosRange == "" && (day >= 1) && (day <= 5) && (hrs = 9)){
      if (min == 0 || min == 14 || min == 20 || min == 30){
      var response = ui.alert('IMPRIMIR MOTOS PARA LA MAÑANA', 
                            "Hay que anotar el sector de las ventas de ayer e imprimir las motos para la mañana.", 
                            ui.ButtonSet.OK_CANCEL);
                         }
  }
  }

回答1:


Use Script properties to store the alert status between function calls

Explanation

  • Script properties allow you to cache variables which are accessible by the subsequent script run
  • Create a property that is set to true if the alert has been shown already
  • Set the property back to false once one minute (or any other timespan you desire) has passed
  • The latter can be done with a time-driven trigger, or - easier - with Utilities.sleep().
  • The idea is that you check within the if statement the status of your boolean and do not show the alert again while the variable is true

Sample:

function alerta() {
  //initial setup
  if(!PropertiesService.getScriptProperties().getProperty("alreadyNotified")){
    PropertiesService.getScriptProperties().setProperty("alreadyNotified", false)
  }
  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var s = ss.getActiveSheet();
  var motosS = ss.getSheetByName("Motos - mañana");
  var motosRange = motosS.getRange(3, 1).getValue()
  var ui = SpreadsheetApp.getUi();
  var date = new Date();  
  var day = date.getDay();
  var hrs = date.getHours();
  var min = date.getMinutes()
  var sec = date.getSeconds()

  Logger.log(date)
  Logger.log(day)
  Logger.log(hrs)
  Logger.log(min)
  Logger.log(sec)
  var alertStatus = PropertiesService.getScriptProperties().getProperty("alreadyNotified");
  //put "false" in quotes because Scriptproperties store the value as a string
  if (alertStatus == "false" && motosRange == "" && (day >= 1) && (day <= 5) && (hrs = 9)){
    if (min == 0 || min == 14 || min == 20 || min == 30){
      PropertiesService.getScriptProperties().setProperty("alreadyNotified", true);
      var response = ui.alert('IMPRIMIR MOTOS PARA LA MAÑANA', 
                              "Hay que anotar el sector de las ventas de ayer e imprimir las motos para la mañana.", 
                              ui.ButtonSet.OK_CANCEL);
      Utilities.sleep(60*1000);
      PropertiesService.getScriptProperties().setProperty("alreadyNotified", false);
    }
  }
}


来源:https://stackoverflow.com/questions/62036894/trigger-an-alert-at-a-specific-time-on-google-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!