Search code examples
google-apps-scriptgoogle-sheetsgoogle-docsgoogle-forms

textReplace function isn't replacing text in created document when running function on a form submit


Following a successful attempt at creating a Autofill version of my organisations referral form using Forms, Sheets, and Docs on Google Suite I have been trying to do the same thing with our Membership form. In my head this should be exactly the same process of defining the e.values, making a copy of the template document and naming it, setting up the textReplace strings, and the then a Save & Close. IT's all that was in the previous one.

However, now when the function is triggered it is creating the document and naming and saving it as I expects, however all the strings of text are in the document it creates, not any of the information I wanted. Can you spot what's wrong with the script?

function AutoFillMembershipForm(e) {
var Timestamp = e.values[0];
var PreferredName = e.values[1];
var Surname = e.values[2];
var Pronouns = e.values[3];
var Different = e.values[4];
var DOB = e.values[5];
var Address = e.values[6];
var Phone = e.values[7];
var Email = e.values[8];
var Language = e.values[9];
var Contact = e.values[10];
var Legal1 = e.values[11];
var Legal1Contact = e.values[12];
var Legal2 = e.values[13];
var Legal2Contact = e.values[14];
var Emergency = e.values[15];
var EmergenyContact = e.values[16];
var Disability = e.values[17];
var Medical = e.values[18];
var Support = e.values[19];
var Allergies = e.values[20];
var GP = e.values[21];
var GDPR = e.values[22];
var Voicemail = e.values[23];

var templateFile = DriveApp.getFileById("\\id");
var templateResponseFolder = DriveApp.getFolderById("\\id");

var copy = templateFile.makeCopy(PreferredName + " " + Surname + " " + DOB + " Membership", templateResponseFolder);
var doc = DocumentApp.openById(copy.getID());

var body = doc.getBody();

body.replaceText("PreferredName", PreferredName);
body.replaceText("{Surname}", Surname);
body.replaceText("{Pronouns}", Pronouns);
body.replaceText("{Different}", Different);
body.replaceText("{DOB}", DOB);
body.replaceText("{Address}", Address);
body.replaceText("{Phone}", Phone);
body.replaceText("{Email}", Email);
body.replaceText("{Language}", Language);
body.replaceText("{Contact}", Contact);
body.replaceText("{Legal1}", Legal1);
body.replaceText("{Legal1Contact}", Legal1Contact);
body.replaceText("{Legal2}", Legal2);
body.replaceText("{Legal2Contact}", Legal2Contact);
body.replaceText("{Emergency}", Emergency);
body.replaceText("{EmergencyContact}", EmergenyContact);
body.replaceText("{Disability}", Disability);
body.replaceText("{Medical}", Medical);
body.replaceText("{Support}", Support);
body.replaceText("{Allergies}", Allergies);
body.replaceText("{GP}", GP);
body.replaceText("{GDPR}", GDPR);
body.replaceText("{Voicemail}", Voicemail);
body.replaceText("{Timestamp}", Timestamp);

doc.saveAndClose();
}

This is exactly the same layout and process I've used for the Referral Form, which is working successfully.

function AutoFillReferralForm(e) {
var Timestamp = e.values[0];
var Consent = e.values[1];
var ReferrerName = e.values[2];
var ReferrerOrg = e.values[3];
var JobTitle = e.values[4];
var ReferrerAddress = e.values[5];
var ReferrerPhone = e.values[6];
var ReferrerEmail = e.values[7];
var YPName = e.values[8];
var DOB = e.values[9];
var YPAddress = e.values[10];
var YPPhone = e.values[11];
var YPEmail = e.values[12];
var ContactPreference = e.values[13];
var HowWork = e.values[14];
var ContinueWork = e.values[15];
var OtherAgencies = e.values[16];
var Reason = e.values[17];
var AdditionalInfo = e.values[18];
var Signature = e.values[19];
var Date = e.values[20];

var templateFile = DriveApp.getFileById("\\id");
var templateResponseFolder = DriveApp.getFolderById("\\id");

var copy = templateFile.makeCopy(YPName + " " + DOB + " Referral", templateResponseFolder);
var doc = DocumentApp.openById(copy.getId());

var body = doc.getBody();

body.replaceText("{ReferrerName}", ReferrerName);
body.replaceText("{ReferrerOrg}", ReferrerOrg);
body.replaceText("{JobTitle}", JobTitle);
body.replaceText("{ReferrerAddress}", ReferrerAddress);
body.replaceText("{ReferrerPhone}", ReferrerPhone);
body.replaceText("{ReferrerEmail}", ReferrerEmail);
body.replaceText("{YPName}", YPName);
body.replaceText("{DOB}", DOB);
body.replaceText("{YPAddress}", YPAddress);
body.replaceText("{YPPhone}", YPPhone);
body.replaceText("{YPEmail}", YPEmail);
body.replaceText("{ContactPreference}", ContactPreference);
body.replaceText("{HowWork}", HowWork);
body.replaceText("{ContinueWork}", ContinueWork);
body.replaceText("{OtherAgencies}", OtherAgencies);
body.replaceText("{Consent}", Consent);
body.replaceText("{Reason}", Reason);
body.replaceText("{AdditionalInfo}", AdditionalInfo);
body.replaceText("{Signature}", Signature);
body.replaceText("{Date}", Date);

doc.saveAndClose(); 
}

I've no idea why one works and one doesn't.

Many thanks in advance


Solution

  • You have a typo in your script getID should be getId

    Replace

    var doc = DocumentApp.openById(copy.getID());
    

    With

    var doc = DocumentApp.openById(copy.getId());