Search code examples
exceptiongoogle-apps-scripteventtrigger

Apps Script - Creating new doc on form submission failing and creating new blank pdf instead


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.


Solution

  • I believe your goal is as follows.

    • You want to create a new Google Document and want to put the created Document to targetFolder.

    Modification points:

    • In your script, I think that a Google Document is created in the root folder. But, by targetFolder.createFile(newDocFile);, the created Document is converted to PDF format and the PDF file is put to targetFolder. In the current stage, when Google Docs files (Document, Spreadsheet, Slide) are created by createFile, 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 createDocFromFormResponse is modified.

    function createDocFromFormResponse(formResponse) {
      var folderName = "Spa Job Log Repository";
    
      var parentFolder = DriveApp.getFolderById("###"); // Please set your folder ID.
    
      var targetFolder = parentFolder.getFoldersByName(folderName).next();
      if (targetFolder) {
        var newDoc = DocumentApp.create("Form Response Document");
        var newDocFile = DriveApp.getFileById(newDoc.getId());
        newDocFile.moveTo(targetFolder);
        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();
        // targetFolder.createFile(doc.getBlob()); // If you want to also create a PDF file into "targetFolder", please use this line.
      } else {
        Logger.log("Folder '" + folderName + "' not found.");
      }
    }
    

    Note:

    • In your script, at var parentFolder = DriveApp.getFolderById("Spa Job Log Repository");, if you use the folder name to getFolderById, an error occurs. Please be careful about this.

    Reference: