0

My script to find the correct email and save it's attachment works however, I do not know how to save the attached excel file as a google sheet. The only way I have managed to change it is by manually opening up the saved file and clicking file - save as google sheets or having another script to convert. I would like the attached file saved as a google spreadsheet in the first instance, is this possible?



    function iTrentStaffToGDrive() { 
  
    var gmailLabels  = 'iTrentData/NewReportForStaff'  
    var archiveLabel = 'iTrentData/ProcessedReportForStaff'
  
    var moveToLabel =  GmailApp.getUserLabelByName(archiveLabel);
    var removeToLabel =  GmailApp.getUserLabelByName(gmailLabels); 
  
    var d = new Date()
    var curr_date = d.getDate()
    var curr_month = d.getMonth() + 1 //Months start at 0 
    var curr_year = d.getFullYear()

    var theDate = curr_year + "-" + curr_month + "-" + curr_date


    var driveFolder  = '1BSNWjnnTLliaCI9VnZrcqBiWga4K_pFF' //iTrentDataCurrentStaffNewReport 
  
    if ( ! moveToLabel ) {    
    moveToLabel = GmailApp.createLabel(archiveLabel);    
    }

  
    var filter = "has:attachment -label:" + archiveLabel + " label:" + gmailLabels;
  
    var threads = GmailApp.search(filter, 0, 5);  

    var destFolder = DriveApp.getFolderById(driveFolder);
    Logger.log(destFolder);

  
  
    for (var x=0; x<threads.length; x++) {
    
    var message = threads[x].getMessages()[0];
    
    var desc   = message.getSubject() + " #" + theDate + " #" + message.getId();
    var att    = message.getAttachments();
    //var blob   = message.setContentType({mimeType: MimeType.GOOGLE_SHEETS});
    // .setContentType({mimeType: MimeType.GOOGLE_SHEETS})
    
    for (var z=0; z<att.length; z++) {
      try {
       file = destFolder.createFile(att[z]); 
       file.setDescription(desc);
       file.setName (message.getSubject() + " " + theDate)  //
      // file.setContentTypeFromExtension()
      // file.setContentTypeFromExtension({mimeType: MimeType.GOOGLE_SHEETS}) 
      }
      catch (e) {
        Logger.log(e.toString());
      }
    }
      
    threads[x].addLabel(moveToLabel);
    threads[x].removeLabel(removeToLabel); 
    }

    }


As you'll see from my notes within the code, I've tried a couple of ways to save as google.sheets but no joy, yet

Paul
  • 1
  • 2
  • The answer linked is to convert a saved file, I am asking if it's possible to save a excel file attachment as a google spreadsheet first time round, without saving as excel then converting. In VBA, you can "saveas" and enter your chosen file format - I'm wanting to replicate something along those lines. – Paul Nov 08 '22 at 11:45

0 Answers0