Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function importAtlas() {
  3.  var sheet_atlas = SpreadsheetApp.getActive().getSheetByName('quantcast');
  4.  sheet_atlas.getRange('L2:AA60000').clearContent();
  5.  var sheetName = "quantcast"
  6.  
  7.  //var last_90days = new Date(SpreadsheetApp.getActive().getSheetByName('date').getRange('A1').getDisplayValue());
  8.  //var month = SpreadsheetApp.getActive().getSheetByName('date').getRange('B1').getDisplayValue()
  9.  //var day = SpreadsheetApp.getActive().getSheetByName('date').getRange('C1').getDisplayValue()
  10.  //var year = SpreadsheetApp.getActive().getSheetByName('date').getRange('D1').getDisplayValue()
  11.  //var last_90days = month.concat("/",day,"/",year)
  12.  //var last_date = new Date(last_90days)
  13.  //Logger.log(last_date)
  14.  //var start = new Date();
  15.  
  16.  var threads = GmailApp.search("Atlas Report Quantcast_Atlas_Creative-ID_7d_30d")
  17.  var msgs = GmailApp.getMessagesForThreads(threads);
  18.  var newData = [];
  19.  
  20.   for (var i = 0 ; i < msgs.length; i++) {
  21.    for (var j = 0; j < msgs[i].length; j++) {
  22.      var attachments = msgs[i][j].getAttachments();
  23.      var bodyEmail = msgs[0][0].getBody();
  24.    
  25.      var regExp = new RegExp('a href="(.*?)"', "gi");
  26.      //var regExp = new RegExp('data-saferedirecturl="(.*?)"', "gi"); // "i" is for case insensitive
  27.      var url = regExp.exec(bodyEmail)[1];
  28.      Logger.log(url)
  29.      var decode = new XML('<d>' + url + '</d>');
  30.      var strDecoded = decode.toString()
  31.      var response = UrlFetchApp.fetch(strDecoded).getContentText();
  32.      var csvdata = Utilities.parseCsv(response)
  33.      var newOrders = []
  34.      //Logger.log(csvdata)
  35.      var map = {};
  36.      var columns = 16;
  37.      for (var eachRow in csvdata){
  38.        //for(var col1 in csvdata[8]){
  39.          //if(csvdata[8][col1] == 'Campaign Name'){ var campaignCol = col1 }
  40.        
  41.          //else if(csvdata[8][col1] == 'Publisher Name'){ var publisherCol = col1 }
  42.          
  43.          //else if(csvdata[8][col1] == 'Statistics Date'){ var dateCol = col1 }
  44.          
  45.        //}
  46.          //Logger.log(dateCol)
  47.          //Logger.log(publisherCol)
  48.          //Logger.log(campaignCol)
  49.      
  50.        if (eachRow>8)
  51.        {
  52.                 var row = csvdata[eachRow];
  53.                 var key = getKey(csvdata[eachRow])
  54.                 if(key in map){
  55.                     // This is a combination we have seen before
  56.                     var value = map[key];
  57.                     for(var cy = 0; cy < columns; cy++) {
  58.                         if(shouldSumColumn(cy)){
  59.                             // Sum the columns
  60.                             if(typeof value[cy] !== "undefined"){
  61.                                 value[cy] = value[cy] + row[cy]
  62.                             }else{
  63.                                 value[cy] = row[cy]
  64.                             }
  65.                         }
  66.                     }
  67.                     map[key] = value;
  68.                 }else{
  69.                     var value = [];
  70.                    
  71.                     value[0] = row[0];
  72.                     value[1] = row[1];
  73.                     // Detta är datumet
  74.                     value[2] = getFormattedDate(row[2]);
  75.                     value[3] = row[3];
  76.                    
  77.                     for(var cy = 0; cy < columns; cy++) {
  78.                         if(shouldSumColumn(cy)){
  79.                             value[cy] = row[cy]
  80.                         }
  81.                     }
  82.                     // Insert new entry into map
  83.                     map[key] = value;
  84.                 }  
  85.         }
  86.    }
  87.    
  88.    // Pushing the result to newOrders
  89.    for (var key in map) {
  90.       if (map.hasOwnProperty(key)) {
  91.         newOrders.push(map[key]);
  92.       }
  93.     }
  94.  
  95.   }
  96.   Logger.log(newOrders)
  97. SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(2,12, newOrders.length, newOrders[0].length).setValues(newOrders)
  98.  
  99.   }
  100. }
  101.  
  102. /*
  103. * This function returns true if the column is supposed to be summed up, else false
  104. */
  105. function shouldSumColumn(column){
  106.     if(0 <= column && column <=3){ // Ändra dessa gränser beroende på hur många kolumner utan siffror
  107.         return false;
  108.     }else {
  109.         return true;
  110.     }  
  111. }
  112.  
  113. /*
  114. * This function finds the unique key given the row
  115. */
  116. function getKey(eachRow){
  117.     // Lägg till mer här för att få in alla fälten utan siffror, dessa är allt vi ska gruppera på
  118.     return "" + eachRow[0] + eachRow[1] + getFormattedDate(eachRow[2]) + eachRow[3]
  119. }
  120.  
  121. /* This function returns the formatted date given a date (without day)*/
  122. function getFormattedDate(fullDate){
  123.         console.log(fullDate);
  124.         var date = fullDate.split("-");
  125.         console.log(date);
  126.         return date[0] + "-" + date[2];
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement