title: How to create a new document from a template with placeholders
body: <p>I'm trying to create a script that will create new documents from a template-document. Replace placeholders in the documents with data from the sheet based on a keyword search in a specific column. And then change the row's value in the specific column so that the row will not process when the script runs again.</p>
  
  <p>I think I've got it right with the first keyword search, and the loop through the rows. But the last part to get the data to 'merge' to the placeholders I can't figure out how to. I just get the value "object Object" and other values in the document.</p>
  
  <pre><code>var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();
  var lastColumn = s.getLastColumn();
  function createDocFromSheet() {
    var headers = getUpsertHeaders(s);//function is defined outside of this function
  
    var statusColNum = headers.indexOf('Status')+1;
  
    var row = getRowsData(s); //The function is defined outside this function.
    for (var i=0; i&lt;row.length; i++) {
       var jobStatus = '';
       if (row[i]['Status'] === '') {
  
  //New: write the status to the correct row and column - this will be moved to the end when I get the rest right
        var jobStatus = "Created";
        s.getRange(i+2, statusColNum).setValue(jobStatus);
  
  //Find the template and make a copy. Activate the body of the new file.
        var templateFile = DriveApp.getFileById('1lkfmqsJMjjPujHqDqKtcDmL-5GMIxpOWTyCOaK29d2A');
        var copyFile = templateFile.makeCopy()      
        var copyId = copyFile.getId()
        var copyDoc = DocumentApp.openById(copyId)
        var copyBody = copyDoc.getActiveSection()
  
  //Find the rows Values as an object.
        var rows = s.getRange(i+2,1,1,lastColumn)
        var rowsValues = rows.getValues();
  Logger.log(rowsValues)
  //Until here I think it's okay but the last part?
  
  //HOW TO replace the text???
        for (var columnIndex = 0; columnIndex &lt; lastColumn; columnIndex++) {
          var headerValue = headerRow[columnIndex]
          var rowValues = s.getActiveRange(i,columnIndex).getValues()
          var activeCell = rowsValues[columnIndex]
          //var activeCell = formatCell(activeCell);
  Logger.log(columnIndex);
  
          copyBody.replaceText('&lt;&lt;' + headerValue + '&gt;&gt;', activeCell)
  }
  </code></pre>
  
  <p>Template doc : <a href="https://docs.google.com/document/d/1lkfmqsJMjjPujHqDqKtcDmL-5GMIxpOWTyCOaK29d2A/edit?usp=sharing" rel="nofollow noreferrer">Link</a>
  Template sheet: <a href="https://docs.google.com/spreadsheets/d/1V1wmiLYfkui7UcLpoonvgXpCtJGdbeFzZuptR369QUo/edit#gid=0" rel="nofollow noreferrer">Link</a></p>