From a17177829607dcd7892c2b559b4482953cd74e4b Mon Sep 17 00:00:00 2001 From: a13870 Date: Mon, 11 Feb 2013 23:29:02 +0100 Subject: [PATCH] Initial commit --- Code.gs | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 37 ++++++++++++++++- 2 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 Code.gs diff --git a/Code.gs b/Code.gs new file mode 100644 index 00000000..ed7eb30c --- /dev/null +++ b/Code.gs @@ -0,0 +1,116 @@ +// Gmail2GDrive by Andreas Hochsteger +// https://github.com/ahochsteger/gmail2gdrive + +/** + * Configuration for Gmail2GDrive + */ +function getGmail2GDriveConfig() { + return { + // Gmail label for processed threads: + "processedLabel": "gmail2gdrive/processed", + // Sleep time in milli seconds between processed messages: + "sleepTime": 100, + // Maximum script runtime in seconds (google scripts will be killed after 5 minutes): + "maxRuntime": 280, + // Only process message newer than (leave empty for no restriction; use d, m and y for day, month and year): + "newerThan": "1m", + // Processing rules: + "rules": [ + // Rule documentation: + // * filter (String, mandatory): a typical gmail search expression (see http://support.google.com/mail/bin/answer.py?hl=en&answer=7190) + // * folder (String, mandatory): a path to an existing Google Drive folder + // * archive (boolean, optional): Should the gmail thread be archived after processing? (default: false) + // * filenameFrom (String, optional): The attachment filename that should be renamed when stored in Google Drive + // * filenameTo (String, optional): The new filename for the attachment. You can use the special characters 'YYYY' for year, 'MM' for month and 'DD' for day as pattern in the filename. + { + "filter": "to:my.name+scans@gmail.com", + "folder": "Scans" + }, + { + "filter": "from:example1@example.com", + "folder": "Examples/example1" + }, + { + "filter": "from:example2@example.com", + "folder": "Examples/example2" + }, + { + "filter": "(from:example3a@example.com OR from:example3b@example.com)", + "folder": "Examples/example3ab", + "filenameFrom": "file.txt", + "filenameTo": "file-YYYY-MM-DD.txt", + "archive": true + } + ] + }; +} + +/** + * Main function that processes Gmail attachments and stores them in Google Drive. + * Use this as trigger function for periodic execution. + */ +function performGmail2GDrive() { + var config = getGmail2GDriveConfig(); + var gProcessedLabel = config.processedLabel; + var gNewerThan = config.newerThan; + var label = GmailApp.getUserLabelByName(gProcessedLabel); + var end, start; + start = new Date(); + + Logger.log("INFO: Starting mail attachment processing."); + for (var i=0; i= config.maxRuntime) { + Logger.log("WARNING: Self terminating script after " + runTime + "s") + return; + } + var messages = thread.getMessages(); + for (var y=0; y