Google Apps脚本事件管理器 [英] Google Apps Script Event Manager

查看:106
本文介绍了Google Apps脚本事件管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在试图用马丁Hawksey的谷歌Apps的事件管理器他的博客,但我遇到了两个问题。



#1)在构造电子邮件中的日期是张贴今天日期&时间而不是事件日期&时间(这是在他的原始,他从来没有修复它)。大多数人认为与本节不一样:

$ p $ var variableData = isDate(data [normalizeHeader(templateVars [i] )]);
email = email.replace(templateVars [i],variableData ||);
}

返回电子邮件;
}

//测试值是否为日期,如果是这样格式
函数isDate(sDate){
var scratch = new Date(sDate);
if(scratch.toString()==NaN|| scratch.toString()==Invalid Date){
return sDate;
}
else {
return Utilities.formatDate(new Date(),TZ,dd MMM yy HH:mm);
}
}

#2)我的其他问题在模板中用于加入说明,我无法调用任何变量(即$ {发票}或$ {金额} ::而不是返回今天日期< - 我添加了更多单元格并为每个列添加了一个列,并在其中包含数据,并在脚本中进行了正确调整;仍然没有任何结果。



ex。

 模板:Your Invoice#is:$ {Invoice},您的总金额为:$ {金额}
现实:您的发票号码是:13 Feb 13 13:18,您的总金额是:13 Feb 13 13:18。

以下是我完整的脚本和我所做的更改(与原始版本没有太大差别): https: //gist.github.com/hakarune/4985606



任何和所有的帮助都会非常感激,最大的也是最重要的是....谢谢你

isDate()函数的注释表明如果给定的 sDate 是有效的日期,该日期的格式化版本将被返回。但对 formatDate()的调用会传递 new Date(),这将成为当前日期&时间。相反,它应该通过新日期(sDate)

  return实用程序.formatDate(new Date(sDate),TZ,dd MMM yy HH:mm); 

对于问题#2,看起来问题再次出现在 isDate() fillInTemplateFromObject()函数调用 isDate()来格式化模板数据,如果它是日期的话,否则保持原样。问题是每个数字都会通过 isDate()检查,因为测试仅仅是 new Date(sDate )会产生一个日期。请参阅此参考资料,您会发现它最终会被视为新日期(毫秒)。由于上述错误,你正在获取当前日期......解决这个问题,你将得到不同的日期,但仍然是一个日期。检查检测无效日期如果它在apps-script中有效,那么它可能会提供更确切的测试。



以下是 isDate()让你试试。它包括用于问题#1的修复,以及从 isValidDate()常规拉一个无效日期实例在JavaScript>检测无效日期

  //从https:// stackoverflow中获取。 com / questions / 1353684 
//如果变量d是日期对象,则返回'true'。
函数isValidDate(d){
if(Object.prototype.toString.call(d)!==[object Date])
return false;
return!isNaN(d.getTime());
}

//测试值是否为日期,如果是,则格式为
//否则,将输入变量反映为原样。
的函数isDate(SDATE){
如果(isValidDate(SDATE)){
SDATE = Utilities.formatDate(新日期(SDATE),TZ, DD MMM YY HH:MM);
}
返回sDate;





$ b

如果您好奇,这是我在调试器中运行的测试代码。注释显示调试器中显示的值。

  var TZ =GMT; // isDate()为TimeZone使用全局变量,让我们试试GMT 

函数myFunction(){
var a = new Date(); // Fri Feb 22 2013 20:48:07 GMT-0500(EST)
var b = isDate(a); //23 Feb 13 01:48
var c = 142312; // 142312.0
var d = isDate(c); // 142312.0
var e ='test string'; //test string
var f = isDate(e); //test string
var g ='Feb 22,2013'//2013年2月22日
var h = isDate(g); //2013年2月22日
调试器;
}


I'm currently trying to use Martin Hawksey's Google Apps event manager from his blog, but I am running into 2 problems.

#1) The date in the conformation emails is posting the "today's" date & time instead of the event date & time (it's in his original and he never fixed it). Most are saying has to do with this section not being right:

    var variableData = isDate(data[normalizeHeader(templateVars[i])]);
    email = email.replace(templateVars[i], variableData || "");
  }

  return email;
}

// Test if value is a date and if so format 
function isDate(sDate) {
  var scratch = new Date(sDate);
  if (scratch.toString() == "NaN" || scratch.toString() == "Invalid Date") {
    return sDate;
  } 
  else {
    return Utilities.formatDate(new Date(), TZ, "dd MMM yy HH:mm");
  }
}

#2) My other issue is in the template for joining instructions I can't call-to any variables (i.e. ${"Invoice"} or ${"Amount"} :: instead it returns "today's" date <-- I added more cells and added a column for each and they have data in them, and made correct adjustments in the script; still nothing.

ex.

Template: "Your Invoice # is: ${"Invoice"} and your total amount due is: ${"Amount"}"
Reality: "Your Invoice # is: 13 Feb 13 13:18 and your total amount due is: 13 Feb 13 13:18."

Here is my full script and changes I made (not too different from his original): https://gist.github.com/hakarune/4985606

Any and all help would be very much appreciated, the biggest and most important thing is that date though.... Thanks you

解决方案

For problem #1, the comments for the isDate() function say that if the given sDate is a valid date, a formatted version of that date will be returned. But the call to formatDate() passes new Date(), which will be the current date & time. Instead, it should pass new Date(sDate).

return Utilities.formatDate(new Date(sDate), TZ, "dd MMM yy HH:mm");

For problem #2, looks like the problem is again with isDate(). The fillInTemplateFromObject() function is calling isDate() to format the template data if it's a date, expecting it will be left as-is otherwise. The problem is that every number will pass the isDate() check, since the test is simply whether new Date(sDate) will produce a date. See this reference, you'll see that it would end up being treated as new Date(milliseconds). You're getting the current date because of the bug described above... fix that and you'll get a different date, but still a date. Check Detecting an "invalid date" Date instance in JavaScript, it may provide a more conclusive test, if it works in apps-script.

Here's a fix of isDate() for you to try. It includes the fix for problem #1, and pulls in the isValidDate() routine from Detecting an "invalid date" Date instance in JavaScript to more accurately differentiate between dates and numbers.

// From https://stackoverflow.com/questions/1353684
// Returns 'true' if variable d is a date object.
function isValidDate(d) {
  if ( Object.prototype.toString.call(d) !== "[object Date]" )
    return false;
  return !isNaN(d.getTime());
}

// Test if value is a date and if so format
// otherwise, reflect input variable back as-is. 
function isDate(sDate) {
  if (isValidDate(sDate)) {
    sDate = Utilities.formatDate(new Date(sDate), TZ, "dd MMM yy HH:mm");
  }
  return sDate;
}

If you're curious, this is the test code I ran in the debugger. The comments show what was displayed as values in the debugger.

var TZ = "GMT"; // isDate() uses a global variable for TimeZone, let's try GMT

function myFunction() {
  var a = new Date();     // Fri Feb 22 2013 20:48:07 GMT-0500 (EST)
  var b = isDate(a);      // "23 Feb 13 01:48"
  var c = 142312;         // 142312.0
  var d = isDate(c);      // 142312.0
  var e = 'test string';  // "test string"
  var f = isDate(e);      // "test string"
  var g = 'Feb 22, 2013'  // "Feb 22, 2013"
  var h = isDate(g);      // "Feb 22, 2013"
  debugger;
}

这篇关于Google Apps脚本事件管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆