JavaScript Google App Script Trigger Id Format google script trigger on form submit,google script trigger not

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

Answer:1

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

Answer:2

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.

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

Here's a note about strict v.s. standard comparison operations in Javascript.

Here's some Apps Script that demonstrates all of the above:

// Run onceOffClockTimer() and wait up to 1 min for the event to fire, then check your log. You might have to run it a few times to see a trigger id where the zeroing occurred

function onceOffClockTimer() {
  var newTriggerId = ScriptApp.newTrigger('timerCallbackFn')
    .timeBased()
    .after(5000)
    .create()
    .getUniqueId();
}


function timerCallbackFn(triggerEvent) {

  var eventTriggerId = triggerEvent.triggerUid,
    triggers = ScriptApp.getProjectTriggers();
  Logger
    .log('### Event Object ###')
    .log('id: %s', eventTriggerId)
    .log('id type: %s', typeof eventTriggerId)
    .log('id as String: %s\n', eventTriggerId.toString());

  var largestSafeInteger = 9007199254740991; // Obtained by Number.MAX_SAFE_INTEGER in a Chrome console 

  Logger
    .log('### Interesting Fact ###')
    .log('In Apps Script the largest safe integer is: %s\n', largestSafeInteger);

  for (var i = 0, x = triggers.length; i < x; i++) {

    var triggerID = triggers[i].getUniqueId();

    Logger
      .log('### Trigger Object [%s] ###', i)
      .log('id: %s', triggerID)
      .log('id type: %s', typeof triggerID)
      .log('id as Number: %s\n', Number(triggerID))

    Logger
      .log('### Comparisons ###')
      .log('Strict (Trigger Event ID === Trigger ID) is %s ', eventTriggerId === triggerID)
      .log('Strict (Trigger Event ID === Trigger ID as Number) is %s', eventTriggerId === Number(triggerID))
      .log('Direct (Trigger Event ID == Trigger ID) is %s', eventTriggerId == triggerID);
  }
}
Answer:3
Logger.log('' + triggerId);;;;
Answer:4

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.

var v = Utilities.base64Encode(Utilities.newBlob(e.triggerUid).getBytes())

or to compare

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

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.

Object.defineProperty(Number.prototype, 'toLongString', {
  value: function() {
    var parts = this.toString().split("e+");
    var first = parts[0].replace('.', "");
    var zeroes = parseInt(parts[1], 10) - (first.length - 1);

    var longString = first;

    for (var i = 0; i < zeroes; i++) {
      longString += "0";
    }

    return longString;
  }
});

function mainFunction(e) {
  var triggerId = e.triggerUid.toLongString();
  Logger.log(triggerId);
}
Answer:6

I'm embedding Rhino in Java and I want to create a global property with a getter and setter. This non-global property definition works: var testObj = {}; Object.defineProperty(testObj, 'testPropName',...

I'm embedding Rhino in Java and I want to create a global property with a getter and setter. This non-global property definition works: var testObj = {}; Object.defineProperty(testObj, 'testPropName',...

  1. define global property
  2. gradle define global property
  3. soapui define global property
  4. ant define global property
  5. maven define global properties

I need to plot a graph in a canvas. But how can I use an algebra equation as input, and based on the equation, draw the curve, using javascript? For example: x2+5y=250 The equation plots a graph ...

I need to plot a graph in a canvas. But how can I use an algebra equation as input, and based on the equation, draw the curve, using javascript? For example: x2+5y=250 The equation plots a graph ...

  1. plot graph based on points
  2. plot graph based on equation
  3. plot graph based on equation excel
  4. plot based graph
  5. plot the graph based on your observations
  6. plot the graph based on multicollinearity

I'm trying to fire a click event based on an element I get using eq(). I have three elements with the same class name: ag-list-item. <!-- item one --> <div ag-repeat="" class="ag-list-item ...

I'm trying to fire a click event based on an element I get using eq(). I have three elements with the same class name: ag-list-item. <!-- item one --> <div ag-repeat="" class="ag-list-item ...

Local variables in functions seem to persist after the function is called. Is this always the case? How does this work? // example 1 function Obj(str) { this.show = function () { ...

Local variables in functions seem to persist after the function is called. Is this always the case? How does this work? // example 1 function Obj(str) { this.show = function () { ...

  1. variables lifetime javascript