我有一个脚本,可以将电子邮件从gmail传输到gdrive,我的计划是在文件传输到gddrive时,将电子邮件附件的文件名替换为当前日期ddmmyy(例如project012322.xlsx(或.csv。
我可以知道我的脚本需要修改或添加什么吗?
const searchItem = "in:inbox subject:(My Project) has:attachment";
const threads = GmailApp.search(searchItem, 0, 100);
const ids = threads.flatMap((thread) => {
const messages = thread.getMessages();
return messages.map((message) => {
const id = message.getId();
if (!values.includes(id)) {
const attachments = message.getAttachments({ includeInlineImages: false, includeAttachments: true });
attachments.forEach((attachment) => {
Drive.Files.insert({ title: attachment.getName(), mimeType: attachment.getContentType(), parents: [{ id: folderId }] }, attachment.copyBlob());
});
}
return [id];
});
});
- 您的脚本包含以下行
Drive.Files.insert({ title: attachment.getName(), mimeType: attachment.getContentType(), parents: [{ id: folderId }] }, attachment.copyBlob());
与title: attachment.getName()
- 这意味着您正在驱动器上创建一个文件,其标题与附件的原始名称完全相同
- 如果您想给文件一个指定的名称加上当前日期,则需要定义指定的名称并获取当前时间戳为此,您可以将上面的行替换为以下三行:
var myName = "project";
var currentDate = new Date();
Drive.Files.insert({ title: myName + " " + currentDate, mimeType: attachment.getContentType(), parents: [{ id: folderId }] }, attachment.copyBlob()); ` with `title: attachment.getName()
- 使用new Date((检索当前时间戳
- 如果您想从
new Date()
检索到的时间戳开始,请使用Utilities.formatDate(date,timeZone,format(