1

我有一个代码可以捕获电子邮件并将其放入电子表格并将此电子邮件放入其他标签中,它可以正常工作,但我将范围设置在 0 到 50 之间,当我查看电子邮件出现超过 50 封电子邮件的标签时,为什么这发生了?

谢谢

代码

function get_emails() {  
  var sheet = SpreadsheetApp.getActiveSheet();

  var label_pending = GmailApp.getUserLabelByName('Fale Conosco/Contato');  
  var label_done = GmailApp.getUserLabelByName('Teste Done');

  var threads = label_pending.getThreads(0, 50); 

  var index = getColIndexByName('Conteúdo');

  for (var t in threads) {

    var thread = threads[t];

    var html = thread.getMessages()[0].getBody();
    html=html.replace(/<\/div>/ig, '\n');
    html=html.replace(/<\/li>/ig, '\n');
    html=html.replace(/<li>/ig, '  *');
    html=html.replace(/<\/ul>/ig, '\n');
    html=html.replace(/<\/p>/ig, '\n');
    html=html.replace(/<br\/?>/ig, '\n');
    html=html.replace(/<[^>]+>/ig, '');

    sheet.getRange((sheet.getLastRow() + 1), index).setValue(html);

    thread.removeLabel(label_pending);  
    thread.addLabel(label_done);
  }
  return 0;
}  

function getColIndexByName(colName) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numColumns = sheet.getLastColumn();
  var row = sheet.getRange(1, 1, 1, numColumns).getValues();
  for (i in row[0]) {
    var name = row[0][i];
    if (name == colName) {
      return parseInt(i) + 1;
    }
  }
  return -1;
}
4

3 回答 3

1

我不知道我是否正确地回答了您的问题,但是您正在处理线程,并且线程可能由许多电子邮件组成,因此这可能是您收到不同数量的消息的原因

于 2012-12-20T15:50:31.117 回答
0

我现在修复了它的工作,我添加了一个循环来逐条查看消息

function processPending() {  
  var sheet = SpreadsheetApp.getActiveSheet();

  var label_pending = GmailApp.getUserLabelByName('Fale Conosco/Contato');  
  var label_done = GmailApp.getUserLabelByName('Teste Done');

  var threads = label_pending.getThreads(0, 5);

  var index = getColIndexByName('Conteúdo');

  for (var t in threads) {

    var thread = threads[t];

    var messaqnt = thread.getMessages();

    for (var i in messaqnt) {

      var html = messaqnt[i].getBody();
      html=html.replace(/<\/div>/ig, '\n');
      html=html.replace(/<\/li>/ig, '\n');
      html=html.replace(/<li>/ig, '  *');
      html=html.replace(/<\/ul>/ig, '\n');
      html=html.replace(/<\/p>/ig, '\n');
      html=html.replace(/<br\/?>/ig, '\n');
      html=html.replace(/<[^>]+>/ig, '');

      sheet.getRange((sheet.getLastRow() + 1), index).setValue(html);

      thread.removeLabel(label_pending);  
      thread.addLabel(label_done);
    }
  }
  return 0;
}  

function getColIndexByName(colName) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numColumns = sheet.getLastColumn();
  var row = sheet.getRange(1, 1, 1, numColumns).getValues();
  for (i in row[0]) {
    var name = row[0][i];
    if (name == colName) {
      return parseInt(i) + 1;
    }
  }
  return -1;
}
于 2012-12-20T16:59:49.740 回答
0

作为对这两个答案的补充,这里有一个脚本,它以一种不那么“琐碎”的方式处理 HTML 到文本的转换。

我使用了Corey G 的帖子中的代码,该代码具有优势(对我而言)可以在法语中获取重音字符......(这是我的用例 ;-)

function get_emails() {  
  var sheet = SpreadsheetApp.getActiveSheet();
  var label_pending = GmailApp.getUserLabelByName('NoLabel');  

  var threads = label_pending.getThreads(0, 10); 
  var index = getColIndexByName('test');

  for (var t in threads) {
    var thread = threads[t];
    var msgcount = thread.getMessageCount()

   for(m=0;m<msgcount;++m){

  var html = thread.getMessages()[m].getBody();
  var txt = getTextFromNode(Xml.parse(html, true).getElement());
  sheet.getRange((sheet.getLastRow() + 1), index).setValue(txt);
}
  }
  return 0;
}  


function getTextFromNode(x) {
  switch(x.toString()) {
    case 'XmlText': return x.toXmlString();
    case 'XmlElement': return x.getNodes().map(getTextFromNode).join('');
    default: return '';
  }
}

function getColIndexByName(colName) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numColumns = sheet.getLastColumn();
  var row = sheet.getRange(1, 1, 1, numColumns).getValues();
  for (i in row[0]) {
    var name = row[0][i];
    if (name == colName) {
      return parseInt(i) + 1;
    }
  }
  return -1;
  }
于 2012-12-21T11:52:04.617 回答