Skip to content

Instantly share code, notes, and snippets.

@ChaiyachetU
Last active August 17, 2019 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChaiyachetU/28bb25c1abc76bc239cf82e767bb8960 to your computer and use it in GitHub Desktop.
Save ChaiyachetU/28bb25c1abc76bc239cf82e767bb8960 to your computer and use it in GitHub Desktop.
function autoFillDoc(e) {
//e.values is an array of last google form values when submit
//see more at https://developers.google.com/apps-script/guides/triggers/events
var timeStamp = e.values[0];
var firstName = e.values[1];
var lastName = e.values[2];
var title = e.values[3];
//file is the template file
//see more at https://developers.google.com/apps-script/reference/drive/drive-app
var file = DriveApp.getFileById('your_file_id_here');
//make a copy of the template, name it, and optionally tell it what folder to live in
//file.makeCopy will return a Google Drive file object
//see more at https://developers.google.com/apps-script/reference/drive/file
var folder = DriveApp.getFolderById('your_folder_id_here');
var fileName = lastName + ',' + firstName;
var newFile = file.makeCopy(fileName, folder);
//the new file created, open it as a document by using its ID
//see more at https://developers.google.com/apps-script/reference/document/document-app
var newFileId = newFile.getId();
var doc = DocumentApp.openById(newFileId);
//change is in the body, we need to get that
//see more at https://developers.google.com/apps-script/reference/document/document
var body = doc.getBody();
//call all of our replaceText methods
//https://developers.google.com/apps-script/reference/document/body
body.replaceText('{{First Name}}', firstName);
body.replaceText('{{Last Name}}', lastName);
body.replaceText('{{Title}}', title);
//save and close the document to persist our changes
//see more at https://developers.google.com/apps-script/reference/document/document
doc.saveAndClose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment