Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adding Stat Reports to Google Sheets using App Scripts
//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);
}
@rollanb
Copy link

rollanb commented Jul 18, 2022

@MacMillanSearch Hi Michael, I've been using this script successfully on multiple Spreadsheets and it's been a huge timesaver. Thanks again!

I've been trying to implement the script on multiple tabs (sheets) on the same Spreadsheet but with no luck. Would you be able to share an example of three scripts running on the same Spreadsheet?

I have two, sometimes three emailed STAT reports in my inbox, that I would like to put into the same Spreadsheet using your script but so far only one sheet would update.

I made sure I edited the COMPANY_NAME, REPORT_NAME and SHEET_NAME, but I think I'm missing additional functions? I tried adding additional functions with unique names but I don't think I did it correctly. Again, only one sheet would update.

Would you be able to share an example I could learn from?
Thank you.

@MacMillanSearch
Copy link
Author

MacMillanSearch commented Jul 19, 2022

@rollanb I am super glad to hear, please connect with me on https://www.linkedin.com/in/michaelkmacmillan/ as I would love to hear more about your use cases

the trick is to make copies of the function

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); }

For each report you want to pull in and give them all unique function names and then name your variables as needed, an issue I ran into is having report names that share words, so make sure there is no overlap in those variables.

Let me know how you make out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment