пятница, 29 июля 2011 г.

The Solution

http://googleappsdeveloper.blogspot.com/2011/07/gmail-snooze-with-apps-script.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+GoogleAppsDeveloperBlog+%28Google+Apps+Developer+Blog%29

The Solution

Here is the Apps Script code for a "Gmail Snooze" extension. First some configuration details. Setting these variables determines whether "unsnoozed" mail gets marked as unread, and whether it gets its own special label.
var MARK_UNREAD = false;
var ADD_UNSNOOZED_LABEL = false;

Setup

The "setup" function creates a new "Snooze" label in your Gmail, along with 7 sublabels for snoozing for different lengths of time, and potentially an "Unsnoozed" label. Running this function will also prompt you to authorize the script to use Gmail. This function makes use of the "getLabelName" helper function, which will be used by the code below.
function getLabelName(i) {
return "Snooze/Snooze " + i + " days";
}

function setup() {
// Create the labels we'll need for snoozing
GmailApp.createLabel("Snooze");
for (var i = 1; i <= 7; ++i) {
GmailApp.createLabel(getLabelName(i));
}
if (ADD_UNSNOOZED_LABEL) {
GmailApp.createLabel("Unsnoozed");
}
}

Moving the Snooze Queue

The "moveSnoozes" function moves messages one day forward in the queue, so that messages snoozed for 6 days are now snoozed for 5 days, etc. Messages in the 1-day label are moved back into the inbox, and potentially marked as unread. To make this work automatically, you'll need to create a nightly event trigger to run "moveSnoozes". See the more detailed instructions at the bottom of the post.
  function moveSnoozes() {
var oldLabel, newLabel, page;
for (var i = 1; i <= 7; ++i) {
newLabel = oldLabel;
oldLabel = GmailApp.getUserLabelByName(getLabelName(i));
page = null;
// Get threads in "pages" of 100 at a time
while(!page || page.length == 100) {
page = oldLabel.getThreads(0, 100);
if (page.length > 0) {
if (newLabel) {
// Move the threads into "today's" label
newLabel.addToThreads(page);
} else {
// Unless it's time to unsnooze it
GmailApp.moveThreadsToInbox(page);
if (MARK_UNREAD) {
GmailApp.markThreadsUnread(page);
}
if (ADD_UNSNOOZED_LABEL) {
GmailApp.getUserLabelByName("Unsnoozed")
.addToThreads(page);
}
}
// Move the threads out of "yesterday's" label
oldLabel.removeFromThreads(page);
}
}
}
}

Комментариев нет:

Отправить комментарий