mardi 1 août 2017

Calling function inside If..If Else..Else inside google-app script

Hello Stackoverflow,

I am working on a project in Google SheatSpreed and are using a script wich will trigger activity on the sheet like Edit/Delete/NewLine etc.. and these activites are sent over to Google Analytics with Measurements Protocol.

Now i have a IF...IF ELSE...ELSE statement wich determends if this is a edit, newLine or deleted value.

This is my Code:

function MyOnEditNeW(e){ 

 // Browser.msgBox(JSON.stringify(e));
  var newValue = (typeof e.value == "object" ? e.range.getValue() : e.value); 
  var oldValue = (typeof e.oldValue == "object" ? e.range.getValue() : e.oldValue); 


  if (!e.oldValue ) {
    Browser.msgBox("GA: newValue (NY LINJE)");
  // var firstCell= SpreadsheetApp.getActiveSheet().getActiveSelection().getCell(1,1);

    GoogleAnalyticsEDIT("UA-XXXXXX-2");
  }

  else if (e.value.hasOwnProperty("oldValue")) {
    Browser.msgBox("GA: removeValue (SLETTET VERDI)");
    GoogleAnalyticsSLETT("UA-XXXXXXX-2");

   }
  else {   Browser.msgBox("GA: ChangeValue (REDIGERT VERDI)"); 
       }


// EDIT SEND
function GoogleAnalyticsEDIT(tid){
  var data = {   'v': '1',
                 't' : 'event',
                 'tid': tid,          // TrackingID Google Analytics
                 'cid': Session.getTemporaryActiveUserKey(),
                 'ec': 'SpreadSheet', // Event Kategori.
                 'ea': 'nyVerdiedit',    // Event Hendelse(action)
                 'el':  SpreadsheetApp.getActiveSpreadsheet().getName(),
                 'ev': '300' };       // test og fjern                                                               <----- TEST OG FJERN
  var payload = Object.keys(data).map(function(key) {
                                        return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
                                    }).join('&');
  var options = {'method' : 'POST',
                 'payload' : payload };
  UrlFetchApp.fetch('http://ift.tt/1bgZ9jH', options); 
  Browser.msgBox(JSON.stringify(options));
}


// SLETT SEND
function GoogleAnalyticsSLETT(tid){
  var data = {   'v': '1',
                 't' : 'event',
                 'tid': tid,          // TrackingID Google Analytics
                 'cid': '555',
                 'ec': 'SpreadSheet', // Event Kategori.
                 'ea': 'slettVerdi',    // Event Hendelse(action)
                 'el':  SpreadsheetApp.getActiveSpreadsheet().getName(),
                  };       // test og fjern                                                               <----- TEST OG FJERN
  var payload = Object.keys(data).map(function(key) {
                                        return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
                                    }).join('&');
  var options = {'method' : 'POST',
                 'payload' : payload };

  UrlFetchApp.fetch('http://ift.tt/1bgZ9jH', options); 
  Browser.msgBox(JSON.stringify(options));

}     
}

Problem: The first IF(..) statement works, i get the msgbox alert and data is sent to Google Analytics, BUT NOT the second blow If Else(...) block. The msgbox alert works but the GoogleAnalyticsSLETT(); function is not triggered.

PS: This is a installable-trigger

Anyone knows that maby it's not allowed to call function in If Else statement, or is my code wrong?

Hope someone can help me and guide me to think in right direction.

Thanks :)

Aucun commentaire:

Enregistrer un commentaire