replace the filename of transferred file from gmail to gdrive with filenameDATE

97 views Asked by At

I have script that can transfer email from gmail to gdrive and my plan is replace the filename of the email attachment to assigned filename with current date ddmmyy (Ex. project012322.xlsx) or .csv when the file is transferred to gdrive.

May I know what I need to modify or add with my script?

  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];
    });
  });
1

There are 1 answers

0
ziganotschka On
  • Your script contains the line Drive.Files.insert({ title: attachment.getName(), mimeType: attachment.getContentType(), parents: [{ id: folderId }] }, attachment.copyBlob()); with title: attachment.getName()
  • This means that you are creating a file on your Drive with the title that is exactly the same like the original name of the attachment
  • If instead you would like to give the file an assigned name plus the current date - you need to define the assigned name and get the current timestamp For this, you can replace the line above by the following three lines:
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()