Submissions Script for Fireside Magazine

Below is the script I used to save files to Google Drive. It was originally created by Amit Agarwal, and modified slightly by me to create additional copies and write to a spreadsheet, including reader assignments. Amit’s tutorial also demonstrates how to install this script, so rather than recreate it, I recommend you check out his site. Apologies for the formatting, but I wanted to post the entire script and WordPress security prevented me from uploading the script as an attachment.

/*************************************************

Send to Google Drive by Digital Inspiration, modified by Matt White for Fireside Magazine
——————————————-

tutorial : http://www.labnol.org/?p=21236 (and original code)

contact : amit@labnol.org
twitter : @labnol

CHANGELOG v2.0

1. Fetch 5 messages at a time to prevent timout
2. Folder logic moved to a separate function
3. Better status messages

***************************************************/
function sendToGDrive() {

var sheet = SpreadsheetApp.getActiveSheet();
var gLabel = “submissions”;
var gFolder = “August 2012”;
var gFolderAnon = “August 2012 Anon”;
var readers = [“Reader One”,”Reader Two”,”Reader Three”];

var threads = GmailApp.search(“label:” + gLabel, 0, 5);
var folder = DocsList.getFolder(gFolder);
var folderAnon = DocsList.getFolder(gFolderAnon);

for (var x=0; x<threads.length; x++) {

var messages = threads[x].getMessages();

for (var y=0; y<messages.length; y++) {

var att = messages[y].getAttachments();
var message = messages[y];
var from = message.getFrom();

for (var z=0; z<att.length; z++) {
try {
var folderFiles = DocsList.getFolder(gFolder).getFiles();
var readerIndex = folderFiles.length%readers.length
var file = folder.createFile(att[z]);
var fileAnon = folderAnon.createFile(att[z]);
Utilities.sleep(1000);
var row = SpreadsheetApp.getActiveSheet().getLastRow();
SpreadsheetApp.getActiveSheet().insertRowAfter(row);
SpreadsheetApp.getActiveSheet().appendRow([from,file.getName(),readers[readerIndex]]);
}
catch (e) {
GmailApp.sendEmail(“subs@example.com”,”system error”, e.message);
}
}
}
GmailApp.getUserLabelByName(gLabel)
.removeFromThread(threads[x]);
}
}

function configure() {
var sheet = SpreadsheetApp.getActiveSheet();
var gLabel = “submissions”;

if (!GmailApp.getUserLabelByName(gLabel)) {
GmailApp.createLabel(gLabel);
}

if (ScriptApp.getScriptTriggers().length == 0) {
ScriptApp.newTrigger(“sendToGDrive”).timeBased().everyMinutes(5).create();
}

SpreadsheetApp.getActiveSpreadsheet()
.toast(“You can now close this Google Docs sheet and it will run in the background.”,
“Success”, -1);
}

function onOpen() {
var menu = [
{name: “Initialize”, functionName: “configure”},
{name: “Run”, functionName: “configure”}
];
SpreadsheetApp.getActiveSpreadsheet()
.addMenu(“Submissions”, menu);
}

Advertisements

One thought on “Submissions Script for Fireside Magazine

  1. Pingback: The Fireside Magazine Submissions Process | geekstarter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s