Skip to content

Instantly share code, notes, and snippets.

@oshliaer
Last active July 24, 2018 07:34
Show Gist options
  • Save oshliaer/ea6597e8c7b48ffe695c to your computer and use it in GitHub Desktop.
Save oshliaer/ea6597e8c7b48ffe695c to your computer and use it in GitHub Desktop.
Gmail checker
/*
* global properties
*/
var properties = {
targetSheetId: 'abcDEF123',
queryString: 'to:[email protected] is:starred',
handlerFunctionName: 'checkMail'
};
/*
* On/Off checker
*/
function switchTrigger(){
var isExist = false;
var triggers = ScriptApp.getProjectTriggers();
for(var i = 0; i < triggers.length; i++){
if(triggers[i].getHandlerFunction() == properties.handlerFunctionName && triggers[i].getEventType() == ScriptApp.EventType.CLOCK){
isExist = true;
ScriptApp.deleteTrigger(triggers[i]);
Logger.log('delete');
}
}
if(!isExist){
ScriptApp.newTrigger(properties.handlerFunctionName).timeBased().everyMinutes(1).create();
Logger.log('create');
}
}
/*
* The checker
*/
function checkMail(){
var sh = SpreadsheetApp.openById(properties.targetSheetId).getSheets()[0];
var query = properties.queryString;
var threads = GmailApp.search(query);
if(threads.length < 1) return;
for(var i = 0; i < threads.length; i++){
var messages = threads[i].getMessages();
for(var j = 0; j < messages.length; j++){
if(messages[j].isStarred()){
sh.appendRow([
new Date(),
messages[j].getFrom(),
messages[j].getPlainBody()
]);
messages[j].unstar();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment