Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function importAtlas() {
- var sheet_atlas = SpreadsheetApp.getActive().getSheetByName('quantcast');
- sheet_atlas.getRange('L2:AA60000').clearContent();
- var sheetName = "quantcast"
- //var last_90days = new Date(SpreadsheetApp.getActive().getSheetByName('date').getRange('A1').getDisplayValue());
- //var month = SpreadsheetApp.getActive().getSheetByName('date').getRange('B1').getDisplayValue()
- //var day = SpreadsheetApp.getActive().getSheetByName('date').getRange('C1').getDisplayValue()
- //var year = SpreadsheetApp.getActive().getSheetByName('date').getRange('D1').getDisplayValue()
- //var last_90days = month.concat("/",day,"/",year)
- //var last_date = new Date(last_90days)
- //Logger.log(last_date)
- //var start = new Date();
- var threads = GmailApp.search("Atlas Report Quantcast_Atlas_Creative-ID_7d_30d")
- var msgs = GmailApp.getMessagesForThreads(threads);
- var newData = [];
- for (var i = 0 ; i < msgs.length; i++) {
- for (var j = 0; j < msgs[i].length; j++) {
- var attachments = msgs[i][j].getAttachments();
- var bodyEmail = msgs[0][0].getBody();
- var regExp = new RegExp('a href="(.*?)"', "gi");
- //var regExp = new RegExp('data-saferedirecturl="(.*?)"', "gi"); // "i" is for case insensitive
- var url = regExp.exec(bodyEmail)[1];
- Logger.log(url)
- var decode = new XML('<d>' + url + '</d>');
- var strDecoded = decode.toString()
- var response = UrlFetchApp.fetch(strDecoded).getContentText();
- var csvdata = Utilities.parseCsv(response)
- var newOrders = []
- //Logger.log(csvdata)
- var map = {};
- var columns = 16;
- for (var eachRow in csvdata){
- //for(var col1 in csvdata[8]){
- //if(csvdata[8][col1] == 'Campaign Name'){ var campaignCol = col1 }
- //else if(csvdata[8][col1] == 'Publisher Name'){ var publisherCol = col1 }
- //else if(csvdata[8][col1] == 'Statistics Date'){ var dateCol = col1 }
- //}
- //Logger.log(dateCol)
- //Logger.log(publisherCol)
- //Logger.log(campaignCol)
- if (eachRow>8)
- {
- var row = csvdata[eachRow];
- var key = getKey(csvdata[eachRow])
- if(key in map){
- // This is a combination we have seen before
- var value = map[key];
- for(var cy = 0; cy < columns; cy++) {
- if(shouldSumColumn(cy)){
- // Sum the columns
- if(typeof value[cy] !== "undefined"){
- value[cy] = value[cy] + row[cy]
- }else{
- value[cy] = row[cy]
- }
- }
- }
- map[key] = value;
- }else{
- var value = [];
- value[0] = row[0];
- value[1] = row[1];
- // Detta är datumet
- value[2] = getFormattedDate(row[2]);
- value[3] = row[3];
- for(var cy = 0; cy < columns; cy++) {
- if(shouldSumColumn(cy)){
- value[cy] = row[cy]
- }
- }
- // Insert new entry into map
- map[key] = value;
- }
- }
- }
- // Pushing the result to newOrders
- for (var key in map) {
- if (map.hasOwnProperty(key)) {
- newOrders.push(map[key]);
- }
- }
- }
- Logger.log(newOrders)
- SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(2,12, newOrders.length, newOrders[0].length).setValues(newOrders)
- }
- }
- /*
- * This function returns true if the column is supposed to be summed up, else false
- */
- function shouldSumColumn(column){
- if(0 <= column && column <=3){ // Ändra dessa gränser beroende på hur många kolumner utan siffror
- return false;
- }else {
- return true;
- }
- }
- /*
- * This function finds the unique key given the row
- */
- function getKey(eachRow){
- // Lägg till mer här för att få in alla fälten utan siffror, dessa är allt vi ska gruppera på
- return "" + eachRow[0] + eachRow[1] + getFormattedDate(eachRow[2]) + eachRow[3]
- }
- /* This function returns the formatted date given a date (without day)*/
- function getFormattedDate(fullDate){
- console.log(fullDate);
- var date = fullDate.split("-");
- console.log(date);
- return date[0] + "-" + date[2];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement