Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Apps Script para enviar dados de email do Gmail para o Google Spreadsheets.
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