do something in 5 with gas 7-email log

15

Click here to load reader

Upload: bruce-mcpherson

Post on 11-Jul-2015

1.001 views

Category:

Data & Analytics


1 download

TRANSCRIPT

Page 1: Do something in 5 with gas 7-email log

do something useful with Apps Script in 5 minutes

7. Log who you sent emails to in a sheetBruce McPhersonwww.mcpher.com

Page 3: Do something in 5 with gas 7-email log

Add libraries to script

● create a spreadsheet● get its id● create a script● Open resources● Add references to librariesMHfCjPQlweartW45xYs6hFai_d-phDA33Mrckbr9_w7PCphJtOzhzA_Cz3TLx7pV4j

Mcbr-v4SsYKJP7JMohttAZyz3TLx7pV4j

Page 4: Do something in 5 with gas 7-email log

layout what you are going to do

function myFunction() {

// get all email threads matching search query

// extract the messages and recipients for each thread

// open a sheet

// clear sheet

// write results

}

Page 5: Do something in 5 with gas 7-email log

get all matching email threads

For this example, we’ll keep it simple - if you expected many results you’d need to handle continuation as by default this only returns a chunk of the matching queries.

What you’ll get back is an array of GMailThread

// get all email threads matching search query

var threads = GmailApp.search("The Excel Liberation forum has moved to a Google+ community");

Page 6: Do something in 5 with gas 7-email log

set up exponential backoffGetting messages within threads is rate limited in Google Apps Script. If we ask for too many too quickly, it will fail. Exponential backoff is a technique that recovers from errors like this and tries again. You’ll only be able to process about 300 messages within the 6 minute GAS execution quota

var emails = threads.reduce ( function (p,c) {

// get messages within each thread and append each recipient

cUseful.rateLimitExpBackoff(function () {

});

return p;

},[]);

Page 7: Do something in 5 with gas 7-email log

get the message threadsEach thread can contain multiple messages, go through them using Array.forEach() to extract info from each method

var emails = threads.reduce ( function (p,c) {

// get messages within each thread and append each recipient

cUseful.rateLimitExpBackoff(function () {

c.getMessages().forEach(function(d) {

});

});

return p;

},[]);

Page 8: Do something in 5 with gas 7-email log

Map the recipientsEach message can contain multiple recipients in the To: property. These are comma separated so we can use String.split(“,”) to create an array, then Array.Map() to transform each item and extract the summary information we need into a new object. Finally these are appended to the growing array being built up by .reduce() var emails = threads.reduce ( function (p,c) {

// get messages within each thread and append each recipient

cUseful.rateLimitExpBackoff(function () {

c.getMessages().forEach(function(d) {

cUseful.arrayAppend(p, d.getTo().split(",").map(function(e) {

return {to:e,subject:d.getSubject(),dateSent:d.getDate().toString(),from:d.getFrom()};

}));

});

});

return p;

},[]);

Page 9: Do something in 5 with gas 7-email log

Open sheetUsing database abstraction, open a sheet as a database.

// open a new sheet

var handler = new cDbAbstraction.DbAbstraction(cDriverSheet, {

"siloid": "emails",

"dbid": "1yTQFdN_O2nFb9obm7AHCTmPKpf5cwAd78uNQJiCcjPk"

});

and remove any data already there

// remove anything already there

var result = handler.remove();

if (handler.handleCode < 0) throw JSON.stringify(result);

Page 10: Do something in 5 with gas 7-email log

Log the resultsUsing Array.sort() , sort by recipient email address then write the results to a sheet

// sort and log results

var result = handler.save(emails.sort(function (a,b) {

return a.to > b.to ? 1 : (a.to < b.to ? -1 : 0) ;

}));

if (handler.handleCode < 0) throw JSON.stringify(result);

Page 11: Do something in 5 with gas 7-email log

Run and authorize it

Page 12: Do something in 5 with gas 7-email log

Here’s the result

Page 13: Do something in 5 with gas 7-email log

The whole thingfunction myFunction() {

// get all matching emails

var threads = GmailApp.search(

"The Excel Liberation forum has moved to a Google+ community");

var emails = threads.reduce ( function (p,c) {

// get messages within each thread and append each recipient

cUseful.rateLimitExpBackoff(function () {

c.getMessages().forEach(function(d) {

cUseful.arrayAppend(p, d.getTo().split(",").map(function(e) {

return {

to:e,

subject:d.getSubject(),

dateSent:d.getDate().toString(),

from:d.getFrom() };

}));

});

});

return p;

},[]);

// open a new sheet

var handler = new cDbAbstraction.DbAbstraction (

cDriverSheet, {

"siloid": "emails",

"dbid": "1yTQFdN_O2nFb9obm7AHCTmPKpf5cwAd78uNQJiCcjPk"

});

if (!handler.isHappy()) throw 'could not open sheet';

// remove anything already there

var result = handler.remove();

if (handler.handleCode < 0) throw JSON.stringify(result);

// sort and log results

var result = handler.save(emails.sort(function (a,b) {

return a.to > b.to ? 1 : (a.to < b.to ? -1 : 0) ;

}));

if (handler.handleCode < 0) throw JSON.stringify(result);

}

Page 14: Do something in 5 with gas 7-email log

Homework

Apps Script quota prevents you dealing with more than about 300 threads within the execution time quota. Figure out how to deal with more than that using search(query, start, max) and running the task several times