Skip to content

Instantly share code, notes, and snippets.

@mjpitz
Last active January 13, 2022 16:38
Show Gist options
  • Save mjpitz/9043fd60ad2a36134e68f790d8f8f59d to your computer and use it in GitHub Desktop.
Save mjpitz/9043fd60ad2a36134e68f790d8f8f59d to your computer and use it in GitHub Desktop.
Various Google AppScripts that I have.

Scripts for https://script.google.com/home/start

Create the project:

  1. Click new "New Project"
  2. Delete the contents of the default project
  3. Paste one of the following snippets into the editor
  4. Name and save your project
  5. In the editor bar, select the function that you want to run (for the tadpoles script, select imageDownloader)
  6. Run the function and grant it the required permissions.

Setting up a trigger:

  1. On the left hand side, click triggers.
  2. Create a new trigger
  3. Select the appropriate function from the dropdown
  4. Use a day timer and run at any time of day that works best for you (for the image downloader, I would run it around 7pm because thats usually when I got them all.
// Uses an initial query to quickly pull ebills / statements from my email, and extract the billing information
var billable = /\$(\d+\.\d\d)/g;
function findBills() {
var query = '"bill is ready" OR "statement is ready" OR "electronic statement" OR "ebill"';
var docs = []
GmailApp.search(query).forEach((thread) => {
thread.getMessages().forEach((message) => {
var matched = billable.exec(message.getBody());
if (!matched) { return; }
docs.push({
timestamp: message.getDate().toISOString(),
who: message.getFrom(),
value: parseFloat(matched[1]),
})
})
})
console.log(docs)
}
// Not in use as much anymore since my kid is no longer in preschool, but figured I'd put it out there for other parents.
// Run this on a daily schedule and it will download any images that are sent to your email and save them to a folder in
// Google drive. Images are organized by [year] and [month] and photos are formated as [day]--[id]--[i] where the id is
// the id of the message, and i is the position of the image in the message.
function mkdirp(root, path) {
var ptr = root;
path.forEach(function(part) {
var iter = ptr.getFoldersByName(part);
if (iter.hasNext()) {
ptr = iter.next();
} else {
ptr = ptr.createFolder(part);
}
});
return ptr;
}
function twodigit(num) {
if (num < 10) {
return '0' + num
} else {
return '' + num
}
}
function imageDownloader() {
var root = DriveApp.getRootFolder();
var folderName = "Tadpoles Photos";
var regexp = /img [^>]*/g;
var query = 'from:updates@tadpoles.com newer_than:1d';
var threads = GmailApp.search(query);
var images = {};
threads.forEach(function(thread) {
var messages = thread.getMessages();
messages.forEach(function(message) {
var date = message.getDate();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = twodigit(date.getDate());
var id = message.getId();
var body = message.getBody();
var $ = Cheerio.load(body);
$('img[alt="tap photo to view at tadpoles"]').each(function(i, elem) {
var src = $(this).attr('src');
if (images[src]) {
return
}
images[src] = {
fileName: [day, id, i].join("--") + ".jpg",
folder: mkdirp(root, [ folderName, year, month ]),
};
});
});
});
Object.keys(images).forEach(function(src) {
var { fileName, folder } = images[src];
var image = UrlFetchApp.fetch(src).getBlob();
image.setName(fileName);
var file = folder.createFile(image);
});
}
@mjpitz
Copy link
Author

mjpitz commented Jan 13, 2022

  • Needs the Cheerio library, but I don't recall how I imported it
  • It also needs permissions for Gmail and Drive

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