Javascript Google应用程序脚本触发器Id格式

Javascript Google应用程序脚本触发器Id格式,javascript,google-apps-script,google-apps,Javascript,Google Apps Script,Google Apps,我正在谷歌电子表格的项目中工作,通过脚本添加/删除基于时间的触发器 激发的所有触发器将运行一个函数,该函数将检查其Id并相应地运行函数 我正在保存启动触发器并用此代码保存它的id function startTimer(rn) { var triggerid = ScriptApp.newTrigger('mainFunction') .timeBased() .everyMinutes(1) .create().getUniqueId(); Spr

我正在谷歌电子表格的项目中工作,通过脚本添加/删除基于时间的触发器

激发的所有触发器将运行一个函数,该函数将检查其Id并相应地运行函数

我正在保存启动触发器并用此代码保存它的id

function startTimer(rn) {
  var triggerid = ScriptApp.newTrigger('mainFunction')
      .timeBased()
      .everyMinutes(1)
      .create().getUniqueId();

  SpreadsheetApp.getActiveSheet().getRange(1, 8).setValue(triggerid);
}
此函数用于保存格式为“6295890543675972447”的触发器id

现在,当触发并运行函数“mainFunction”时。 使用此代码,我试图获取触发器的id

function mainFunction(e) {
  var triggerId = e.triggerUid;
  Logger.log(triggerId);
}
此时,我得到了格式为“6.29589E+18”的触发器id
如果我尝试使用toString()方法更改格式,格式将更改为“6295890543675973000”

我无法将这两种格式与我保存的id匹配。

知道我怎样才能把id恢复到添加触发器时的格式吗?



谢谢

,因为triggerId是一个
数字
对象,因此它会自动转换为指数表示法

要处理此问题,可以扩展
Number.prototype
以处理正确的表示

Object.defineProperty(Number.prototype,'toLongString'{
值:函数(){
var parts=this.toString().split(“e+”);
var first=parts[0]。替换(“,”);
var zeroes=parseInt(parts[1],10)-(first.length-1);
var longString=第一个;
对于(变量i=0;i<0;i++){
长字符串+=“0”;
}
返回长串;
}
});
函数main函数(e){
var triggerId=e.triggerUid.toLongString();
Logger.log(triggerId);
}

触发器Id可以是一个大数字(长8字节整数,或存储为对象的更大数字),要克服这一问题,请将其转换为二进制,然后转换为64位编码字符串。这样您就可以安全地存储和比较

var v = Utilities.base64Encode(Utilities.newBlob(e.triggerUid).getBytes())
还是比较

if(Utilities.base64Encode(Utilities.newBlob(e.triggerUid).getBytes()) === Utilities.base64Encode(Utilities.newBlob(triggers[i].getUniqueId()).getBytes()))

我花了几个小时在这上面,弄掉了一些头发。 我发现了。它有一段代码,当您在其中传递e.triggerUid时会奇怪地工作,而当您直接使用它时则不会:

function getFileByTriggerId(triggerId){
  var triggers = ScriptApp.getProjectTriggers();
  for(var i =0; i<triggers.length; i++){
    if(triggers[i].getUniqueId() == triggerId){
      return triggers[i].getTriggerSourceId();
    }
  }
}
函数getFileByTriggerId(triggerId){ var triggers=ScriptApp.getProjectTriggers();
对于(var i=0;i当您使用触发器对象的
getUniqueId()
方法请求触发器对象的id时,返回的值是一个数值字符串

但是,当触发器调用函数时,事件对象的
triggerUid
属性将是转换为数字的字符串

问题是字符串id通常(但不总是)表示的整数值大于应用程序脚本可以安全处理的最大整数值。当发生这种情况时,从字符串到数字的转换会导致最低有效位的零

因此,要正确地比较两者,只需将字符串id转换为数字,这将确保相同的归零操作

someTrigger.getUniqueId() === event.triggerUid
// false - because the left side is a string, and the right is a number. 

Number(someTrigger.getUniqueId()) === event.triggerUid
// true - both sides are now numbers, the same number

someTrigger.getUniqueId() == event.triggerUid
// true - because using the standard comparison operator the string is automatically converted to a number in the background
下面介绍Javascript中严格的v.s.标准比较操作

以下是一些应用程序脚本,演示了上述所有功能:

//运行onceOffClockTimer()并等待事件触发1分钟,然后检查日志。可能需要运行几次才能查看发生归零的触发器id
函数onceOffClockTimer(){
var newTriggerId=ScriptApp.newTrigger('timerCallbackFn')
.基于时间的()
.之后(5000)
.create()
.getUniqueId();
}
功能timerCallbackFn(triggerEvent){
var eventTriggerId=triggerEvent.triggerUid,
触发器=ScriptApp.getProjectTriggers();
记录器
.log('####事件对象###')
.log('id:%s',eventTriggerId)
.log('id类型:%s',eventTriggerId的类型)
.log('id为字符串:%s\n',eventTriggerId.toString());
var largestsafeinger=9007199254740991;//通过Chrome控制台中的Number.MAX_SAFE_INTEGER获得
记录器
.log(有趣的事实)
.log('在应用程序脚本中,最大的安全整数是:%s\n',最大的安全整数);
for(变量i=0,x=triggers.length;i}
我也遇到了同样的问题。对我有效的解决方案是:
toPrecision([numberOfDigits])

例如:

([largenumber]).toPrecision(27)

资料来源如下:

我希望这能帮助别人

someTrigger.getUniqueId() === event.triggerUid
// false - because the left side is a string, and the right is a number. 

Number(someTrigger.getUniqueId()) === event.triggerUid
// true - both sides are now numbers, the same number

someTrigger.getUniqueId() == event.triggerUid
// true - because using the standard comparison operator the string is automatically converted to a number in the background