First of all, I am not familiar with coding. I have a code that I use to pull certain information from Gmail based on the labels. Now the problem is, the code does not include a line to have the name of attachment. It would be really helpful if someone can write an additional code for that.

Below is the code that I use.. I tried adding the code var attachment = messages[j].getAttachments().getName(); to it, but doesn't work.

  var label = GmailApp.getUserLabelByName("NAME OF LABEL");
  var threads = label.getThreads();
  for (var i = threads.length - 1; i >=0; i--){
    var messages = threads[i].getMessages();
    for (var j = 0; j <messages.length; j++){
      var message = messages[j];

function extractDetails(message){
  var dateTime = message.getDate();
  var subjectText = message.getSubject();
  var senderDetails = message.getFrom();
  var bodyContents = message.getPlainBody();
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  activeSheet.appendRow([dateTime, senderDetails, subjectText, bodyContents]);
  • Hi, welcome to WebApps. You'd be familiar with the saying "Give a man a fish... teach a man to fish". getAttachments() returns ALL of the attachments (even if there is only one attachment) in an array. So you need to loop through each item in the array to get the attachment name(s). There's a good example of this in the documentation.
    – Tedinoz
    May 14, 2022 at 0:33
  • Hi, I tried implementing an additional line of code based on the developers page you shared. However, I am not getting the desired result out of it, reason being me so unfamiliar with the coding background. Would appreciate if you could help me with it.
    – Vishnu
    May 14, 2022 at 14:57

1 Answer 1


As noted in the comments, getAttachments() returns an array. So you need to loop through to get any/all of the attachment details.

  • for (var k = 0; k < attachments.length; k++) { is taken from the documentation.
  • var messagedetails=[] - this is a blank array created to accept message details (including attachment details).
  • messagedetails.push(attachments[k].getName(),attachments[k].getSize()) This line adds the attachment Name and Size to the message details
  • activeSheet.appendRow(messagedetails) - this line appends a new row. the details come from the "messagedetails" array.

function getmessagedetails(){
  //var label = GmailApp.getUserLabelByName("NAME OF LABEL");
  var label = GmailApp.getUserLabelByName("[email protected]/164538")
  var threads = label.getThreads()

  for (var i = threads.length - 1; i >=0; i--){
    var messages = threads[i].getMessages();
    for (var j = 0; j <messages.length; j++){
      var message = messages[j]

function extractDetails(message){

  var messagedetails=[]
  var attachments = message.getAttachments()
    for (var k = 0; k < attachments.length; k++) {
      Logger.log('Message "%s" contains the attachment "%s" (%s bytes)',
        message.getSubject(), attachments[k].getName(), attachments[k].getSize());
  var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.