Need help with my simple script

95 views
Skip to first unread message

Leandro Figueira

unread,
Oct 2, 2024, 1:57:54 AM10/2/24
to Google Apps Script Community
hello all!

I need help with the script I use to get responses from Google Forms and save each response individually in a .txt file inside a folder on My Google Drive.

I am getting an error on line 3. Could you please help?

This is the script:

function onFormSubmit(e) {
  // Getting responses from the form
  var responses = e.response.getItemResponses();
 
  // Getting responses from each question
  var subject = responses[0].getResponse();  
  var guest = responses[1].getResponse();  
 
  // IDs da pasta do Google Drive onde os arquivos serão salvos
  var folderId = "MY_FOLDER_ID";  // ID for Google Drive folder in order to save files
  var folder = DriveApp.getFolderById(folderId);
 
  // To save / replace txt files for new forms
  function saveOrReplaceFile(fileName, content) {
    var files = folder.getFilesByName(fileName);
   
    // If files exist, replace them
    if (files.hasNext()) {
      var file = files.next();
      file.setTrashed(true);
    }
   
    // To create a new file
    folder.createFile(fileName, content);
  }

  // to save subject.txt
  saveOrReplaceFile("subject.txt", subject);
 
  // to save guest.txt"
  saveOrReplaceFile("guest.txt", guest);
}

Fabrice Faucheux

unread,
Oct 12, 2024, 5:54:48 AM10/12/24
to Google Apps Script Community
Hello Leandro,

The error you’re seeing on line 3 likely arises from the structure of the trigger event object e. Specifically, it looks like the code is trying to access e.response, which is not valid. In the case of the onFormSubmit trigger, the correct property is e.values, which contains an array of the submitted responses in the same order as the form’s questions.

Here’s how you can fix the script by accessing the form responses properly:

function onFormSubmit(e) {
// Getting responses from the form
var responses = e.values;
// Getting responses from each question
var subject = responses[1]; // The first question's response (index 1 because index 0 is the timestamp)
var guest = responses[2]; // The second question's response
// ID for Google Drive folder in order to save files
var folderId = "MY_FOLDER_ID";
var folder = DriveApp.getFolderById(folderId);
// To save / replace txt files for new forms
function saveOrReplaceFile(fileName, content) {
var files = folder.getFilesByName(fileName);
// If files exist, replace them
if (files.hasNext()) {
var file = files.next();
file.setTrashed(true);
}
// To create a new file
folder.createFile(fileName, content);
}

// Save subject.txt
saveOrReplaceFile("subject.txt", subject);
// Save guest.txt
saveOrReplaceFile("guest.txt", guest);
}

Key Changes:

  • Replaced e.response.getItemResponses() with e.values to correctly access the form responses.
  • Adjusted the indices for subject and guest (starting at index 1) because e.values[0] corresponds to the timestamp.

This should resolve your error and allow the script to save the form responses as .txt files in your Google Drive folder. Let me know if you need further adjustments!

Fabrice

Ed Sambuco

unread,
Oct 12, 2024, 3:51:01 PM10/12/24
to google-apps-sc...@googlegroups.com
No.  The code structure is malformed.  The invocations of the SaveorReplaceFile need to be moved up into the onSubmitForm function.

"Ce message et toutes les pièces jointes (ci-après le "message") sont établis à l'intention exclusive de ses destinataires et sont confidentiels. Si vous recevez ce message par erreur, merci de le détruire et d'en avertir immédiatement l’expéditeur. Toute utilisation de ce message non conforme à sa destination, toute diffusion 
ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse. L'internet ne permettant pas d'assurer l'intégrité de ce message, nous déclinons toute responsabilité au titre de ce message, dans l’hypothèse où il aurait été modifié."

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/94d0514d-5bb9-4096-8e77-c0b318f31d1fn%40googlegroups.com.

HOPE VISION SERVICE

unread,
Oct 14, 2024, 4:38:52 PM10/14/24
to Google Apps Script Community

I'm designing a website using Google Apps Script and I need help setting a background image in HTML. I've tried various codes, but the image is not displaying. any one please help me with codes

Wilson Galter

unread,
Oct 15, 2024, 7:30:14 AM10/15/24
to google-apps-sc...@googlegroups.com
I would add a try and catch, re-use the saveOrReplacefile and logs to track the successes and errors.


function onFormSubmit(e) {
  try {
    // Get form responses
    var formResponses = e.values;
   
    // Extracting each response
    var subjectResponse = formResponses[1];  // First question's response
    var guestResponse = formResponses[2];    // Second question's response
   
    // Google Drive folder ID to save files

    var folderId = "MY_FOLDER_ID";  
    var folder = DriveApp.getFolderById(folderId);

    // Save or replace a file in the folder
    function saveOrReplaceFile(fileName, content) {
      var existingFiles = folder.getFilesByName(fileName);
     
      // Replace the existing file if it exists
      if (existingFiles.hasNext()) {
        var existingFile = existingFiles.next();
        existingFile.setTrashed(true);  // Trash the old file before creating a new one
      }
     
      // Create a new file with the given content
      folder.createFile(fileName, content);
      Logger.log("File " + fileName + " saved successfully.");
    }

    // Save responses as text files
    saveOrReplaceFile("subject.txt", subjectResponse);
    saveOrReplaceFile("guest.txt", guestResponse);

  } catch (error) {
    Logger.log("Error: " + error.message);
  }
}

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages