Save Gmail Attachment to Google Drive by Label, then Change the Label Name - Stack Overflow

admin2025-04-17  3

I've looked through several of the similar questions that have been asked, but I just can't get it to work for what I am doing. I need to get attachments from specific labels, move them to a google drive location, then change the label on those emails so they're 'complete' to prevent the attachments being moved to the folder again. I'll be using python to download the attachment (csv) to my desktop for the rest of the process which is all fine. It's just this part I can't get to work.

I've tried two versions of the code, here's the first one:

function saveAttachment(){
var folder = DriveApp.getFolderById('FOLDERID');
var workLabel = GmailApp.getUserLabelByName("_Projects/SUBLABEL/SUBLABEL2")
var doneLabel = GmailApp.getUserLabelByName("_Projects/SUBLABEL/SUBLABEL2 Completed")
var threads = GmailApp.search('from:[email protected]');
var attachments = [];
threads.forEach(thread => {
  thread.getMessages().forEach(message => {
    message.getAttachments().forEach(attachment => {
      Logger.log(attachment);
      var file = folder.createFile(attachment.copyBlob().setName(attachment.getName()));
      attachments.push(attachment.getName());
      })
    })
  
     
  })  
  Logger.log(attachments);
    //doneLabel.addToThread(threads[i]);
    //workLabel.removeFromThread(threads[i])
}

This actually works, moves what I need to where I need it. But I can't figure out how to change the label once it's done.

A coworker then gave me this to modify, but I can't get it to work AT ALL - I am getting the error TypeError: attachments.copyBlob is not a function referencing the line I ATTEMPTED to bold below, it's the one with two asterisks at the beginning and two at the end:

function saveAttachment(){
var folder = DriveApp.getFolderById('FOLDER ID');
var workLabel = GmailApp.getUserLabelByName("_Projects/SUBLABEL/SUBLABEL2");
var doneLabel = GmailApp.getUserLabelByName("_Projects/SUBLABEL/SUBLABEL2 Completed")   
var threads = workLabel.getThreads();  
var msgs = GmailApp.getMessagesForThreads(threads);
  for (var i = 0 ; i < msgs.length; i++) {
    for (var j = 0; j < msgs[i].length; j++) {
      var attachments = msgs[i][j].getAttachments();
      for (var k = 0; k < attachments.length; k++) {
        var FileName = attachments[k].getName();
        attachments: attachments[k];
      }
        **var file = folder.createFile(attachments.copyBlob().setName(attachments.getName()));**
      attachments.push(attachments.getName());
    }
    doneLabel.addToThread(threads[i]);
    workLabel.removeFromThread(threads[i]);       
  }
}

I have no actual training on appscript or any coding at all, I'm literally just a code thief - my employer won't pay for the people who actually know what they're doing. I'm hoping it's something simple since I'm just a dum-dum here. I appreciate any feedback, thank you!

I've looked through several of the similar questions that have been asked, but I just can't get it to work for what I am doing. I need to get attachments from specific labels, move them to a google drive location, then change the label on those emails so they're 'complete' to prevent the attachments being moved to the folder again. I'll be using python to download the attachment (csv) to my desktop for the rest of the process which is all fine. It's just this part I can't get to work.

I've tried two versions of the code, here's the first one:

function saveAttachment(){
var folder = DriveApp.getFolderById('FOLDERID');
var workLabel = GmailApp.getUserLabelByName("_Projects/SUBLABEL/SUBLABEL2")
var doneLabel = GmailApp.getUserLabelByName("_Projects/SUBLABEL/SUBLABEL2 Completed")
var threads = GmailApp.search('from:[email protected]');
var attachments = [];
threads.forEach(thread => {
  thread.getMessages().forEach(message => {
    message.getAttachments().forEach(attachment => {
      Logger.log(attachment);
      var file = folder.createFile(attachment.copyBlob().setName(attachment.getName()));
      attachments.push(attachment.getName());
      })
    })
  
     
  })  
  Logger.log(attachments);
    //doneLabel.addToThread(threads[i]);
    //workLabel.removeFromThread(threads[i])
}

This actually works, moves what I need to where I need it. But I can't figure out how to change the label once it's done.

A coworker then gave me this to modify, but I can't get it to work AT ALL - I am getting the error TypeError: attachments.copyBlob is not a function referencing the line I ATTEMPTED to bold below, it's the one with two asterisks at the beginning and two at the end:

function saveAttachment(){
var folder = DriveApp.getFolderById('FOLDER ID');
var workLabel = GmailApp.getUserLabelByName("_Projects/SUBLABEL/SUBLABEL2");
var doneLabel = GmailApp.getUserLabelByName("_Projects/SUBLABEL/SUBLABEL2 Completed")   
var threads = workLabel.getThreads();  
var msgs = GmailApp.getMessagesForThreads(threads);
  for (var i = 0 ; i < msgs.length; i++) {
    for (var j = 0; j < msgs[i].length; j++) {
      var attachments = msgs[i][j].getAttachments();
      for (var k = 0; k < attachments.length; k++) {
        var FileName = attachments[k].getName();
        attachments: attachments[k];
      }
        **var file = folder.createFile(attachments.copyBlob().setName(attachments.getName()));**
      attachments.push(attachments.getName());
    }
    doneLabel.addToThread(threads[i]);
    workLabel.removeFromThread(threads[i]);       
  }
}

I have no actual training on appscript or any coding at all, I'm literally just a code thief - my employer won't pay for the people who actually know what they're doing. I'm hoping it's something simple since I'm just a dum-dum here. I appreciate any feedback, thank you!

Share Improve this question asked Jan 31 at 7:16 A R WA R W 111 silver badge3 bronze badges 1
  • Can you explain what you mean by this then change the label on those emails so they're 'complete' to prevent the attachments being moved to the folder again. in greater detail . Explain what you mean by 'complete' – Cooper Commented Feb 4 at 17:48
Add a comment  | 

1 Answer 1

Reset to default 1

Move attachments to specific Google Drive folder and update the labels as intended

Inside the for (var k = 0; k < attachments.length; k++) loop, I added the attachment = attachments[k] to get the current attachment. Also Correcting the attachments.copyBlob() to attachment.copyBlob() this makes sure that you're copying the individual's attachement's blob, not the whole array. added logging for the statement to track saved attachment, so that you can check if that part is working

Modified Script

function saveAttachment(){
  var folder = DriveApp.getFolderById('FOLDER ID');
  var workLabel = GmailApp.getUserLabelByName("FromLabel");
  var doneLabel = GmailApp.getUserLabelByName("FromLabel - Completed");
  var threads = workLabel.getThreads();
  var msgs = GmailApp.getMessagesForThreads(threads);
  
  for (var i = 0; i < msgs.length; i++) {
    for (var j = 0; j < msgs[i].length; j++) {
      var attachments = msgs[i][j].getAttachments();
      
      for (var k = 0; k < attachments.length; k++) {
        var attachment = attachments[k];
        var file = folder.createFile(attachment.copyBlob().setName(attachment.getName()));
        Logger.log("Saved attachment: " + file.getName());
      }
    }

    // This is for the update labels
    doneLabel.addToThread(threads[i]);
    workLabel.removeFromThread(threads[i]);
  }
}

This script should now move attachments to your specified Google Drive folder and update the labels as intended.

Sample Output

  • FromLabel

  • Execution log

  • FromLabel - Completed

  • GDriveFolder

转载请注明原文地址:http://anycun.com/QandA/1744876772a88881.html