1
votes

Here is my code and I can't figure out why replaceText() isn't working.

function createDoc(){
  var templateid = "1jM-6Qvy47gQ45u88WfDU_RvfuSTsw27zBP_9MfsUGr8"; // get template file id
  var FOLDER_NAME = "Completed Rental Agreements"; // folder name of where to put doc
// get the data from an individual user
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var data = sheet.getRange(2, 1, sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
  var lastRow = sheet.getLastRow();

  var firstName = sheet.getRange(lastRow, 2, 1,1).getValues();
  var lastName = sheet.getRange(lastRow, 3, 1,1).getValues();
  var guestEmail = sheet.getRange(lastRow, 7, 1,1).getValues();
  var companyEmail = "[email protected]";
  var companyName = "Bear Lake Project Management";
  var username = "[email protected]"; // get their email (from sheet name)
  var me = "[email protected]";

  //Copy Template
  var docid = DocsList.getFileById(templateid).makeCopy("Rental Agreement - "+firstName+""+lastName+"-"+guestEmail).getId();
//  var file = DocsList.getFileById(docid).addEditors(me);

 // move file to right folder
  var file = DocsList.getFileById(docid);
  var folder = DocsList.getFolder(FOLDER_NAME);
  file.addToFolder(folder);

  var doc = DocumentApp.openById(docid);
  var body = doc.getActiveSection();
  var body_text = doc.addEditor("[email protected]");


 //  Append Cabin Rules 
//   doc.appendParagraph("This is a typical paragraph.");
body.replaceText("/^companyEmail$/", "test");
body.replaceText("%companyName%", "test1");
body.replaceText("%todayDate%", "test1");
doc.saveAndClose();

}

I've tried doc.replaceText and body.replaceText along with several other options.

Any ideas why this isn't working?

Thank in advance

1
Describe what you mean by "isn't working", please.Mogsdad

1 Answers

4
votes

Consider this:

body.replaceText("%companyName%", "test1");

That will look for every instance of "companyName" with "%" on either side of it. The "%" in this case is just that, a piece of punctuation in a strange place. This is a convention used to decrease the likelihood of accidentally replacing real text in the document.

Your template document must have that exact pattern for the replacement to work. (Yours doesn't... instead you have just "companyName". Change it to "%companyName%".) Apply that rule for any other replacement you want to make.


You could benefit from some optimization.

  ...
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  // Next line is hard to maintain - there's a better way.
  // var data = sheet.getRange(2, 1, sheet.getLastRow()-1,sheet.getLastColumn()).getValues();
  // Read whole spreadsheet, skip headers
  var data = sheet.getDataRange().getValues().slice(1);

  // Already read in all data, use it instead of reading sheet again.
  var firstName = data[data.length-1][2-1];    // (2-1) because array counts from 0
  var lastName = data[data.length-1][3-1];     // while spreadsheet columns from 1
  var guestEmail = data[data.length-1][7-1];   // Better: put these into variables.
  ...

While experimenting with your code, I ran into an autocompletion issue with doc.getActiveSection(). It turns out that there has been a recent change, according to the release notes for April 15 2013.

Renamed Document.getActiveSection() to getBody().

You should update your code accordingly.