0
votes

I'm having trouble, replacing text in a Google Doc, using App Script.

Inside the document I have certain text like {{Date of birth (dd/mm/yyyy)}}. I need to replace it with user entered values from Google Form. I am getting the values from Google Form properly but few replacement pattern e.g {{Date of birth (dd/mm/yyyy)}} not getting replaced. Other replacement characters like {{Name}} , {{Email}} working properly. Here is my part of the replacement script as follows:

if(this.sheetColumns.length){    
        for(var k=0; k < this.sheetColumns.length; k++){
          docValues.replaceText(Utilities.formatString('\\{\\{(%s)\\}\\}', escape(this.sheetColumns[k])), this.formObj.namedValues[this.sheetColumns[k].trim()]);            
        }
}

Any help will be appreciated.

1
Can you share the docs (or a copy of it with public access) and the values that need to be passed as var in the script?Kessy

1 Answers

0
votes

The regex expression to get {{Date of birth (dd/mm/yyyy)}} is "({{Date of birth [(]dd[/]mm[/]yyyy[)]}})".

For the date I have created a form to report to a Spreadsheet and get the date from there. In my case the date was in the second column (num. 1 in apps script) and I have formatted the date like day-month-year and it goes like Utilities.formatDate(sheetData[i][1], "GMT+02:00", "dd-MM-yyyy".

The end result is the following:

if(sheetData.length) {
    for (var i = 1; i < sheetData.length; i++) {
      body.replaceText("({{Date of birth [(]dd[/]mm[/]yyyy[)]}})",Utilities.formatDate(sheetData[i][1], "GMT+02:00", "dd-MM-yyyy"));
    }
  }