Google 已经获取电子邮件信息但需要附件名称的脚本
Google script that already fetches email infos but needs attachment name
上下文
我正在使用脚本来获取我在此处找到的电子邮件(来自 Niclas, I think):
我已经根据自己的需要对其进行了调整,效果非常好!
我测试了什么
- 我从 Google 脚本 Class 附件中看到了 getDescription(),但无法让它工作,我什至不确定这是否可行
我想要什么
- 此外,我想获取附件文件名,因为它是每封电子邮件的独特标记
- 非常感谢任何帮助。提前致谢
修改:
对您在问题中提到的 的答案进行以下修改:
添加这些行:
var attachments = messages[maxIndex].getAttachments();
var attNames = attachments.map(att=>att.getName());
并修改这个:
ss.appendRow([from, cc, time, sub,...attNames ,'https://mail.google.com/mail/u/0/#inbox/'+mId])
解决方案:
function myFunction() {
// Use sheet
var ss = SpreadsheetApp.getActiveSheet();
// Gmail query
var query = "label:support -label:trash -label:support-done -from:me";
// Search in Gmail, bind to array
var threads = GmailApp.search(query);
// Loop through query results
for (var i = 0; i < threads.length; i++)
{
// Get messages in thread, add to array
var messages = threads[i].getMessages();
// Used to find max index in array
var max = messages[0];
var maxIndex = 0;
// Loop through array to find maxIndexD = most recent mail
for (var j = 0; j < messages.length; j++) {
if (messages[j] > max) {
maxIndex = j;
max = messages[j];
}
}
// Find data
var mId = messages[maxIndex].getId() // ID used to create mail link
var from = messages[maxIndex].getFrom();
var cc = messages[maxIndex].getCc();
var time = threads[i].getLastMessageDate()
var sub = messages[maxIndex].getSubject();
var attachments = messages[maxIndex].getAttachments();
var attNames = attachments.map(att=>att.getName());
// Write data to sheet
ss.appendRow([from, cc, time, sub,...attNames ,'https://mail.google.com/mail/u/0/#inbox/'+mId])
}
}
不要忘记根据您的需要更改 query
的值。
参考文献:
您必须 enable V8 运行时才能使用代码段。
上下文
我正在使用脚本来获取我在此处找到的电子邮件(来自 Niclas, I think):
我已经根据自己的需要对其进行了调整,效果非常好!
我测试了什么
- 我从 Google 脚本 Class 附件中看到了 getDescription(),但无法让它工作,我什至不确定这是否可行
我想要什么
- 此外,我想获取附件文件名,因为它是每封电子邮件的独特标记
- 非常感谢任何帮助。提前致谢
修改:
对您在问题中提到的
添加这些行:
var attachments = messages[maxIndex].getAttachments(); var attNames = attachments.map(att=>att.getName());
并修改这个:
ss.appendRow([from, cc, time, sub,...attNames ,'https://mail.google.com/mail/u/0/#inbox/'+mId])
解决方案:
function myFunction() {
// Use sheet
var ss = SpreadsheetApp.getActiveSheet();
// Gmail query
var query = "label:support -label:trash -label:support-done -from:me";
// Search in Gmail, bind to array
var threads = GmailApp.search(query);
// Loop through query results
for (var i = 0; i < threads.length; i++)
{
// Get messages in thread, add to array
var messages = threads[i].getMessages();
// Used to find max index in array
var max = messages[0];
var maxIndex = 0;
// Loop through array to find maxIndexD = most recent mail
for (var j = 0; j < messages.length; j++) {
if (messages[j] > max) {
maxIndex = j;
max = messages[j];
}
}
// Find data
var mId = messages[maxIndex].getId() // ID used to create mail link
var from = messages[maxIndex].getFrom();
var cc = messages[maxIndex].getCc();
var time = threads[i].getLastMessageDate()
var sub = messages[maxIndex].getSubject();
var attachments = messages[maxIndex].getAttachments();
var attNames = attachments.map(att=>att.getName());
// Write data to sheet
ss.appendRow([from, cc, time, sub,...attNames ,'https://mail.google.com/mail/u/0/#inbox/'+mId])
}
}
不要忘记根据您的需要更改 query
的值。
参考文献:
您必须 enable V8 运行时才能使用代码段。