Google应用脚本触发标识格式 [英] Google App Script Trigger Id Format

查看:95
本文介绍了Google应用脚本触发标识格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



所有触发器都会运行一个函数,它会检查它的Id和运行相应的函数。

我保存开始触发器并使用此代码保存它的ID。

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

SpreadsheetApp.getActiveSheet()。getRange(1,8).setValue(triggerid);





这个函数以'6295890543675972447'格式保存触发器ID



现在触发器被触发并运行'mainFunction'函数。
使用这段代码我试图得到触发器的id。

$ p $ function mainFunction(e){
var triggerId = e.triggerUid;
Logger.log(triggerId);
}

此时,我得到这种格式为'6.29589E + 18'


如果我尝试使用toString()方法更改格式,则格式更改为'6295890543675973000'



我无法匹配都格式化到我保存的ID。



任何想法如何以加入触发器的格式获取id?




谢谢

解决方案

我在这上面花了几个小时,弄了一些头发。
我发现此页。它有一段奇怪的代码,当你传入e.triggerUid时不起作用,而不是直接使用它:

 函数getFileByTriggerId(triggerId){
var triggers = ScriptApp.getProjectTriggers();
for(var i = 0; i< triggers.length; i ++){
if(triggers [i] .getUniqueId()== triggerId){
return triggers [i] .getTriggerSourceId ();



$ / code $ / pre

希望它能帮助某人


I am working on Google Spreadsheet's project where I am adding/deleting TimeBased triggers through scripting.

All triggers fired will run one function, which will check its Id and run function accordingly.

I am saving starting trigger and saving it's id with this code.

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

  SpreadsheetApp.getActiveSheet().getRange(1, 8).setValue(triggerid);
}

This functions save's trigger id in this format '6295890543675972447'

Now when trigger is fired and runs function 'mainFunction'. Using this code I am trying to get the id of trigger.

function mainFunction(e) {
  var triggerId = e.triggerUid;
  Logger.log(triggerId);
}

At this point, I get the trigger id in this format '6.29589E+18'
If I try to change format with toString() method , the format changes to '6295890543675973000'

I cant match with both formatting to my saved id.

Any idea how can I get id back in format it was when adding trigger?


Thanks

解决方案

I spent some hours on this, got some hair off. And I found this page. It has a piece of code that strangely works when you pass e.triggerUid in it, and not when using it directly :

function getFileByTriggerId(triggerId){
  var triggers = ScriptApp.getProjectTriggers();
  for(var i =0; i<triggers.length; i++){
    if(triggers[i].getUniqueId() == triggerId){
      return triggers[i].getTriggerSourceId();
    }
  }
}

Hope it will help someone

这篇关于Google应用脚本触发标识格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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