Search code examples
google-apps-scriptgoogle-sheets

Issue with body.replaceText() in Google Docs for one field only


I am creating Google Doc based on a Google sheet generated from a google Form submission, and all the fields in the google doc template are well replaced using the function body.replaceText().

Now, I had to add a field, and followed the same steps, but this field is not populated with any kind of data, neither static nor from the specific cell.

here you go the part of code:

  body.replaceText("{Br2}",Br2);
  body.replaceText("{Curr2}",Curr2);
  body.replaceText("{OldSc2}",OldSc2);
  body.replaceText("{Sp2}",Sp2);

the last line is the one which is not working, even if i tried to put a direct value into {Sp2}. I am receiving no error upon running the code. Any help?

I tried replacing the new value with a static value and didn't work.


Solution

  • This works for me:

    function myFunction() {
      const doc = DocumentApp.getActiveDocument();
      const body = doc.getBody();
      const [Br2,Curr2,OldSc2,Sp2] = [10,20,30,40];
      
      body.replaceText("{Br2}",Br2);
      body.replaceText("{Curr2}",Curr2);
      body.replaceText("{OldSc2}",OldSc2);
      body.replaceText("{Sp2}",Sp2);
    }
    

    Before replacement:

    {Br2} {Curr2} {OldSc2} {Sp2}

    after replacement:

    10 20 30 40