Created
March 18, 2015 20:36
-
-
Save nkgokul/1350728dde7d882aafd9 to your computer and use it in GitHub Desktop.
Import Mails that have particular label
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Retrieves all the rows in the active spreadsheet that contain data and logs the | |
* values for each row. | |
* For more information on using the Spreadsheet API, see | |
* https://developers.google.com/apps-script/service_spreadsheet | |
*/ | |
function readRows() { | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var rows = sheet.getDataRange(); | |
var numRows = rows.getNumRows(); | |
var values = rows.getValues(); | |
for (var i = 0; i <= numRows - 1; i++) { | |
var row = values[i]; | |
Logger.log(row); | |
} | |
}; | |
/** | |
* Adds a custom menu to the active spreadsheet, containing a single menu item | |
* for invoking the readRows() function specified above. | |
* The onOpen() function, when defined, is automatically invoked whenever the | |
* spreadsheet is opened. | |
* For more information on using the Spreadsheet API, see | |
* https://developers.google.com/apps-script/service_spreadsheet | |
*/ | |
function onOpen(e) { | |
SpreadsheetApp.getUi().createAddonMenu() // Or DocumentApp. | |
.addItem('Import from a Label', 'getMailsForALabel') | |
.addToUi(); | |
} | |
function getMailsForALabel(labelToFetchMailsFor) { | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var cursorPosition = sheet.getActiveCell(); | |
var row = cursorPosition.getRow(); | |
var column = cursorPosition.getColumn(); | |
//sheet.toast("Importing mails form the thread " + labelToFetchMailsFor); | |
//var labelToFetchMailsFor = typeof labelToFetchMailsFor !== 'undefined' ? labelToFetchMailsFor : 'mailmerge'; | |
var labelToFetchMailsFor = Browser.inputBox("Enter the label you want to import from"); | |
var selectedLabel = GmailApp.getUserLabelByName(labelToFetchMailsFor); | |
var threadsInLabel = selectedLabel.getThreads(); | |
var messagesArray = []; | |
threadsInLabel.forEach(function(thread) { | |
thread.getMessages().forEach(function(message){ | |
Logger.log(message.getPlainBody()); | |
messagesArray.push([message.getPlainBody()]); | |
}); | |
}); | |
var destinationRange = sheet.getRange(row, column, messagesArray.length, 1); | |
destinationRange.setValues(messagesArray); | |
//sheet.toast("Finished importing mails from the label " + labelToFetchMailsFor); | |
} | |
function onInstall(e) { | |
onOpen(e); | |
// Perform additional setup as needed. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment