Last active
December 12, 2022 16:21
-
-
Save MacMillanSearch/f483745c7b9cae9056b0a8eb0358279e to your computer and use it in GitHub Desktop.
Adding Stat Reports to Google Sheets using App Scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//menu | |
function onOpen() { | |
var ui = SpreadsheetApp.getUi(); | |
ui.createMenu('Manual Update') | |
.addItem('Import Keywords', 'importKws')//Copy to add additional functions | |
.addToUi(); | |
} | |
var SEARCH_QUERY = "from:noreply@getstat.com subject:Download your STAT report: " //stat report email address and subject | |
var COMPANY_NAME = "MacMillan Search" //company name variable that we put in parentheses when we create the report in Stat | |
//grab the link from the email | |
function getEmails_(q) { | |
var emails = []; | |
var threads = GmailApp.search(q, 0, 1); //get only the latest email - limits!!!! | |
for (var i in threads) { | |
var msgs = threads[i].getMessages(); | |
for (var j in msgs) { | |
emails.push([msgs[j].getPlainBody().split('Link: ')[1].split('\n')[0].replace(/\s/g, "")]); //extract the link from the email and remove final whitespace | |
} | |
} | |
return emails; | |
} | |
//does the heavy lift of taking the email link, unzipping and adding it to the sheet | |
function updateSpreadsheet(NAMING_ARRAY) { | |
var url = getEmails_(SEARCH_QUERY+NAMING_ARRAY[0]+" ("+COMPANY_NAME+")"); //gets the link | |
var zipblob = UrlFetchApp.fetch(url).getBlob(); | |
var unzipblob = Utilities.unzip(zipblob); | |
var unzipstr=unzipblob[0].getDataAsString('UTF-16'); //charset this took me way to long to figure out | |
var csv = Utilities.parseCsv(unzipstr, "\t"); | |
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(NAMING_ARRAY[1]); | |
ss.clearContents().clearFormats(); // clear the old contents | |
ss.getRange(1, 1, csv.length, csv[0].length).setValues(csv) | |
ss.sort(12, false) //sorts by global volume, review which column you want to sort by and adjust accordingly | |
} | |
//The settings for each report these 2 variables & the function name are all you need to change | |
function importKws() { | |
var REPORT_NAME = 'Rankings'; //report name minus the company or project name and parentheses | |
var SHEET_NAME = 'Rankings'; //sheet name | |
var NAMING_ARRAY = [REPORT_NAME,SHEET_NAME] | |
updateSpreadsheet(NAMING_ARRAY); | |
} |
that is the issue, the variables that define the subject of the email don't line up based on what you have defined. Based on the Sciprt you posted above your report in STAT should be named exactly:
Briggs GDS (Briggs Marine/Briggs Marine New)
Try recreating the report with that name and see if it works
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the subject line of the email you are trying to pull the report from?