I'm building a script to automatically move my emails from one label to the next on a day by day basis (so that a message that is stored labelled "5 days" will automatically be labelled "4 days" tomorrow then "3 days" the day after that, and so on).
The problem is that it applies the label to the entire thread, not just the message - which is problematic if you have Conversation View turned off and the various emails seperated under different labels.
Here is my script (which has a daily trigger):
function moveUp(previousLabel, newLabel) {
var threads = GmailApp.getUserLabelByName(previousLabel).getThreads()
var numThreads = threads.length
if (numThreads>0) {
for(var i = 0; i < numThreads; i++) {
if(GmailApp.getUserLabelByName(previousLabel).getThreads().length>0) {
var lastThread = GmailApp.getUserLabelByName(previousLabel).getThreads()[0]
GmailApp.getUserLabelByName(newLabel).addToThread(lastThread.markUnread());
GmailApp.getUserLabelByName(previousLabel).removeFromThread(lastThread)
Utilities.sleep(200)
} else {
{break;}
}
}
}
}
function myFunction() {
var threads = GmailApp.getUserLabelByName("-To Do/1 Day").getThreads()
var numThreads = threads.length
if (numThreads>0) {
for(var i = 0; i < numThreads; i++) {
if(GmailApp.getUserLabelByName("-To Do/1 Day").getThreads().length>0) {
var lastThread = GmailApp.getUserLabelByName("-To Do/1 Day").getThreads()[0]
lastThread.moveToInbox().markUnread();
GmailApp.getUserLabelByName("-To Do/1 Day").removeFromThread(lastThread)
Utilities.sleep(200)
} else {
{break;}
}
}
}
moveUp("-To Do/2 Days", "-To Do/1 Day")
moveUp("-To Do/3 Days", "-To Do/2 Days")
moveUp("-To Do/4 Days", "-To Do/3 Days")
moveUp("-To Do/5 Days", "-To Do/4 Days")
}
So does anyone know how to only apply the labels to specific messages? Or a workaround to achieve that same result?
Thanks
You can do this using the GMail API instead. You need to enable the API for your project. In the editor, select
Resources > Advanced Google services
, then click on "These services must also be enabled in the Google Developers Console." There, enable the GMail API.modifyMessage
Example of use:
TL;DR The full code is available in this gist, and below in a hidden code snippet.
The GMail API is a RESTful API with more flexible and detailed access to Threads, Messages, Labels, Drafts, and History than is provided using the built-in GMailApp Service. From our script, we access the API by URLs - here, we start by filling in details in the base URL for the
modify
Message method.In addition to enabling the API for our project, each request requires user authentication, which is done by embedding a Bearer Token in the command header.
Next, we will build the rest of the command parameters.
The specification for
modify
calls for aPOST
operation, with a payload containing lists of Label IDs. Those IDs are not available through the built-in GMail Service, so we'll also use the GMail API for that -getLabelId()
is another function similar to this one, specialized for retrieving the ID of labels by name, and creating new labels as needed. See the gist or snippet for that code.Finally, we have assembled the
params
object for the command, with properties containing theheaders
with our authentication token, andpayload
with the command parameters,stringify
-d for transport. SettingmuteHttpExceptions: true
squelches system-generated exceptions in case of failure, so that we can handle them ourselves.I've commented out a call to
UrlFetchApp.getRequest()
- it's handy for validating the content of a command before sending it.The call to
UrlFetchApp.fetch()
transmits our command to the service running at the URL we built up top.When a response is received, we first check the
result
of the command, which is in the form of an HTTP Status Code. A200 OK
tells us that we were successful, so we go ahead and pull our result out of the response.JSON.parse()
turns the JSON string in the message into a JavaScript object, which we return to the caller.If we encountered an error, though, we throw our own exception. Depending on the details of a command, we may want to handle specific errors differently. For example, on this
modify
command, if we provide a Label ID that does not exist, we will receive an error: