http://stackoverflow.com/questions/34801292/how-to-add-a-line-to-a-google-spreadsheet-via-email
Last active
July 24, 2018 07:34
-
-
Save oshliaer/ea6597e8c7b48ffe695c to your computer and use it in GitHub Desktop.
Gmail checker
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
/* | |
* 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