基本上,我要做的是把收到的电子邮件中的所有附件放到google的一个文件夹中(大部分是.PDF)。但它说,我不能超过500个附加的文件与搜索功能,我必须使用一个叫做pageToken的东西,我不知道如何应用到我的代码。因此,我需要一些建议或指导,或者一些例子来做这件事。
function saveGmailtoGoogleDrive() {
const folderId = '1apaQJjDSK-bNfd3ZgiFqK23cE7SCPqoB'; //Google Drive Folder
const searchQuery = 'label:unread has:attachment'; //Filter
const threads = GmailApp.search(searchQuery, 0, 500);
threads.forEach(thread => {
const messages = thread.getMessages();
messages.forEach(message => {
const attachments = message.getAttachments({
includeInlineImages: false,
includeAttachments: true
});
attachments.forEach(attachment => {
// Insert the attachment to google drive folder
Drive.Files.insert(
{
title: attachment.getName(),
mimeType: attachment.getContentType(),
parents: [{ id: folderId }]
},
attachment.copyBlob()
);
});
});
});
};
function saveGmailtoGoogleDrive() {
const folderId = '1apaQJjDSK-bNfd3ZgiFqK23cE7SCPqoB'; //Google Drive Folder
const searchQuery = 'label:unread has:attachment'; //Filter
const threads = GmailApp.search(searchQuery, 0, 500);
threads.forEach(thread => {
const messages = thread.getMessages();
messages.forEach(message => {
const attachments = message.getAttachments({
includeInlineImages: false,
includeAttachments: true
});
attachments.forEach(attachment => {
// Insert the attachment to google drive folder
Drive.Files.insert(
{
title: attachment.getName(),
mimeType: attachment.getContentType(),
parents: [{ id: folderId }]
},
attachment.copyBlob()
);
});
});
});
};
发布于 2021-12-16 01:04:38
search(query, start, max)方法的参数是query, start, max。max的当前最大值是500。当此值结束时,会发生像Argument max cannot exceed 500.这样的错误。start是搜索的起始位置。所以我想这可以用来实现你的目标。当这反映在您的脚本中时,如下所示。
修改脚本:
发自:
const threads = GmailApp.search(searchQuery, 0, 500);至:
let [start, end] = [0, 500];
let threads = [];
do {
const t = GmailApp.search(searchQuery, start, end);
start += end;
threads = [...threads, ...t];
} while (threads.length == start);threads中检索超过500封电子邮件。参考资料:
https://stackoverflow.com/questions/70369249
复制相似问题