Google.App.Script replace.Text not working

39 Views Asked by At

I am very new to this but would appreciate any help. I am trying to take a spreadsheet and convert the text into a doc using replace text. Everything seems to work, it creates the doc, saves it in the designated folder with the right title but none of the text is being replaced in the actual GoogleDoc.I have triple checked spelling etc but I can't seem to find the error. Pasting the entire code as I am not sure where the problem is.

function onOpen() {
  const ui = SpreadsheetApp.getUi();
  const menu = ui.createMenu('AutoFill Docs');
  menu.addItem('Create New Docs', 'createNewGoogleDocs');
  menu.addToUi();
}

function createNewGoogleDocs() {
  const googleDocTemplate = DriveApp.getFileById('1UqtS6Uw4JU59IizIHZwncJjuSftKOsQH');
  const destinationFolder = DriveApp.getFolderById('1avJUSvwJoA31evoNzngcLJiZfympFCWe');
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  const rows = sheet.getDataRange().getValues();

  rows.forEach(function(row, index) {
    if(index === 0)return;
    if(row[29])return;

    const copy = googleDocTemplate.makeCopy(`CC_${row[0]}_${row[1]}`, destinationFolder);
    const doc = DocumentApp.openById(copy.getId());
    const body = doc.getBody();
    const friendlyDate = new Date(row[4]).toLocaleDateString();

    body.replaceText('{{Full Name}}',row[0]);
    body.replaceText('{{Position}}',row[1]);
    body.replaceText('{{Yrs of Experience}}',row[2]);
    body.replaceText('{{Nationality}}',row[3]);
    body.replaceText('{{Date of Birth}}',friendlyDate);
    body.replaceText('{{Languages}}',row[5]);
    body.replaceText('{{Profile Summary}}',row[6]);
    body.replaceText('{{Qualifications}}',row[7]);
    body.replaceText('{{Skills}}',row[8]);
    body.replaceText('{{Emp1 Date}}',row[9]);
    body.replaceText('{{Emp1 Name}}',row[10]);
    body.replaceText('{{Emp1 Position}}',row[11]);
    body.replaceText('{{Emp1 Resp}}',row[12]);
    body.replaceText('{{Emp2 Date}}',row[13]);
    body.replaceText('{{Emp2 Name}}',row[14]);
    body.replaceText('{{Emp2 Position}}',row[15]);
    body.replaceText('{{Emp2 Resp}}',row[16]);
    body.replaceText('{{Emp3 Date}}',row[17]);
    body.replaceText('{{Emp3 Name}}',row[18]);
    body.replaceText('{{Emp3 Position}}',row[19]);
    body.replaceText('{{Emp3 Resp}}',row[20]);
    body.replaceText('{{Emp4 Date}}',row[21]);
    body.replaceText('{{Emp4 Name}}',row[22]);
    body.replaceText('{{Emp4 Position}}',row[23]);
    body.replaceText('{{Emp4 Resp}}',row[24]);
    body.replaceText('{{Emp5 Date}}',row[25]);
    body.replaceText('{{Emp5 Name}}',row[26]);
    body.replaceText('{{Emp5 Position}}',row[27]);
    body.replaceText('{{Emp5 Resp}}',row[28]);

    doc.saveAndClose();
    const url = doc.getUrl();
    sheet.getRange(index+1,29).setValue(url);
  })
}

I am trying to take a spreadsheet and convert the text into a doc using replace text.

1

There are 1 best solutions below

1
Cooper On

I reduced the size of the code but left it mostly intact. I did eliminate the headers from the beginning so skipping over them was no longer necessary. And I found that the code worked with no problems

function createNewGoogleDocs() {
  const googleDocTemplate = DriveApp.getFileById(gobj.globals.testdocid);//replace id
  const destinationFolder = DriveApp.getFolderById(gobj.globals.folder1id);//replace id
  const ss = SpreadsheetApp.getActive();
  const sheet = ss.getSheetByName('Sheet0');//replace sheet
  const [,...vs] = sheet.getDataRange().getValues();//removed header

  vs.forEach((r, i) => {
    if(r[8])return;//not sure what this is for except maybe to show that it already has a url so a copy may have been made already
    const copy = googleDocTemplate.makeCopy(`CC_${r[0]}_${r[1]}`, destinationFolder);
    const doc = DocumentApp.openById(copy.getId());
    const body = doc.getBody();
    const dts = new Date(r[4]).toLocaleDateString();
    body.replaceText('{{COL1}}',r[0]);
    body.replaceText('{{COL2}}',r[1]);
    body.replaceText('{{COL3}}',r[2]);
    body.replaceText('{{COL4}}',r[3]);
    body.replaceText('{{COL5}}',dts);
    body.replaceText('{{COL6}}',r[5]);
    body.replaceText('{{COL7}}',r[6]);
    body.replaceText('{{COL8}}',r[7]);
    doc.saveAndClose();
    const url = doc.getUrl();
    sheet.getRange(i+2,9).setValue(url);//changed the offset because I removed the headers
  })
}