Asim
Asim

Reputation: 976

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

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

Now i have a If...Else if...Else which determines that this is an edit, newLine or deleted existing value.

This is my Code:
// UPDATED code after answer, but still not working.

    var GoogleAnalyticsEDIT;
var GoogleAnalyticsSLETT;
var GoogleAnalyticsREDIGER;


// MP
// EDIT SEND
GoogleAnalyticsEDIT = function GoogleAnalyticsEDIT(tid){
  var data = {   'v': '1',
                 't' : 'event',
                 'tid': tid,          // TrackingID Google Analytics
                 'cid': Session.getTemporaryActiveUserKey(),
                 'ec': 'SpreadSheet', // Event Kategori.
                 'ea': 'nyVerdi-'+noLetter,    // 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://www.google-analytics.com/collect', options); 
}



//MP
// SLETT SEND
GoogleAnalyticsSLETT = function GoogleAnalyticsSLETT(tid){
  var data = {   'v': '1',
                 't' : 'event',
                 'tid': tid,          // TrackingID Google Analytics
                 'cid': Session.getTemporaryActiveUserKey(),
                 'ec': 'SpreadSheet', // Event Kategori.
                 'ea': 'slettVerdi-'+noLetter,    // 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://www.google-analytics.com/collect', options); 
}   




// MP  
// REDIGER SEND
GoogleAnalyticsREDIGER = function GoogleAnalyticsREDIGER(tid){
  var data = {   'v': '1',
                 't' : 'event',
                 'tid': tid,          // TrackingID Google Analytics
                 'cid': Session.getTemporaryActiveUserKey(),
                 'ec': 'SpreadSheet', // Event Kategori.
                 'ea': 'redigerVerdi-'+noLetter,    // 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://www.google-analytics.com/collect', options); 
}   




//FUNKSJON ---- DEBUG
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)");
   GoogleAnalyticsEDIT("UA-101502909-2"); 
    }

  else if (e.value.hasOwnProperty("oldValue")) {
   GoogleAnalyticsSLETT("UA-101502909-2");
     }

  else { 
    Browser.msgBox("GA: ChangeValue (REDIGERT VERDI)"); 
   GoogleAnalyticsREDIGER("UA-101502909-2")

    }


} //slutt: MyOnEditNeW();

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. This is a installable-trigger

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

Upvotes: 1

Views: 999

Answers (1)

Cooper
Cooper

Reputation: 64100

I'd probably change this: else if (e.value.hasOwnProperty("oldValue")) to this else if (e.oldvalue)

Upvotes: 2

Related Questions