Função base + Duas funções acessórias

 avatar
cordeirogg
javascript
2 years ago
1.4 kB
3
Indexable
Never
function getMailClean() {
  // Setting variables
  var spreadsheet = SpreadsheetApp.openById('CODIGO_DA_PLANILHA');
  var sheetName = "getMailClean"
  var start = 2499
  var max = 500
  count = 0

  while (true) {
    // Getting messages
    var messagesDataArray = getMessages(start, max, count);
    if (messagesDataArray == "No thread found") break;
    start += max;
    count++;

    // Accessing spreadsheet
    var spreadsheetRange = getSpreadsheetRange(spreadsheet, sheetName, messagesDataArray);

    // Setting values
    spreadsheetRange.setValues(messagesDataArray);
    Logger.log(new Date);
  }
}


function getMessages(start, max, count) {
  var threads = GmailApp.search('category:promotions', start, max);
  if (threads.length == 0) return "No threads found";

  var messages = GmailApp.getMessagesForThreads(threads);

  var messagesDataArray = [];
  for (var i = 0; i < threads.length; i++) {
    messagesDataArray.push([
      count,
      new Date(),
      messages[i][0].getDate(),
      messages[i][0].getSubject(),
      messages[i][0].getFrom(),
      messages[i][0].getReplyTo()
    ]);
  }
  return messagesDataArray
}


function getSpreadsheetRange(spreadsheet, sheetName, messages) {
  var sheet = spreadsheet.getSheetByName(sheetName);

  var range = sheet.getRange(sheet.getLastRow() + 1, 1, messages.length, messages[0].length)
  return range
}