Google form: Can you explain the process of saving form responses in an array and sending them to a cloud system via a POST request using JavaScript or Google App script?

I am currently working on a project where I need to store Google Form responses in an array instead of a Google Sheet. After collecting the responses, I plan to extract specific elements from the array and use them to create a new project through an API. Although I have searched online for a solution, the code I found is posing difficulties in accessing only the necessary elements for the post request.

Below is the code snippet that fetches the form responses upon submission:

function captureFormResponse() 
{
    var form = FormApp.openById('form-id');
    var formResponses = form.getResponses();
    for (var i = 0; i < formResponses.length; i++)
    {
      var formResponse = formResponses[i];
      var itemResponses = formResponse.getItemResponses();
      for (var j = 0; j < itemResponses.length; j++) 
      {
        var itemResponse = itemResponses[j];
        Logger.log('Response #%s to the question "%s" was "%s"',
        (i + 1).toString(),
        itemResponse.getItem().getTitle(),
        itemResponse.getResponse());
      }
    }
  return formResponses;
 }

Additionally, here is the code for the POST request that should fetch data from the previous function. Specifically, I aim to retrieve the project name, client name, start date, and end date from the form submission and pass this information directly to the post request.

var data = {
                'name': lastRow[0][2],
                'client': lastRow[0][5],
                'starts_at': lastRow[0][7],
                'ends_at': lastRow[0][8],
                'project_state': "Tentative",

            };
            var payload = JSON.stringify(data);
            var options = {
                'method': 'POST',
                'Content-Type': 'application/json',
                'payload': data,
            };

        }
        var url = TK_URL + 'auth=' + TOKEN
        var response = UrlFetchApp.fetch(url, options);
        if (response.getResponseCode() === 200) {
            var json = JSON.parse(response);
            var id = json["id"];

If anyone can provide guidance on how to structure the code to seamlessly capture responses upon submission and forward them to the post request, it would be greatly appreciated.

Answer №1

By saving all data from the Linked Sheet into an array accessible throughout the script via PropertiesService labeled as 'myArray', you can ensure easy retrieval and usage.

function onFormSubmit(e) {
  let obj=PropertiesService.getScriptProperties().getProperties();
  obj.myArray=JSON.stringify(e.range.getSheet().getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).getDisplayValues());
  PropertiesService.getScriptProperties().setProperties(obj);
}

A concern to note is that PropertiesService may have a storage limit of roughly 30KB, necessitating potentially storing data in a file if exceeded. Additionally, remember to parse the data back into an object when retrieving it for use.

If you only need to save the current submission, this simplified function should suffice:

function onFormSubmit(e) {
  let obj=PropertiesService.getScriptProperties().getProperties();
  obj.myArray=JSON.stringify(e.values);
  PropertiesService.getScriptProperties().setProperties(obj);
}

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

Having trouble displaying Firebase Firestore data when using getStaticProps in Next.js?

I'm currently facing an issue while fetching data from Firebase Firestore using Next.js's getStaticProps method. When I try to utilize getStaticProps, I encounter the following error: res.json() is not a function This is how my implementation ...

maximizing the benefits of async/await for improved productivity

While it's not recommended to use await in loops, I'm facing a unique challenge that I can't seem to efficiently solve. My goal is to achieve a final output for the variable values structured like this: { jobId1: [[..], [..], [..], [..]], ...

Is there a way to transform a value into a time format using AngularJS?

I am having an issue with the Ionic Timepicker. When I choose a time, it returns values like 75600 (for example, when I select 09:00pm). How can I retrieve a human-readable string instead of an epoch timestamp? Below is the code snippet: $scope.timeP ...

The behavior of Angular ngif remains consistent regardless of the variable

My service successfully loads a list of people from a local database, including a functional filter. However, I am having trouble displaying a loading wheel when applying the filter. Upon initializing my AppComponent, I set a variable called "loadingData" ...

FullCalendar jQuery caught in an endless loop

After successfully implementing drag and drop deletion, I encountered a new issue. Whenever I delete an event, the removal process functions properly but then the code gets stuck in a loop within the eventDragStop function causing the calendar to freeze ...

Utilizing CSS files to incorporate loading icons in a component by dynamically updating based on passed props

Is it possible to store icons in CSS files and dynamically load them based on props passed into a component? In the provided example found at this CodeSandbox Link, SVG icons are loaded from the library named '@progress/kendo-svg-icons'. Instea ...

End the SQL connection in Nodejs

I'm having trouble closing the connection for a query using connection.close(). Does anyone know how to properly close the connection inside a route file? var express = require('express'); var router = express.Router(); var connection = req ...

Accumulating database data into an organized array

My task involves counting the number of orders in each month based on order dates stored in a database column. Currently, my code uses a switch statement to achieve this, but I am aware that it can be done in a more efficient and precise manner. The desire ...

Issue with Mootools Ajax call and form submission

I'm dealing with a table that displays data from a database. I'm trying to implement a way to (a) delete rows from the table and (b) edit the content of a row in real-time. Deleting rows is working perfectly, but editing the content is proving to ...

Animating the addition of the final child to the beginning

How can I animate the movement of a parent element's children arranged in a grid, going from the last item to the first item by using the .append() method or another technique? ...

Troubleshooting Angular 2 Fallback Route Failure

My current project is using Angular 2 Webpack Starter but I am having trouble with the fallback route. In my app.routes.ts file, I have defined the routes as follows: import { Routes } from '@angular/router'; import { HomeComponent } from &apos ...

Guide on changing the color of an input box based on the variable size in ReactJs!

I need help with a disabled input box that receives a variable x, which can be either < 1 or > 1. My goal is to change the background color to red if x > 1, green if x < 1, and grey if there's no value provided. Here is what I have attempt ...

Is there a way for me to consolidate the properties of a complex nested object into the top level of an array of objects through either moving or summing them up?

I have a collection array that looks like this: const collection = [ { "name": "Top1", "data": [ { "name": "shahnshah", "data": [ { ...

Organizing lists with HTML unordered lists

Is it possible to sort list items by numbers within a strong tag using JavaScript code? The current code successfully sorts the numbers, but removes them from the div tag. (The JavaScript code used below is for sorting by Name and works properly when &apos ...

Tips for choosing only 1 single row from a Table with Jquery

I have created a table to display multiple records, and I am looking to add update functionality using Ajax. However, the current code I have written makes all rows editable when clicked, whereas I only want to edit the specific row that was clicked on. C ...

Leverage AngularJS $http.get method to continuously fetch JSON array upon scrolling

Here is a snippet of code that utilizes AngularJS to retrieve a JSON response. I am looking for assistance in implementing a functionality where the page should make additional requests when the user scrolls to the bottom, continuing until the JSON array ...

Repeated action of rows with the same value

I am currently rebuilding this module within my application using AntDesign. However, I am looking to add a duplicate function that captures the values entered as well. https://i.sstatic.net/IvdqW.png Here is my code snippet, but it seems to not be funct ...

Ways to implement the tabIndex attribute in JSX

As per the guidelines provided in the react documentation, this code snippet is expected to function properly. <div tabIndex="0"></div> However, upon testing it myself, I encountered an issue where the input was not working as intended and ...

What is the process for updating my API data with information submitted through a form?

I am encountering a challenge with my Products component that fetches data from an API endpoint. I also have a Form component where users can input data to update the Products component, displaying both fetched and new data. How can I achieve this? I passe ...

How can you create a jQuery fade in effect for a single <li> element

I'm in the process of developing a task management app that generates a new li element every time a user adds an item. However, I am facing an issue where fadeIn() is activating for every li on the page whenever a new item is added. Does anyone have s ...