我有一个代码可以捕获电子邮件并放入电子表格并将此电子邮件放在其他标签中,但是我的工作范围在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;
}
答案 0 :(得分:1)
我不知道我是否正确地提出了您的问题,但是您正在处理线程,并且线程可能由许多电子邮件消息组成,因此这可能是您获得不同数量的消息的原因
答案 1 :(得分: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;
}
答案 2 :(得分:0)
作为这两个答案的补充,这里有一个脚本,可以用一种不那么“微不足道”的方式处理HTML到文本的转换。
我使用了来自a post by 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;
}