Created
August 12, 2020 14:09
-
-
Save arthuralvim/bcc951ebd5fa4b7abf1c26d3e2b6ac8b to your computer and use it in GitHub Desktop.
Google Apps Script para enviar dados de email do Gmail para o Google Spreadsheets.
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
function count_emails() { | |
var max = 500; | |
var offset = 0; | |
var emails = []; | |
while (true) { | |
var threads = GmailApp.search("in:anywhere", offset, max); | |
emails = searchThreads.concat(threads); | |
if (threads.length < max) { | |
Logger.log("Contagem:", emails.length); | |
break; | |
} | |
offset += max; | |
} | |
} | |
function extractDetails(message){ | |
var dateTime = message.getDate(); | |
var subjectText = message.getSubject(); | |
var senderDetails = message.getFrom(); | |
var bodyContents = message.getPlainBody(); | |
return { | |
'data': dateTime, | |
'assunto': subjectText, | |
'de': senderDetails | |
} | |
} | |
// after:2020/01/01 | |
// before:2020/01/01 | |
// category:principal | |
// category:social | |
// category:promoções | |
// category:atualizações | |
// category:fóruns | |
// category:reservas | |
// category:compras | |
function main_loop() { | |
var max = 500; | |
var offset = 0; | |
var emails = []; | |
var all_threads = []; | |
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet = spreadsheet.getSheetByName('Página1'); | |
sheet.clear(); | |
sheet.appendRow(['Email Address']); | |
while (true) { | |
var threads = GmailApp.search("in:anywhere", offset, max); | |
all_threads = all_threads.concat(threads); | |
Logger.log("offset:", offset, " max:", max); | |
if (threads.length < max) { | |
for (var i = 0; i < all_threads.length; i++) { | |
var messages = all_threads[i].getMessages(); | |
Logger.log("thread: ", i, " mensagens:", messages.length); | |
for (var x = 0; x < messages.length; x++) { | |
var sender = messages[x].getFrom(); | |
emails.push([sender]); | |
} | |
} | |
emails.sort(); | |
sheet.getRange(sheet.getLastRow()+1, 1, emails.length,1).setValues(emails); | |
Logger.log("Contagem:", emails.length); | |
break; | |
} | |
offset += max; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment