I've been running this amazing script (provided in full, below) with great success (thanks to user tedinoz!) but have recently been getting an error that reads "Exception: Limit Exceeded: Email Total Attachments Size" -- even though I haven't exceeded the "Email Total Attachments Size" (25 MB / msg, based on this Google Apps Script site).

I've reviewed and removed any all of my large-sized email attachments (and emptied the trash in my GDrive), just to be sure. That seemed to work at first, but the problem has recurred and I'm unable to fix it.

Are there ways to fix the script, or modify my settings in my Google Drive or Google Sheets, that would overcome this Exception error? Many thanks!

 * @OnlyCurrentDoc

function saveGmailMessagebyLabelAsPDF() { 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var ui = SpreadsheetApp.getUi();
  var sheet = ss.getActiveSheet();
  // get the Assumptions sheet
  var assumptionsSheetName = "Assumptions";
  var assumptions = ss.getSheetByName(assumptionsSheetName);
  // get the Gmail label
  var GmailLabelName= assumptions.getRange("GmailLabel").getA1Notation();
  //Logger.log("DEBUG: GmailLabel Name range = "+ GmailLabelName);
  var GmailLabelValue=assumptions.getRange(GmailLabelName).getValue();
  // Logger.log("DEBUG:GmailLabel row nuber is "+GmailLabelValue);

  // test for/get the GMail search label 
  var gmailLabels = sheet.getRange(GmailLabelValue,2).getValue();
  Logger.log("DEBUG: the label is "+gmailLabels+", length = "+gmailLabels.length);

  if (gmailLabels.length  == 0){
    // label lengh is zero, so the field is empty, so stop processing
    var response = ui.alert('GMail 2 PDF Alert:', 'It looks like the Gmail label field is empty. Processing will stop so you can double-check', ui.ButtonSet.OK);

  // get the Gmail Output ID column
  var GMailbyIDPDF= assumptions.getRange("GMailbyIDPDF").getA1Notation();
  // Logger.log("DEBUG: GMailbyIDPDF Name range = "+GMailbyIDPDF);
  var GMailbyIDPDFValue=assumptions.getRange(GMailbyIDPDF).getValue();
  // Logger.log("DEBUG: GMailIDSearch Column number is "+GMailbyIDPDFValue);

  // get the Gmail starting row
  var InsertNewRowRange = assumptions.getRange("InsertNewRowRange").getA1Notation();
  // Logger.log("DEBUG: InsertNewRowRange Name range = "+InsertNewRowRange);
  var InsertNewRowValue=assumptions.getRange(InsertNewRowRange).getValue();
  // Logger.log("DEBUG: InsertNewRowValue row number is "+InsertNewRowValue);
  // get the Internal ID column
  var InternalIDRange = assumptions.getRange("InternalID").getA1Notation();
  // Logger.log("DEBUG: InternalIDRange Name range = "+InternalIDRange);
  var InternalIDValue=assumptions.getRange(InternalIDRange).getValue();
  // Logger.log("DEBUG: InternalIDValue Column number is "+InternalIDValue);
  // get details of active cell
  var cell = sheet.getActiveCell();
  var activerow = cell.getRow();
  var activecol = cell.getColumn();
  // Logger.log("DEBUG: Activecell = "+cell.getA1Notation()+", row:"+activerow+", column:"+activecol);
  var columnLetter = columnToLetter(GMailbyIDPDFValue);
   // test for activecell in the right column and row
   if (activerow < InsertNewRowValue || activecol != GMailbyIDPDFValue){
    // row isn't a data row and/or column isn't Gmail Link, so stop processing
    var startresponse = ui.alert('GMail 2 PDF ALERT:', 'The cursor is not in the right Row ('+InsertNewRowValue+' or below) or Column (Column '+columnLetter+'). Processing will stop so you can double-check', ui.ButtonSet.OK);
   // test for id value
   var InternalId = sheet.getRange(activerow, InternalIDValue).getValue();
   Logger.log("DEBUG: the sub-label is "+InternalId+", length = "+InternalId.length);
  if (InternalId.length  == 0){
    // label lengh is zero, so the field is empty, so stop processing
    var IDlenresponse = ui.alert('GMail 2 PDF ALERT:', 'The Sub Label field on this row is empty. Processing will stop so you can double-check', ui.ButtonSet.OK);
  // get the Google Drive folder
  var GoogleDriveFolderName= assumptions.getRange("GoogleDriveFolder").getA1Notation();
  //Logger.log("DEBUG: GoogleDriveFolder Name range = "+GoogleDriveFolderName);
  var GoogleDriveFolderValue=assumptions.getRange(GoogleDriveFolderName).getValue();
  //Logger.log("DEBUG:GoogleDriveFolder row nuber is "+GoogleDriveFolderValue);
  // test for/get the GDrive folder
  var driveFolder = sheet.getRange(GoogleDriveFolderValue,2).getValue();
  // Logger.log("DEBUG: G/Drive folder name = "+driveFolder+", length = "+driveFolder.length);
  if (driveFolder.length  == 0){
    // driveFolder lengh is zero, so the field is empty, so stop processing
    var response = ui.alert('GMail 2 PDF ALERT:', 'The GDrive Folder Name field on this sheet is empty. Processing will stop so you can double-check', ui.ButtonSet.OK);
  Logger.log("DEBUG: the gdrive folder is "+driveFolder);
  var folders = DriveApp.getFoldersByName(driveFolder); // returns folder iterator
   Logger.log("folders next"); // DEBUG header
   Logger.log(folders); // DEBUG folders list -> FolderIterator
  var flr_hasnext = folders.hasNext(); // will calling next() will return an item.
  var temprange1 = sheet.getRange("h3");
  var temprange2 = sheet.getRange("h4");
  if (!flr_hasnext){
    var message = driveFolder+" does NOT exist";
    var response = ui.alert('GMail 2 PDF ALERT:', 'The GDrive Folder does NOT exist. Processing will stop so you can create it', ui.ButtonSet.OK);
     var message = driveFolder+" DOES exist";
   Logger.log("DEBUG: flr_hasnext = "+flr_hasnext); // => FALSE
  var fdr_next = folders.next();
  temprange2.setValue("line 120")
   Logger.log("fdr_next = "+fdr_next+", and ID = "+fdr_next.getId());
  var subFolders = fdr_next.getFoldersByName(InternalId);
    var subFolderExist = subFolders.next().getName();
    // Logger.log("Yes, There is ID folder.");
  } else {
    // Logger.log("There was no ID folder. But created") ;
  var IDfolders = DriveApp.getFoldersByName(InternalId);
  var IDfdr_next = IDfolders.next();
  var IDfdrID = IDfdr_next.getId();
  var IDfdrURL = IDfdr_next.getUrl();
  // Logger.log("ID Folder = "+IDfdr_next+", and ID = "+IDfdrID);
  // Logger.log("ID Folder URL:"+IDfdrURL);
  var labelsearch = gmailLabels+"-"+InternalId;
  // Logger.log("DEBUG: label search value = "+labelsearch)

  //Get the threadsfor the ID
  //var threads = GmailApp.search("in:" + gmailLabels, 0, 5);  
  //var threads = GmailApp.search(InternalId);  
  var threads = GmailApp.search("label:" + labelsearch);  
  // Logger.log(threads) // list of threads

  if (threads.length > 0) {
    /* Google Drive folder where the Files would be saved */
    var targetfolder = IDfdr_next;
    /* Loop through the threads */
    for (var t=0; t<threads.length; t++) {
      var msgs = threads[t].getMessages();
      // Logger.log("DEBUG: LIts of threads = "+msgs)
      // Logger.log("DEBUG: number of messages = "+msgs.length) 

      var html = "";
      var attachments = [];
      var subject = threads[t].getFirstMessageSubject();
      /* Append all the threads in a message in an HTML document */
      for (var m=0; m<msgs.length; m++) {
        // Logger.log("DEBUG: m="+m+", msg = "+msgs[m]);    
        var msg = msgs[m];
        var html = "";
        var attachments = [];
       // build details of the message
        html += "From: " + msg.getFrom() + "<br />";  
        html += "To: " + msg.getTo() + "<br />";
        html += "Date: " + msg.getDate() + "<br />";
        html += "Subject: " + msg.getSubject() + "<br />"; 
        html += "<hr />";
        html += msg.getBody().replace(/<img[^>]*>/g,"");
        html += "<hr />";
        var DisplayDate = Utilities.formatDate(msg.getDate(), "GMT-5", "MM/dd/YY");
        //Logger.log("DEBUG: message details#"+(+m+1)+" = "+html);
        var atts = msg.getAttachments();
        for (var a=0; a<atts.length; a++) {
          //var attachmentName = "Attachment: "+msg.getSubject()+": "+atts[a];
        /* Save the attachment files and create links in the document's footer */
        if (attachments.length > 0) {
        var footer = "<strong>Attachments:</strong><ul>";
        for (var z=0; z<attachments.length; z++) {
          //var attName = "Attachment: "+attachments[z];
          //Logger.log("attName = "+attName)
          //Logger.log("create file = "+attachments[z]);
          //var attname = "Attachment_Thread:"+subject+"_"+attachments[z].getName();
          var attname = DisplayDate+"-Msg#"+(+m+1)+"-Attach:"+attachments[z].getName();
          // Logger.log("attname = "+attname);
          // Logger.log("does this attachment already exist? - "+targetfolder.getFilesByName(attname).hasNext());
          if (targetfolder.getFilesByName(attname).hasNext() == true){
            // Logger.log("this file already exists - "+targetfolder.getFilesByName(attname).hasNext());
            // delete the file
          //var file = folder.createFile(attname);
          var file = targetfolder.createFile(attachments[z]).setName(attname);
          //var file = folder.createFile(attachments[z]);
          //Logger.log("attachment = "+file.getName());
          footer += "<li><a href='" + file.getUrl() + "'>" + file.getName() + "</a></li>";
        html += footer + "</ul>";
        // Logger.log("message details-Final = "+html);
      } // end of attachments
        /* Convert the Email Message into a PDF File */
        var tempFile = DriveApp.createFile("temp.html", html, "text/html");
        var newname = DisplayDate+"-Msg#"+(+m+1)+"-Email:"+subject + ".pdf";
        // Logger.log("new name = "+newname);
        // Logger.log("does this file already exist? - "+targetfolder.getFilesByName(newname).hasNext());
        if (targetfolder.getFilesByName(newname).hasNext() == true){
          // Logger.log("this file already exists - "+targetfolder.getFilesByName(newname).hasNext());
          // delete the file
      //folder.createFile(tempFile.getAs("application/pdf")).setName("Thread: "+subject + ".pdf");
      Logger.log("Thread#"+(m)+"-message#"+t+" - saved") 
        Logger.log("end of message#"+m)
      } // end looping through messages
    } // looping through threads
  // creatre hyperlink to GDrive subfolder
  cell.setFormula('=HYPERLINK("'+IDfdrURL+'","GDrive Link")');
  // Logger.log("aded hyperlink to GDrive folder");

  • Welcome to Web Applications Stack Exchange. Your question will be more likely to attract answers if you edit it to show the code. See How to create a Minimal, Reproducible Example and The correct way to publicly share a Google test sheet. Nov 5, 2021 at 7:54
  • Please add the textual error message and include the steps that you follow to run the script. Nov 5, 2021 at 16:02
  • The steps include: (1) Filling out information in cells B1 - B7 in GoogleSheets, which include the Gmail label name (B4) and Google Drive label name (B5), (2) Filling out information for row 9, crucially the Gmail Sub label (C9), which then generates a link for cell M9 (Gmail Link by Sub label), and then (3) placing the cursor on cell N9 and running the script. This is the full error message that I see: "Message details Exception: Limit Exceeded: Email Total Attachments Size." Is that what you requested? Thanks for your help! Nov 5, 2021 at 16:25
  • I should quickly add that I recently faced the processing error while using this script: "Exception: Conversion from text/ html to application/pdf failed." It only happened a few times, and the real Exception error has been: "Limit Exceeded: Email Total Attachments Size." Any ideas or suggestions on how to fix this? Please let me know if I can provide you with any more info. Thanks again! Nov 7, 2021 at 6:51
  • Hi, user12228535, can we discuss off-line?
    – Tedinoz
    Nov 7, 2021 at 23:25

1 Answer 1


There is no fix for this AFAIK. Google Apps Script has a hard setting for attachment sizes of 25 MiB regardless of whether you are a Google Workspace or normal consumer (@gmail.com) user. There is no way to increase this quota at this time.

  • OP noted that "even though I haven't exceeded the "Email Total Attachments Size"
    – Blindspots
    Mar 22 at 18:23

Your Answer

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

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