Guest User

Ingress Portal Submission Tracker - ver 1.5

a guest
Dec 17th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. Ingress Portal Submission Tracker fog Google Sheets - ver 1.5 (unknown author)
  3. based on http://pastebin.com/6cWdH8WT
  4. v1.3: add portal number counter collumn; changed search filter to be inclusive of legacy e-mails from [email protected] and [email protected]
  5. v1.4: add link to Intel map for NIA emails which include the link
  6. v1.5: adjust search query to better detect relevant e-mails
  7.  
  8. Installation:
  9. - start new google docs spreadsheet
  10. - Tools -> Script editor
  11. - paste the whole code, save and close the editor and spreadsheet
  12. - open spreadsheet again
  13. - next to Help menu you will see "Ingress Portals" menu
  14. - Ingress Portals -> Update shreadsheet
  15.  
  16. Embrace the Enlightenment!
  17. **/
  18.  
  19. function ingressSubmissionsTracker() {
  20.  var threads = GmailApp.search("subject:(ingress portal (Submitted|Live|Rejected|Duplicate)) "+"@google.com");
  21.  var sheet   = SpreadsheetApp.getActiveSheet();
  22.  var ss      = SpreadsheetApp.getActiveSpreadsheet()
  23.  ss.toast("Scanning mailbox for Niantic messages. Please wait..", "Scan Started", -1);
  24.  var rowBackgrounds = {
  25.    'Live':'lightgreen',
  26.    'Rejected':'orange',
  27.    'Submitted':'white',
  28.    'Duplicate':'magenta',
  29.  }
  30.  var rownum  = 1;
  31.  sheet.clear();
  32.  sheet.appendRow(["Thread ID","Portal Name","Date Submitted","Date Resolved","Monkey Speed","Age","Email Link","Status","Location"]);
  33.  sheet.setColumnWidth(1, 90);  
  34.  sheet.setColumnWidth(2, 250);
  35.  sheet.setColumnWidth(3, 120);
  36.  sheet.setColumnWidth(4, 120);
  37.  sheet.setFrozenRows(1);
  38.  var portalStatus = {};
  39.  var portalLink = {};
  40.  var resolveDate = {};
  41.  for (var i = 0; i < threads.length; i++) {
  42.    var header = threads[i].getFirstMessageSubject();
  43.    Logger.log("header = " + header);
  44.    var date = threads[i].getLastMessageDate();
  45.    Logger.log("date = " + date);  
  46.    var link = threads[i].getPermalink();
  47.    Logger.log("link = " + link);      
  48.    var portalState = header.split(":")[0].split(" ")[2];
  49.    Logger.log("portalState = " + portalState);
  50.    var portalName = header.split(": ")[1];
  51.    Logger.log("portalName = " + portalName);
  52.    var portalKey = portalName.toUpperCase();
  53.    var sheetLink = '=HYPERLINK("' + link + '" ; "Link")';                                  
  54.    if (!(portalKey in portalStatus)){
  55.      portalStatus[portalKey] = portalState;
  56.    }
  57.    if (!(portalKey in resolveDate)){
  58.      resolveDate[portalKey] = "";
  59.    }
  60.    //If portal Live, try to get Intel coordinates
  61.    Logger.log(portalName + "; portalState = " + portalState);
  62.    if (portalState == 'Live'){
  63.      if (!(portalKey in portalLink)){
  64.        //get messages properly and enumerate
  65.        //var NIAmessages = threads[i].getMessages();
  66.        //threads[x].getMessages()[0].getFrom();
  67.        //blah blah blah .. or just make a dummy assumption about targetting the first message in a thread (Gmail doesn't seem to thread these properly)
  68.        var NIAmsg = threads[i].getMessages()[0].getBody();
  69.        //extract URL
  70.        portalLink[portalKey] = '=HYPERLINK(' + getLink(NIAmsg)+ '; "Intel Link")';
  71.        //portalLink[portalKey] = '=HYPERLINK("' + 'http://ingress.com/intel' + '" ; "Intel Link")';
  72.        //Logger.log("portalKey link = " + portalLink[portalKey]);
  73.      }  
  74.    }  
  75.    if (portalState != "Submitted"){
  76.      portalStatus[portalKey] = portalState;
  77.      resolveDate[portalKey] = date;
  78.    }
  79.    // populate spreadsheet only when we hit Submitted message
  80.    if (portalState == 'Submitted') {
  81.      rownum++;
  82.      sheet.appendRow([i,portalName, date, resolveDate[portalKey],
  83.                      "=IF(NOT(ISBLANK(D"+rownum+"));INT(D"+rownum+"-C"+rownum+");)",
  84.                      "=INT(NOW()-C"+rownum+")",
  85.                      sheetLink, portalStatus[portalKey],portalLink[portalKey]]);
  86.      var lastRow = sheet.getRange("B"+rownum+":I"+rownum);
  87.      lastRow.setBackground(rowBackgrounds[portalStatus[portalKey]]);
  88.    }
  89.  }
  90.  ss.toast("Processed " + rownum + " portals.", "Scanning Done", 10);  
  91. }
  92.  
  93.  
  94.  function getLink(str) {
  95.    //check if href is found then do it.
  96.    // if
  97.    //or just do it the dummy way...
  98.    var link = str.split('<a href=')[1].split('target="_blank">')[0];
  99.    return link;  
  100.  }
  101.  
  102. /**
  103.  * Adds a custom menu to the active spreadsheet, containing a single menu item
  104.  * for invoking the ingressSubmissionsTracker() function specified above.
  105.  * The onOpen() function, when defined, is automatically invoked whenever the
  106.  * spreadsheet is opened.
  107.  * For more information on using the Spreadsheet API, see
  108.  * https://developers.google.com/apps-script/service_spreadsheet
  109.  */
  110. function onOpen() {
  111.   var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  112.   var entries = [{
  113.     name : "Update spreadsheet",
  114.     functionName : "ingressSubmissionsTracker"
  115.   }];
  116.   spreadsheet.addMenu("Ingress Portals", entries);
  117. };
Advertisement
Add Comment
Please, Sign In to add comment