The following code is supposed to gather the responses from a submitted google form, and create a google doc and add them to it within a target folder. The code uses a trigger that activates on a form submission. However, it is currently only creating blank pdf files when the forum is submitted, what is the problem? The createTrigger function is what I used to create the trigger that runs the code on a form submission. I've messed with this for a long time but I only ever get it to create blank pdfs instead of google docs with the form data. I'm a way beginner at this stuff and really only messed with python, so take it with grain of salt.
function onFormSubmitTrigger() {
var formResponse = getLastFormResponse();
createDocFromFormResponse(formResponse);
}
function getLastFormResponse() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = sheet.getLastRow();
var lastRowValues = sheet.getRange(lastRow, 1, 1, sheet.getLastColumn()).getValues()[0];
return lastRowValues;
}
function createDocFromFormResponse(formResponse) {
var folderName = "Spa Job Log Repository";
var parentFolder = DriveApp.getFolderById("Spa Job Log Repository");
var targetFolder = parentFolder.getFoldersByName(folderName).next();
if (targetFolder) {
var newDoc = DocumentApp.create("Form Response Document");
var newDocFile = DriveApp.getFileById(newDoc.getId());
targetFolder.createFile(newDocFile);
var doc = DocumentApp.openById(newDoc.getId());
var body = doc.getBody();
for (var i = 0; i < formResponse.length; i++) {
body.appendParagraph("Response " + (i + 1) + ": " + formResponse[i]);
}
doc.saveAndClose();
} else {
Logger.log("Folder '" + folderName + "' not found.");
}
}
function createTrigger() {
var form = FormApp.openById('1jGAWZXcOHOfbM1uk88QMBg-yl4VdSHxynRM-NHawASQ');
ScriptApp.newTrigger('onFormSubmitTrigger')
.forForm(form)
.onFormSubmit()
.create();
}
The createTrigger function is what I used to create the trigger that runs the code on a form submission. I've messed with this for a long time but I only ever get it to create blank pdfs instead of google docs with the form data. I'm a way beginner at this stuff and really only messed with python, so take it with grain of salt.
I believe your goal is as follows.
targetFolder.Modification points:
targetFolder.createFile(newDocFile);, the created Document is converted to PDF format and the PDF file is put totargetFolder. In the current stage, when Google Docs files (Document, Spreadsheet, Slide) are created bycreateFile, the mimeType is automatically converted to PDF. I guessed that this is the reason for your current issue.When these points are reflected in your script, how about the following modification?
Modified script:
In this modification, the function
createDocFromFormResponseis modified.Note:
var parentFolder = DriveApp.getFolderById("Spa Job Log Repository");, if you use the folder name togetFolderById, an error occurs. Please be careful about this.Reference: