Home > Software design >  Auto Fill a Google Doc from a Google Form Submission and send it as pdf
Auto Fill a Google Doc from a Google Form Submission and send it as pdf

Time:10-24

Auto Fill a Google Doc from a Google Form Submission and send it as pdf, this is my code

function autoFillGoogleDocFromForm(e) {

  //e.values is an array of form values
  var timestamp = e.values[0];
  var firstName = e.values[1];
  var email = e.values[2];
  var note = e.values[3];


var tempfile = DriveApp.getFileById('1GfUEmx07Ev1YFubbuhpJOjYY0OHtMGoojUK92c0q_So'); 


var answerfolder = DriveApp.getFolderById('1x5EhuzXP9EgcJZBqsl_8uaniRDjz2hS1')
var copy = tempfile.makeCopy(firstName, answerfolder); 
var doc = DocumentApp.openById(copy.getId());

var body = doc.getBody(); 

body.replaceText('{{firstname}}', firstName); 
  body.replaceText('{{email}}', email);  
  body.replaceText('{{note}}', note); 
  doc.saveAndClose(); 



var subject = "thank you "   firstName;
var messageBody = "Hi "   firstName ;
 var waiver = DriveApp.getFolderById('1x5EhuzXP9EgcJZBqsl_8uaniRDjz2hS1').getFilesByName(firstName);
 var files = waiver.getAs('MimeType.PDF');
 
 
MailApp.sendEmail({
to:email,
subject:subject,
htmlBody: messageBody,
attachments: [files]
});


  }

but I get this error: Function: Time autoFillGoogleDocFromForm
Error Message: TypeError: waiver.getAs is not a function

enter image description here

CodePudding user response:

In your OP waiver is a file iterator. You need to get one of the files. Assuming there is only one file with the firstName, try this.

var waiver = DriveApp.getFolderById('1x5EhuzXP9EgcJZBqsl_8uaniRDjz2hS1').getFilesByName(firstName);
waiver = waiver.next();
var files = waiver.getAs('MimeType.PDF');
  • Related