Created
August 13, 2025 14:34
-
-
Save phillypb/1b9fa0ffab47b68c498475b96be4327b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function main(e) { | |
// get all data from Google Form submission | |
var formValues = e.namedValues; | |
// get email address | |
var emailAddress = formValues["Email address"][0]; | |
console.log("Email address is: " + emailAddress) | |
// get comma-separated list of file upload URLs | |
var files = formValues["Upload files"][0]; | |
console.log("List of uploaded file URLs: " + files); | |
// get template Doc for copying | |
var templateDoc = DriveApp.getFileById("1EmhWZT2ugvOid3Ihzi1sAB_nu3H9Q2-8RatcR9dwYQw"); | |
// get destination folder | |
var destFolder = DriveApp.getFolderById("1NhYzp9S1H4HviDEyO9N_erwuzQP11M2Q"); | |
// create name for new Doc | |
var docName = emailAddress + " - example Doc"; | |
// make copy and get URL | |
var newDocURL = templateDoc.makeCopy(docName, destFolder).getUrl(); | |
// open new Doc | |
var openDoc = DocumentApp.openByUrl(newDocURL); | |
// get Doc body | |
var docbody = openDoc.getBody(); | |
// replace email address | |
docbody.replaceText("<<emailAddress>> ", emailAddress); | |
// check some files have actually been uploaded before proceeding | |
if (files != "") { | |
// search for keyword tag | |
var searchResult = docbody.findText("<<fileUpload>>"); | |
// get the element from the result | |
var para = searchResult.getElement(); | |
// get the Parent element (twice) from the container we are in | |
var cell = para.getParent().getParent().asTableCell(); | |
// separate out the file upload URLs and action each one | |
files.split(', ').forEach((linkUrl) => { | |
// extract the ID from the URL | |
var fileId = linkUrl.match(/[-\w]{25,}/); | |
// get the file and then its name | |
var linkName = DriveApp.getFileById(fileId).getName(); | |
// append the file name and set it as a clickable link | |
cell.appendParagraph(linkName).setLinkUrl(linkUrl); | |
// append empty paragraph just for better spacing in the Doc | |
cell.appendParagraph(""); | |
}); | |
} else { | |
// do nothing as no file uploaded | |
}; | |
// remove the tag from the Doc so it is not there regardless of there being any files | |
docbody.replaceText("<<fileUpload>>", ""); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment