如何更正此脚本以在特定日期不运行触发器

时间:2019-05-05 03:25:10

标签: google-apps-script

我当前正在使用此脚本

function shouldRunTrigger() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var fiestas = ss.getSheetByName("x");
  var data = fiestas.getRange(2,1,fiestas.getLastRow()-1,4).getValues();
  var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  var date = new Date();
  var day = days[date.getDay()];
  var hours = date.getHours();  
  var FiestaEmpieza = date
  var FiestaAcaba = date
  for (var row in data){    
     FiestaEmpieza = data[row][2] ;
    }
  for (var row in data){    
     FiestaAcaba = data[row][3] ;
    }
  // Don't run from Friday 6pm until Saturday 10Pm
   if ((day === "Fri" && hours >= 18) || (day === "Sat" && hours <= 21)){
    return false;
  }

  // Dont turn from specific dates from Tab "x" from 6pm until 10pm
   else if (date >= FiestaEmpieza && hours >= 18 && date <= FiestaAcaba && hours <= 21){
    return false; 
  }

   else {
    return true; 
  }

}

脚本的目的:我必须每5分钟运行一些触发器,但是如果一天在星期五晚上6点到星期六晚上10点之间,则触发器将不运行。

我还需要不要在特定日期运行(脚本的第31行) 它从我的电子表格标签“ x”中获取日期,开始日期为C列,结束日期为D列,数据如下所示:

enter image description here

我认为问题在于脚本的第31行

else if (date >= FiestaEmpieza && hours >= 18 && date <= FiestaAcaba && hours <= 21){

无法正确检测日期和小时,我不确定&&是否正确。

请问如何解决此问题的任何帮助?

2 个答案:

答案 0 :(得分:0)

我认为这可能有效。如果我可以访问您的电子表格以便仅复制数据,则可以对其进行测试。

function shouldRunTrigger() {
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var sh=ss.getSheetByName("x");
  var vA=sh.getRange(2,1,sh.getLastRow()-1,4).getValues();
  var days=["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  var today=new Date();
  var day=days[today.getDay()];
  var hours=today.getHours();  
  if((day=="Fri" && hours>=18) || (day=="Sat" && hours<=22)){return false;}
  for(var i=0;i<vA.length;i++){    
    var dt1=new Date(vA[row][2]).valueOf();
    var dt2=new Date(vA[row][3]).valueOf();
    if(today.valueOf()>=dt1 && hours >=18 && today.valueOf() <= dt2 && hours <= 21){
      return false; 
    }
  }
  return true; 
}

答案 1 :(得分:0)

FiestaEmpeizaFiestaAcaba始终是最后一行,因为您循环遍历日期值并赋值,但随后不执行任何条件检查。您需要将循环合并为一个,然后将if语句移入循环。

我建议更改某些变量名称以使其更具描述性,并将触发时间传递给函数,以便您可以更轻松地对其进行测试。

此外,请确保与日期检查保持一致-日期对象包括时间。按照编写方式,您正在比较日期对象,这意味着“ 4月1日下午4点”早于“ 4月1日下午4:01”。因此,您的条件可能会失败,具体取决于您在date中定义的值。您可以通过setting hours来解决此问题。 (请注意时区问题。)

function test_shouldRunTrigger() {
  var triggerTime = new Date("Fri Apr 19 17:00:00 GMT+08:00 2019"); // Change the date to test
  Logger.log(shouldRunTrigger(triggerTime));
}

function shouldRunTrigger(triggerTime) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("x");
  var fiestas = sheet.getRange(2,1,sheet.getLastRow()-1,4).getValues();
  var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  var triggerDay = days[triggerTime.getDay()]; // Day of the week
  var triggerHour = triggerTime.getHours(); // Hour of the trigger
  var triggerDate = new Date(triggerTime.setHours(0, 0, 0, 0)); // Set time to 00:00:00
  Logger.log(fiestas)
  for (var row in fiestas) {
    var fiestaEmpieza = new Date(fiestas[row][2].setHours(0, 0, 0, 0)); // Set time to 00:00:00
    var fiestaAcaba = new Date(fiestas[row][3].setHours(0, 0, 0, 0)); // Set time to 00:00:00

    // Don't run from Friday 6pm until Saturday 10pm
    if ((triggerDay === "Fri" && triggerHour >= 18) || (triggerDay === "Sat" && triggerHour <= 21)) {
      return false;
    } else if ( // Dont run from specific dates from Tab "x" from 6pm until 10pm
      triggerDate >= fiestaEmpieza && // Compares dates and we know the times are all 00:00:00
      triggerHour >= 18 &&
      triggerDate <= fiestaAcaba && // Compares dates and we know the times are all 00:00:00
      triggerHour <= 21
    ) { 
      return false; 
    } else {
      return true; 
    }
  }
}