Rafael
Rafael

Reputation: 49

Don't convert one file to PDF - Google Apps Script

I have this part of code below

      doc.saveAndClose();

      reportPDF = doc.getAs('application/pdf')
      reportPDF.setName('Doc1 - '+ rows[0][0] + ".pdf");
      
      var file = destinationFolder.createFile(reportPDF);  
      var folder = DriveApp.getFolderById("12bcF_SHkGtENTcAJrqbR_Vsd1oN7Qd3S");
      var file2 = folder.getFilesByName("Doc2.pdf");

      DriveApp.getFileById(doc.getId()).setTrashed(true);

      if(file2.hasNext()){
            var file2pdf = file2.next().getAs(MimeType.PDF);
            emails.forEach(function(email) {
            MailApp.sendEmail(email, "Subject - " + rows[0][0], "Hello!" + '\n' + '\n' + "Attached to this email is your Documents " +dadosMes , {
            name: 'Best Practices Report',
            attachments: 
              [
              file.getAs(MimeType.PDF),
              file2pdf                
              ]
                                       
             });
           })
        } 

}

The code sends an email to whoever compiles it, containing two PDF documents that are created.

Is there a easy way to not convert the Doc1 to pdf? I want to send the Doc1 like a document in word and the doc2 will stay as a pdf.

I'm thinking here and doing some tests (like delete or try to modify the reportPDF part), but I don't know if there's an 'easier' way.

Edit:

The part that create the doc is:

        const copy = googleDocTemplate.makeCopy('Metrics' , destinationFolder);
        const doc = DocumentApp.openById(copy.getId());
        const body = doc.getBody();

Upvotes: 0

Views: 149

Answers (1)

Rafa Guillermo
Rafa Guillermo

Reputation: 15357

Answer:

To convert a Google Doc file to a Microsoft Word file, you must use an export link.

Code:

/**
 * Converts Google Doc file to defined Mime Type Format
 * @param  {String} fileID   - Document ID for export
 * @param  {String} folderId - Folder ID of folder in which to save Sheet 
 * @return {Blob}   blob     - blob of the file
 */
function convertFile(fileId, mimeType) {
  // Define export endpoint and method.
  const url = `https://www.googleapis.com/drive/v3/files/${fileId}/export?mimeType=${mimeType}`
    
  const blob = UrlFetchApp.fetch(url, {
    method: "get",
    headers: {
      "Authorization": `Bearer ${ScriptApp.getOAuthToken()}`
    },
    muteHttpExceptions: true
  }).getBlob()
   
  const fileName = DriveApp.getFileById(fileId).getName()
  blob.setName(`${fileName}.docx`)

  return blob  
}

function main() {
  // This will be the ID of your Google Doc file, 'doc':
  const fileId = "file-ID" 

  const docx = convertFile(fileId, MimeType.MICROSOFT_WORD)
  const email = "[email protected]"

  const file2 = folder.getFilesByName("Doc2.pdf")
  DriveApp.getFileById(doc.getId()).setTrashed(true);

  if(file2.hasNext()) {
    const file2pdf = file2.next().getAs(MimeType.PDF)
    MailApp.sendEmail(email, "Subject" , "Message Body", {
      "name": "Best Practices Report",
      "attachments": [
        docx.getAs(MimeType.MICROSOFT_WORD),
        file2pdf
      ]
    })
  }
}

Upvotes: 2

Related Questions