为什么在Google脚本中由时间驱动的触发器执行时,函数参数的值未定义?

问题描述 投票:0回答:1

我在Google Apps脚本中的时间驱动功能是:

    function addTopRow(d){

      if(d == "undefined"){ //when ran from time trigger
        var dT = (new Date().setHours(0,0,0,0));
        d = new Date(dT);
      }
      else{
        d = new Date(d+"T00:00:00-08:00");
      }

      var row = [];
      var sheet_db = // DEFINE workbook SHEET here
      row.push(d);
      sheet_db.getRange("A3").setValues([row]);
    }

    //run same above function without any trigger, on & when required 
    function testInsertDate("2019-11-10");

**Usage Cases**  
1. On time-driven Trigger every day around 1am. `d` will be UNDEFINED; so set `d` manually to today's date.
2. If called by any other function in sheet with `d` as date string. D will be defined this time.

**Expected Behaviour**  
In Case 1, d is undefined, so function is using today's date.  
In Case 2, d is defined/passed, so function should use that date.

**What's Happening**  
Case 2 is working as expected. 
In Case 1, Date is always being taken as UNIX EPOCH (0) & upon adding the Timezone -8:00 sets it to Dec 31, 1969 always. Expected output is today's date.

**Question**  
Why 'd' is NOT undefined in time driven trigger?  
Is there any documentation/reference which lists all the parameters passed to a function WHEN it is time driven?

javascript function google-apps-script
1个回答
0
投票

根据我的发现部分回答我自己的问题,但也会等待其他答案。

[使用了许多Logger.log()和执行摘要后,我发现当由时间驱动的触发器触发此函数时,d将作为具有以下值的对象出现:

````json
{
    "authMode": {},
    "week-of-year": 47,
    "day-of-week": 7,
    "month": 11,
    "hour": 1,
    "year": 2019,
    "timezone": "UTC",
    "day-of-month": 24,
    "triggerUid": "2594419",
 ...)

很显然,...部分意味着还有更多的键和值Logger.log()决定要截断。

因此,我更新了以下if条件以检查typeof string而不是未定义的状态:

````javascript
//Previous Line:
if(d == "undefined"){ //when ran from time trigger

//Changed To:
if(typeof d != "string"){ //when ran from time trigger

&现在正在工作。

© www.soinside.com 2019 - 2024. All rights reserved.