Created
April 9, 2018 17:02
-
-
Save netinhoteixeira/77d9bab545b0acee58ea877d612ee481 to your computer and use it in GitHub Desktop.
Gmail Huge Attachments
This file contains 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
/************************************************************* | |
* Gmail Huge Attachments 0.1 - Francisco Ernesto Teixeira | |
* forked from Gmail Size 2.1 - Digital Inspiration | |
* ----------------------------------------------------------- | |
* | |
* Tutorial : https://francisco.pro | |
* Contact : [email protected] | |
* | |
* Original | |
* Tutorial : http://labnol.org/?p=21191 | |
* Contact : [email protected] | |
* Twitter : @labnol | |
* | |
* Change Log: | |
* 1. Reduced the message batch count to prevent timeouts; | |
* 2. Added Sleep to avoid hitting the quota early; | |
* 3. Send log to e-mail, verify if it is already finished, | |
* you can force to retry to scan from scratch. | |
* | |
* Dependencies: | |
* | |
* Library: ContinuousBatchLibrary | |
* Key: 1nwsTO5nEWTPQ9fQocPYumFQqH5VzxR__JRdsTDKCMFaODpMPVzV9n0Du | |
* Reference: http://patt0.blogspot.com.br/2014/01/using-scriptapp-to-process-batches-in.html | |
* | |
************************************************************/ | |
function onOpen() { | |
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var menu = [ | |
{ | |
name: 'Scan mailbox', | |
functionName: 'scanMailbox_' | |
}, | |
{ | |
name: 'Force scan mailbox from start', | |
functionName: 'forceScanMailboxFromStart_' | |
} | |
]; | |
activeSpreadsheet.addMenu('Gmail Sort', menu); | |
activeSpreadsheet.toast('Click the menu Complements > Gmail Sort > Scan Mailbox to continue..'); | |
} | |
/** | |
* @OnlyCurrentDoc | |
*/ | |
function scanMailbox_(threads) { | |
Logger.log('Searching...'); | |
var functionName = 'scanMailbox_'; | |
// DONE: 2018-04-09 Fix to "Exceeded maximum execution time" | |
ContinuousBatchLibrary.startOrResumeContinousExecutionInstance(functionName); | |
var continuousBatchKey = ContinuousBatchLibrary.getBatchKey(functionName) || 0; | |
var sheet = SpreadsheetApp.getActiveSheet(); | |
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var now = new Date(); | |
var filter = 'larger:1m has:attachment'; | |
var start = getStartIndex_(); | |
var limit = 200; | |
Logger.log('Filter: ' + filter); | |
Logger.log('Start: ' + start); | |
Logger.log('Limit: ' + limit); | |
var finished = false; | |
if (!threads) { | |
Logger.log('First iteration...'); | |
threads = searchForHugeEmails_(filter, start, limit); | |
} | |
// If do not exists e-mail to scan | |
if (threads.length === 0) { | |
finished = notifyFinished_(); | |
} else { | |
// Else | |
var count = 0; | |
for (var i = 0; i < threads.length - 1; i++) { | |
// DONE: 2018-04-09 Fix to "Exceeded maximum execution time" | |
if (ContinuousBatchLibrary.isTimeRunningOut(functionName)) { | |
ContinuousBatchLibrary.setBatchKey(functionName, continuousBatchKey++); | |
break; | |
} | |
var messages = threads[i].getMessages(); | |
var subject = threads[i].getFirstMessageSubject(); | |
count++; | |
activeSpreadsheet.toast('Processing ' + subject); | |
var size = getSize_(messages); | |
if (size) { | |
sheet.appendRow([ | |
Utilities.formatDate(messages[0].getDate(), 'GMT', 'yyyy-MM-dd'), | |
messages[0].getFrom(), | |
subject, | |
size, | |
'=HYPERLINK("' + threads[i].getPermalink() + '"; "View")' | |
]); | |
} | |
setStartIndex_(start + i); | |
Utilities.sleep(2000); | |
if (isTimeUp_(now)) { | |
break; | |
} | |
} | |
// Update start index | |
start = getStartIndex_() + 1; | |
setStartIndex_(start); | |
// Search for e-mails again | |
Logger.log('Next iteration...'); | |
threads = searchForHugeEmails_(filter, start, limit); | |
if (threads.length > 0) { | |
scanMailbox_(threads); | |
} else { | |
finished = notifyFinished_(); | |
} | |
} | |
if (finished) { | |
sendLogToEmail_(); | |
ContinuousBatchLibrary.endContinuousExecutionInstance(functionName, | |
Session.getActiveUser().getEmail(), 'Scan Mailbox'); | |
} | |
} | |
function forceScanMailboxFromStart_() { | |
setStartIndex_(0); | |
scanMailbox_(); | |
} | |
/** | |
* Search for huge emails. | |
* | |
* @param {*} filter Filter to be used to fetch e-mails. | |
* @param {*} start Index of start. | |
* @param {*} limit Limit to e-mails retrieved. | |
* @returns Messages. | |
*/ | |
function searchForHugeEmails_(filter, start, limit) { | |
var threads = GmailApp.search(filter, start, limit); | |
Logger.log('Found: ' + threads.length); | |
return threads; | |
} | |
/** | |
* Get the size of messages (in MB) in e-mail. | |
* | |
* @param {*} messages Messages in e-mail. | |
* @returns Total size of messages in e-mail. | |
*/ | |
function getSize_(messages) { | |
var size = 0; | |
for (var m in messages) { | |
var att = messages[m].getAttachments(); | |
for (var a in att) { | |
size += att[a].getSize(); | |
} | |
} | |
return Math.round(size * 100 / (1024 * 1024)) / 100; | |
} | |
/** | |
* Get index of current scan. | |
*/ | |
function getStartIndex_() { | |
// FIX: 2018-04-09 Call many times properties | |
Utilities.sleep(2000); | |
return parseInt(PropertiesService.getDocumentProperties().getProperty('GMAILSTART') || 0); | |
} | |
/** | |
* Set index of current scan. | |
*/ | |
function setStartIndex_(i) { | |
// FIX: 2018-04-09 Call many times properties | |
Utilities.sleep(2000); | |
return PropertiesService.getDocumentProperties().setProperty('GMAILSTART', i); | |
} | |
function isTimeUp_(start) { | |
var now = new Date(); | |
return (now.getTime() - start.getTime()) > 4.5 * 60 * 1000; | |
} | |
function notifyFinished_() { | |
var message = 'All messages have been processed.'; | |
Browser.msgBox(message); | |
Logger.log(message); | |
return true; | |
} | |
function sendLogToEmail_() { | |
var recipient = Session.getActiveUser().getEmail(); | |
var subject = 'Log of Scan Mailbox'; | |
var body = Logger.getLog(); | |
MailApp.sendEmail(recipient, subject, body); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment