时间驱动触发器中的功能参数

时间:2014-02-10 09:26:07

标签: google-apps-script

我的函数根据其参数应该有两种不同的行为:

  • 如果没有参数,请执行A

  • 否则请执行B

如果我只是运行该功能在脚本编辑器上选择它并单击运行它将按预期运行。它完成了没有参数的应该做的事情。如果我在时间驱动的触发器中运行它,那么没有任何反应。我认为在触发器中运行该函数也会将参数设置为undefined。我错过了什么吗?

这是功能代码:

function logToday(sede) {
  var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('workedDays');
  var dateElement = new dateElement_();
  var hour=dateElement.hour()+":45";
  var monthYear=dateElement.month(); 
  var day=dateElement.dayInMonth();
  var dayName=dateElement.weekDayName();

  Logger.log("Running log Today with: "+sede);

  if(! sede)
  {

    sede="";hour="";
    sheet.appendRow([monthYear,dayName,day,sede,hour]);

  }
  else
  {
    var LastRow=new LastRow_(sheet);

    if(LastRow.getColValue("sede")=="")
    {
      LastRow.setColValue("sede",sede);
      LastRow.setColValue("Entered Hour",hour);
      if(dateElement.weekDay()==0 || dateElement.weekDay()==6 )colorWeekend(sheet);
      Logger.log(dateElement.weekDay());

    }
  }

}

1 个答案:

答案 0 :(得分:1)

触发时,会从触发器中为函数提供一些参数。例如,每分钟的时间触发器将返回如下内容: {minute = 58,day-of-week = 1,timezone = UTC,day-of-year = 7,second = 20,day-of-month-month = 10,month = 2,year = 2014,hour = 10,authMode =满}

执行此测试的功能:

function test(e){
  SpreadsheetApp.openById("YOUR_SPREADSHEET_ID").appendRow([e]);
}

你可以做的是:在你的代码中,不是将一个简单的字符串解析为参数,你可以将一个对象传递给你的函数:

function passSedeArg(){
  var obj={"sede":"argument"}; // here "argument" must be changed for the old "sede" you where passing
  logToday(obj);
}

function logToday(obj) {
  var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('workedDays');
  var dateElement = new dateElement_();
  var hour=dateElement.hour()+":45";
  var monthYear=dateElement.month(); 
  var day=dateElement.dayInMonth();
  var dayName=dateElement.weekDayName();

  Logger.log("Running log Today with: "+sede);

  if(typeof obj.sede=="undefined") // no obj.sede argument where given
  {

    sede="";hour="";
    sheet.appendRow([monthYear,dayName,day,sede,hour]);

  }
  else // you have a obj.sede argument
  {
    var LastRow=new LastRow_(sheet);

    if(LastRow.getColValue("sede")=="")
    {
      LastRow.setColValue("sede",obj.sede);
      LastRow.setColValue("Entered Hour",hour);
      if(dateElement.weekDay()==0 || dateElement.weekDay()==6 )colorWeekend(sheet);
      Logger.log(dateElement.weekDay());

    }
  }
}