英文:
How can I prevent duplicate Emails from being posted into the Spreadsheet
问题
function grabDover() {
const dover = GmailApp.search('has:attachment label:"1st Choice Auto Dover Scans"');
const doverSheet = ss.getSheetByName('1st Choice Auto Dover Scans');
dover.forEach(thread => {
const messages = thread.getMessages();
messages.forEach(message => {
const messageid = message.getId();
const attachments = message.getAttachments();
const date = message.getDate();
const subject = message.getSubject();
const url = 'https://mail.google.com/mail/u/0/#label/' + messageid;
attachments.forEach(attachment => {
Logger.log(date);
const array = [attachment.getName(), date, url, messageid];
doverSheet.appendRow(array);
});
});
});
}
我有一个从Gmail中提取邮件附件的脚本,基于标签名称和即将到来的时间触发器,但我无法弄清楚如何防止重复的邮件附件被提取。
我试图让脚本交叉检查从Gmail中提取到表格中的messageId,以便不会将重复的邮件发布到表格中。
我已经将messageid存储在一个数组中。我尝试使用scriptproperties方法,但它没有起作用,或者我操作不正确。
我可以通过Google表格公式使其工作,但对于我的确切用例来说,这种方法行不通。
英文:
function grabDover() {
const dover = GmailApp.search('has:attachment label:"1st Choice Auto Dover Scans"');
const doverSheet = ss.getSheetByName('1st Choice Auto Dover Scans');
dover.forEach(thread => {
const messages = thread.getMessages();
messages.forEach(message => {
const messageid = message.getId();
const attachments = message.getAttachments();
const date = message.getDate();
const subject = message.getSubject();
const url = 'https://mail.google.com/mail/u/0/#label/' + messageid;
attachments.forEach(attachment => {
Logger.log(date);
const array = [attachment.getName(), date, url, messageid];
doverSheet.appendRow(array);
});
});
});
}
I have this script that pulls email attachments from Gmail, based on label names and soon to be a time trigger, and I can not figure out how to prevent duplicate email attachments from being pulled.
I am trying to have the script, cross check the messageIds that are being pulled from Gmail into the sheet, so no duplicate emails are being posted into the sheet.
I have the messageid in a array already. I have tried doing it with a scriptproperties method and it did not work or I was doing it wrong.
I can get it to work via google spread sheet formula but It wont work for my exact use case.
答案1
得分: 0
我相信你的目标如下。
- 你想从
const dover = GmailApp.search('has:attachment label:"1st Choice Auto Dover Scans"');
中获取值。 - 当你从
dover
中检索值时,你想检查消息ID,并且只在找不到消息ID时放入值。
修改要点:
-
在你的脚本中,使用了
appendRow
循环。在这种情况下,处理成本会很高。参考(作者:我) -
没有检查消息ID。
当这些要点反映在你的脚本中时,以下是修改建议:
修改后的脚本:
function grabDover() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const dover = GmailApp.search('has:attachment label:"1st Choice Auto Dover Scans"');
const doverSheet = ss.getSheetByName('1st Choice Auto Dover Scans');
const lastRow = doverSheet.getLastRow();
const currentMessageIds = [...new Set(doverSheet.getDataRange().getValues().map(([, , , d]) => d))];
const values = dover.reduce((ar, thread) => {
thread.getMessages().forEach(message => {
const messageid = message.getId();
if (!currentMessageIds.includes(messageid)) {
const attachments = message.getAttachments();
const date = message.getDate();
Logger.log(date);
const url = 'https://mail.google.com/mail/u/0/#label/' + messageid;
ar = [...ar, ...attachments.map(attachment => [attachment.getName(), date, url, messageid])];
}
});
return ar;
}, []);
if (values.length == 0) return;
doverSheet.getRange(lastRow + 1, 1, values.length, values[0].length).setValues(values);
}
- 运行此脚本时,当
dover
中的消息ID在当前表格 "1st Choice Auto Dover Scans" 中找不到时,将值放入表格中。
注意:
- 在这个修改后的脚本中,假设从你的脚本中,消息ID放在列 "D" 中。请注意这一点。
参考资料:
英文:
I believe your goal is as follows.
- You want to put the values from
const dover = GmailApp.search('has:attachment label:"1st Choice Auto Dover Scans"');
. - When you retrieve the values from
dover
, you want to check the message IDs and want to put only the values when the message IDs are not found.
Modification points:
-
In your script,
appendRow
is used in a loop. In this case, the process cost becomes high. Ref (Author: me) -
Message IDs are not checked.
When these points are reflected in your script, how about the following modification?
Modified script:
function grabDover() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const dover = GmailApp.search('has:attachment label:"1st Choice Auto Dover Scans"');
const doverSheet = ss.getSheetByName('1st Choice Auto Dover Scans');
const lastRow = doverSheet.getLastRow();
const currentMessageIds = [...new Set(doverSheet.getDataRange().getValues().map(([, , , d]) => d))];
const values = dover.reduce((ar, thread) => {
thread.getMessages().forEach(message => {
const messageid = message.getId();
if (!currentMessageIds.includes(messageid)) {
const attachments = message.getAttachments();
const date = message.getDate();
Logger.log(date);
const url = 'https://mail.google.com/mail/u/0/#label/' + messageid;
ar = [...ar, ...attachments.map(attachment => [attachment.getName(), date, url, messageid])];
}
});
return ar;
}, []);
if (values.length == 0) return;
doverSheet.getRange(lastRow + 1, 1, values.length, values[0].length).setValues(values);
}
- When this script is run, when the message IDs in
dover
are not found in the current sheet "1st Choice Auto Dover Scans", the values are put into the sheet.
Note:
- In this modified script, it supposes that from your script, the message IDs are put into column "D". Please be careful about this.
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论