title: Google sheets API PDF naming
body: <p>Working on a project, merging rows to create a PDF and Emailing it.
  Can't figure out why my code is not renaming the PDF to the Filename followed by the Name
  I am thinking it is something to do with <code>**var PDF_FILE_NAME**</code> or <code>// Self note: Rename the new PDF file</code> or</p>
  <pre><code>var FILE_NAME_COLUMN_NAME = 'File Name'
  var EMAIL_COLUMN_NAME = 'Email'
  var NAME_COLUMN_NAME = 'Name'
  </code></pre>
  <p>here is the set of codes and a screenshot of the data.
  <strong>Apologies for the self notes and long code. it guides me and educates me to find errors when I work on it</strong>
  <a href="https://i.stack.imgur.com/9coxs.png" rel="nofollow noreferrer">enter image description here</a></p>
  <pre><code>// self note:Doc to merge template
  var TEMPLATE_ID = '1a1iQGpCo3c4djPcYGtcTZg1uSi1-KA_ZzpzxAcNFcvU'
  // self note: to specify name code below
  var PDF_FILE_NAME = (FILE_NAME_COLUMN_NAME = 'File Name' + ' '+'testing')
  // self note: If Email available will email
  var EMAIL_SUBJECT = 'The email subject'
  var EMAIL_BODY = 'The email body'
  
  // Self note: where PDF saved on drive    
  var RESULTS_FOLDER_ID = '1jZJ9u1BGZu8bfwsFJGcZFz4pkAHBG0K-'
  // Self Note: Constants
  // Self Note:You can pull out specific columns values 
  var FILE_NAME_COLUMN_NAME = 'File Name'
  var EMAIL_COLUMN_NAME = 'Email'
  var NAME_COLUMN_NAME = 'Name'
  
  var DATE_FORMAT = 'yyyy/MM/dd';
  
  function onOpen() {
    SpreadsheetApp
      .getUi()
      .createMenu('[ Create PDFs ]')
      .addItem('Create a PDF for each row', 'createPdfs')
      .addToUi()
  } // onOpen()
  
  /**  
   * self note: pick fields, each row in the active sheet to doc to creat pdf
   */
  
  function createPdfs() {
    var ui = SpreadsheetApp.getUi()
    if (TEMPLATE_ID === '') {    
      ui.alert('TEMPLATE_ID needs to be defined in code.gs')
      return
    }
    // self noteSet docs sheet access
    var templateFile = DriveApp.getFileById(TEMPLATE_ID)
    var activeSheet = SpreadsheetApp.getActiveSheet()
    var allRows = activeSheet.getDataRange().getValues()
    var headerRow = allRows.shift()
  
    // self Note: Create a PDF for each row
    allRows.forEach(function(row) {
      createPdf(templateFile, headerRow, row)
  
      /**
       * Create a PDF
       *
       * @param {File} templateFile
       * @param {Array} headerRow
       * @param {Array} activeRow
       */
    
      function createPdf(templateFile, headerRow, activeRow) {
        var headerValue
        var activeCell
        var ID = null
        var recipient = null
        var copyFile
        var numberOfColumns = headerRow.length
        var copyFile = templateFile.makeCopy()      
        var copyId = copyFile.getId()
        var copyDoc = DocumentApp.openById(copyId)
        var copyBody = copyDoc.getActiveSection()
        for (var columnIndex = 0; columnIndex &lt; numberOfColumns; columnIndex++) {
          headerValue = headerRow[columnIndex]
          activeCell = activeRow[columnIndex]
          activeCell = formatCell(activeCell);
                  
          copyBody.replaceText('&lt;&lt;' + headerValue + '&gt;&gt;', activeCell)
          if (headerValue === FILE_NAME_COLUMN_NAME) {
            ID = activeCell
          } else if (headerValue === EMAIL_COLUMN_NAME) {
            recipient = activeCell
          }
        }
        // Self Note: PDF file create
        copyDoc.saveAndClose()
        var newFile = DriveApp.createFile(copyFile.getAs('application/pdf'))  
        copyFile.setTrashed(true)
  
        // Self note: Rename the new PDF file
        if (PDF_FILE_NAME !== '') {
          newFile.setName(PDF_FILE_NAME)
        } else if (ID !== null){
          newFile.setName(ID)
        }
        
        // Self note PDF put in folder
        if (RESULTS_FOLDER_ID !== '') {
          DriveApp.getFolderById(RESULTS_FOLDER_ID).addFile(newFile)
          DriveApp.removeFile(newFile)
        }
  
        // Self Note: Email the new PDF
        if (recipient !== null) {
          MailApp.sendEmail(
            recipient, 
            EMAIL_SUBJECT, 
            EMAIL_BODY,
            {attachments: [newFile]})
        }
      } // createPdfs.createPdf()
    })
    ui.alert('New PDF files created')
    return
    
    // Private Functions
    /**
    * Format the cell's value
    *
    * @param {Object} value
    *
    * @return {Object} value
    */
    function formatCell(value) {
      var newValue = value;
      if (newValue instanceof Date) {
        newValue = Utilities.formatDate(
          value, 
          Session.getScriptTimeZone(), 
          DATE_FORMAT);
      } else if (typeof value === 'number') {
        newValue = Math.round(value * 100) / 100
      }
      return newValue;
    } // createPdf.formatCell()
  } // createPdfs()
  </code></pre>