I encountered an issue while trying to utilize e.value in a Google Sheet script, receiving a TypeError that states "Cannot read property 'Values' from undefined" on line 7 of the "Code" file

Here is a two-part query:

  1. I am trying to set up a script that will copy responses from a Google Form to another Google Sheet, where I can modify and reformat them before sending them via email. However, when using the current script with an onFormSubmit trigger, I keep encountering the error message:

TypeError: Cannot read property "Values" from undefined. (line 7, file "Code")

Below is the code snippet:

function formSubmitReply(e)
{    
var t = "1g-wIs6nGxu3mJYA1vKtPCxBLCsvh1upeVGbCokOOTIw"; 
var tName = "AggregationOutput";

//Get information from the form and set as variables
  var email = e.Values[2];
  var name = e.Values[3];

// Get template, copy it as a new temp, and save the Doc’s id
var tcopyId = SpreadsheetApp.openById(t).copy(tName+' for '+name).getId();

// Open the temporary document & copy form responses into template copy response sheet
var copyt = SpreadsheetApp.openById (tcopyId);
var copyts = copyt.getSheetByName('Resp');  

// Transfer Data from Form Responses to Temporary file
copyts.getRange('A3').setValue(name);

// Send a copy of the template in an email as an Excel file
var url = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + copyt.getId();  
var subject = 'Aggregaton Output for' + name;
var body = url;   
MailApp.sendEmail(email, subject, body);

// Delete the temporary file
DriveApp.getFileById(tcopyId).setTrashed(true);
}
  1. In relation to the above code, my second question pertains to dealing with skipped questions in the form. How should I handle this so that it doesn't affect the array from e.values? Considering that respondents may go back and edit their responses on the form before resubmitting, relying on the last row as an indicator may not always work. Your suggestions are welcomed.

Any assistance or advice provided would be greatly appreciated.

Answer №1

Here is a suggestion for Part 1:

function submitFormResponse(e)
{    
var id = "1g-wIs6nGxu3mJYA1vKtPCxBLCsvh1upeVGbCokOOTIw"; 
var nameOfFile = "AggregationOutput";

//Extract data from form submission and assign to variables
var responseItems = e.response.getItemResponses();

  var email = responseItems[2].getResponse();
  var name = responseItems[3].getResponse();

// Duplicate template, save new file ID
var copyId = SpreadsheetApp.openById(id).copy(nameOfFile+' for '+name).getId();

// Access the new document & paste form responses
var copiedFile = SpreadsheetApp.openById(copyId);
var copiedSheet = copiedFile.getSheetByName('Resp');  

// Copy Form Data to New File
copiedSheet.getRange('A3').setValue(name);

// Email template as an Excel file attachment
var attachmentUrl = "https://docs.google.com/feeds/download/spreadsheets/Export?key=" + copiedFile.getId();  
var subjectLine = 'Aggregaton Output for' + name;
var emailContent = attachmentUrl   
MailApp.sendEmail(email, subjectLine, emailContent);

// Delete temporary file
DriveApp.getFileById(copyId).setTrashed(true);
}

Answer №2

Question 1: The issue you are experiencing is a result of incorrect syntax, as the values should be in lowercase (all small, not Values).

  var email = e.values[2];
  var name = e.values[3];

Question 2: If a question is skipped, the response value will be blank. For example, if an email field is left empty, e.values[2] will still point to the email field in the form but will contain no value.

If the form has the option for later editing enabled, only the edited responses will appear in the e.values array. In the case where only the email ID is updated, e.values[2] will equal "updated Email ID" while e.value[0-1,3-end] will be empty or blank.

To determine whether a submission is a new entry or an edited entry, you can use e.range to identify where the responses will be added on the "Form Response" sheet. You can then replicate that range on your "resp" sheet to keep it updated in sync with the form response sheet.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Make sure that when a user clicks on the back button in their web browser, it

Designing a file selection menu where users can choose a file and view it in a text area on a new page, allowing for text editing and saving with a click of a button. Users should be able to return to the file selection menu by simply clicking the browser ...

Rendering an element in React Router Dom based on specific conditions

Starting a new project with the latest version of react-router-dom and following their recommendation to use createBrowserRouter. The goal is to display a different header based on the path parameters. Currently, I have set up an optional path parameter: ...

Can the pointerover event be managed on a container within the am5 library?

While attempting to add an HTML label to a map and trigger a pointerover event, I encountered issues. The objective was to change the HTML content upon hover. Despite trying to incorporate a tooltip, the hover event failed to work properly, and the tooltip ...

Having trouble with jQuery live clock freezing your browser?

I recently created a clock script and utilized setInterval to keep it running smoothly. However, I've encountered an issue where my browser freezes after a short period of time. Unfortunately, I'm unsure how to troubleshoot this problem on my own ...

Spring Boot fails to recognize path variable data sent from Angular

When working with Angular 7, I encountered a situation where I needed to pass a value from the service class of Angular. Here is how I achieved it: executeHelloWorldBeanServiceWithPathVariable(name){ console.log("name coming from here"+name); retu ...

The display of options in React Bootstrap typeahead is not functioning properly

When I try to implement React Bootstrap Typeahead, the options do not appear in the Typeahead component upon page load. Here is a snippet of my code: const React = require('react'); ... (code continues) The options are generated as follows: [ ...

Is there a way to effortlessly scroll an element when the CSS overflow property is designated as auto?

I need help with creating a testimonial section where two divs automatically scroll inside a fixed-height container. Currently, only one div is scrolling while the other remains static in my code. I have implemented JavaScript for automatic scrolling wit ...

Advantages and disadvantages of using Jaxer

While I have seen this question asked previously, it has been a while without any satisfactory responses... My attention has been caught by Aptana's Jaxer, and I must say, the concept is quite intriguing. For those unfamiliar with it, here's a ...

A guide on effectively mocking functions in Jest tests with Rollup.js

I am currently in the process of developing a React library called MyLibrary, using Rollup.js version 2.58.3 for bundling and jest for unit testing. Synopsis of the Issue The main challenge I am facing is with mocking a module from my library when using j ...

Issue with external JavaScript file being unresponsive on mobile browser

Hello everyone, hope you're having a great afternoon or evening I've encountered an issue with mobile browsers like Chrome Mobile and Kiwi where the external js file is not visible in the html file. The script.js file looks like this: alert(&ap ...

What is the best way to transfer data from a fully filled out JSON form to a PHP database

Is there a way to automatically send a completed JSON form when the device detects an internet connection? I am facing the challenge of dynamically generated IDs for each question and need to figure out how to submit the data to the database on button cl ...

The ID of the jQuery droppable element cannot be found

Is there a way to retrieve the ID of a li element from a droppable function? Currently, when I try to do so, it returns undefined. Any suggestions on why this might be happening? The structure of each li element is as follows: <li class="ui-state-def ...

I am unable to tap on the input field

Having a major issue here... I've encountered a problem with this website: sk.maze-it.dk When attempting to click on "BOOK MØDE" in the navigation bar and select an available date (e.g. 30) followed by clicking on "Book møde", two fields appear - o ...

Updating a table without the need for a button in PHP and AJAX with an Excel-inspired approach

I'm facing a scenario where I need to dynamically update the contents of a table row. To achieve this, I want the cell to transform into a text box when clicked. Here's how I implemented it: <td contenteditable></td> After making ch ...

Finding ways to access intricate JSON objects in Angular 2

//component.ts public items: any; getResult(){ var jsonBody = {}; jsonBody['activity'] = this.activity; jsonBody['seatnumber'] = this.seatNo; this.localService.postCall(jsonBody) .subscribe( data =& ...

Is there a way to extend the TTL expiresAfterSeconds duration?

Currently, I am working on a simple application and my model looks like this: import mongoose from "mongoose"; const urlSchema = new mongoose.Schema( { originalUrl: { type: String, required: true }, longUrl: { type: String, required: t ...

Using JavaScript to make an asynchronous call to the server via AJAX

Is it true that clients can block JavaScript in their browsers? If so, what impact does it have on AJAX calls - do they still function properly? ...

Res.write inexpressively proceeding without waiting for the completion of an array of promises

snippet const processAll = async (tasks) => { let processes = []; tasks.forEach(task => { if (task === "1") processes.push(asyncProcess1); if (task === "2") processes.push(asyncProcess2); if (task === "3&q ...

What is the best way to send multiple variables to a url using jQuery ajax?

I am having trouble passing multiple values in the method below. Can someone help me with the correct syntax? I have attempted data: ('keyword='+$(this).val(),'id='10), as well as data: {'keyword='+$(this).val(),'id=&a ...

Retrieve and showcase PHP results utilizing the power of jQuery/AJAX

I am working with a Leaflet map and a text input field. My goal is to extract the address from the text input, process it using a PHP script, and receive the results back via jQuery. Below is the form structure: <form id="mapcheck" method="POS ...