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 :
Hope it will help someone
As triggerId is a
Number
object and as such it is automatically converted to the exponential notation.To handle this you can extend the
Number.prototype
to handle the correct representation.When you request the id of a trigger object using its
getUniqueId()
method, the value returned is a numerical String.However when a function is called by the trigger, the
triggerUid
property of the event object will be that string converted to a Number.The issue is that the string id often (but not always) expresses an integer value larger than the maximum integer value Apps Script can handle safely. When this happens the conversion from string to number results in the zeros in the least significant digits.
So, to compare the two properly you just need to convert the string id to a number, this ensures the same zeroing will occur.
Here's a note about strict v.s. standard comparison operations in Javascript.
Here's some Apps Script that demonstrates all of the above:
The trigger Id could be a large number (long 8 byte integer, or an even bigger number stored as an object), to overcome this, convert it to binary and then to 64 bit encoded string. This way you can safely store, and compare as well.
or to compare