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

@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.

@StephMillerSEO
Copy link

Hi, I've used this code a couple of times and it's been a timesaver when I can get it working, I'm having some issues with getting it going for another sheet. I've taken the code out and replaced it a few times, ensuring ll my details are correct where they need to be, but when I run this one it comes up with this error and I can't seem to figure out why it's erroring. Any help would be appreciated!
Screenshot 2022-12-12 at 15 14 18

@MacMillanSearch
Copy link
Author

@StephMillerSEO odd, nothing sticks out to me. Are you able to run the script from the spreadsheet?

@StephMillerSEO
Copy link

@StephMillerSEO odd, nothing sticks out to me. Are you able to run the script from the spreadsheet?

Hi, no, it comes up with this error when I press 'Manual Update'

Screenshot 2022-12-12 at 15 42 49

@MacMillanSearch
Copy link
Author

What is the subject line of the email you are trying to pull the report from?

@StephMillerSEO
Copy link

I've double checked it a few times and it all looks good - here's the email subject
Screenshot 2022-12-12 at 15 57 16

@MacMillanSearch
Copy link
Author

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