Trying to convert google form into a pdf and then email it. Currently it stores the data into an array, and then later grabs that info, puts it into a doc instead of a form.
When it gets to grabbing the info is where it keeps throwing an error.
function createPDF(){
// all info collected on the form, pre-filled to test
const info = {
'First Name' : ['Mike'],
'Last Name' : ['Wazowski'],
'Birthday' : [0],
'Email' : ['m@w.com'],
'Phone Number' : ['1'],
'Province' : ['Ontario'],
'Contact Method' : ['email'],
'Language': ['english'],
'Type Of Service': ['in person'],
'Child Name': [],
'Child Birthday': [],
'Services Required': ['counselling'],
'Staff Requested': ['Nancy'],
'Priority':['Health'],
'Referral': ['blank'],
'Jane Consent': ['Yes'],
};
const pdfFolder = DriveApp.getFolderById("1g58GUQLPjPonsHtxj5LlxyoDgXs5wj2R");
const tempFolder = DriveApp.getFolderById("1Fkzf0xeZcedfq7BF2k3V4mn4Pz_LsXsv");
const templateDoc = DriveApp.getFileById("1eOqom8SqhuDUpIqYEVum-EvQ09cVz2d_XCLcRNAz8jE");
const newTempFile = templateDoc.makeCopy(tempFolder);
const openDoc = DocumentApp.openById(newTempFile.getId());
const body = openDoc.getBody();
body.replaceText("{fn}", info['First Name'][0]);
body.replaceText("{ln}", info['Last Name'][1]); /*throwing an error at this line, specifically Error
Exception: Invalid argument: replacement
createPDF @ Code.gs:36 */
body.replaceText("{bd}", info['Birthday'][2]);
body.replaceText("{em}", info['Email'][3]);
body.replaceText("{pn}", info['Phone Number'][4]);
body.replaceText("{pv}", info['Province'][5]);
body.replaceText("{cm}", info['Contact Method'][6]);
body.replaceText("{lg}", info['Language'][7]);
body.replaceText("{ts}", info['Type of Service'][8]);
body.replaceText("{cn}", info['Child Name'][9]);
body.replaceText("{cbd}", info['Child Birthday'][10]);
body.replaceText("{sr}", info['Services Required'][11]);
body.replaceText("{stf}", info['Staff Requested'][12]);
body.replaceText("{pri}", info['Priority'][13]);
body.replaceText("{ref}", info['Referral'][14]);
body.replaceText("{jc}", info['Jane Consent'][15]);
const blobPDF = newTempFile.getAs(MimeType.pdf);
const pdfFile = pdfFolder.createFile(blobPDF).getName("Form Response");
}
Originally I thought the birthday was an issue because it's not a string it's a date on the form, but after I tried replacing that with a string instead it still didn't work.
I'm trying to copy this guy: https://www.youtube.com/watch?v=EpZGvKIHmR8&ab_channel=LearnGoogleSheets%26ExcelSpreadsheets
but his works and mine doesn't, and the only difference is the amount of data I have and the fact that i'm using a date and not a string.
All of your info values are arrays with only one element and you are trying to access elements beyond that. So you need to change your body.replaceText()
calls to use index of [0].
body.replaceText("{fn}", info['First Name'][0]);
body.replaceText("{ln}", info['Last Name'][0]);
body.replaceText("{bd}", info['Birthday'][0]);
body.replaceText("{em}", info['Email'][0]);
body.replaceText("{pn}", info['Phone Number'][0]);
body.replaceText("{pv}", info['Province'][0]);
body.replaceText("{cm}", info['Contact Method'][0]);
body.replaceText("{lg}", info['Language'][0]);
body.replaceText("{ts}", info['Type of Service'][0]);
body.replaceText("{cn}", info['Child Name'][0]);
body.replaceText("{cbd}", info['Child Birthday'][00]);
body.replaceText("{sr}", info['Services Required'][0]);
body.replaceText("{stf}", info['Staff Requested'][0]);
body.replaceText("{pri}", info['Priority'][0]);
body.replaceText("{ref}", info['Referral'][0]);
body.replaceText("{jc}", info['Jane Consent'][0]);