Articles on: Forms

Automate Form Data Collection Using Google Sheets

A step-by-step guide on how to collect data using PDF Generator API Forms integrated with Google Sheets. This integration lets you populate your spreadsheet with data one row at a time. Once you're done with this guide, every customer submission from your questionnaire will be automatically recorded in your Google Sheets.


This guide assumes you have already created a form in PDF Generator API. For this integration to work correctly, your form must contain fields with the following exact Field Names: name, surname, email, company, position and phone.


For a guide on creating forms, please see our support article How to Create a Form.


Step 1: Set Up Your Spreadsheet


First, open your Google Sheet (or create a new one). It's a best practice to label your columns in the first row to keep your data organized. Make sure the headers match the data the script will add:


Step 2: Open Apps Script


In your Google Sheet, go to Extensions > Apps Script. This will open a new script editor tab where you'll place the automation code.


Step 3: Paste the Script


Delete any default code in the Code.gs file. Then, copy and paste the entire script below.


IMPORTANT: In order this script to interact with your Form, you need to use exact fields like we did in our example: name; surname; email; company; position. Follow our support article on HOW TO CREATE A FORM.


// This function runs when the script's URL receives an HTTP POST request.
function doPost(e) {

try {
// Get the raw JSON string from the request body.
const jsonString = e.postData.contents;

// Parse the JSON string into a JavaScript object.
const data = JSON.parse(jsonString);

// Access the nested data from the 'form_data' object.
const formData = data.form_data;
const name = formData.name || 'N/A';
const surname = formData.surname || 'N/A';
const company = formData.company || 'N/A';
const position = formData.position || 'N/A';
const email = formData.email || 'N/A';
const phone = formData.phone || 'N/A';

// Get the form ID from the top-level object.
const formId = data.form_public_id || 'N/A';

// Get the current timestamp.
const timestamp = new Date();

// Get the target sheet.
// ⚠️ IMPORTANT: Change "Sheet1" if your sheet tab has a different name!
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");

// Append the data as a new row. The order here is important.
sheet.appendRow([
timestamp,
name,
surname,
email,
company,
position,
formId,
phone
]);

// Return a success response.
return ContentService
.createTextOutput(JSON.stringify({ "status": "success", "message": "Record added." }))
.setMimeType(ContentService.MimeType.JSON);

} catch (error) {
// If any error occurs (e.g., malformed JSON), log it and return an error response.
Logger.log(error.toString());
return ContentService
.createTextOutput(JSON.stringify({ "status": "error", "message": error.toString() }))
.setMimeType(ContentService.MimeType.JSON);
}
}


Step 4: Deploy the Script as a Web App ⚙️


Step 5: Authorize and Copy the URL


  • Once deployed, a new window will appear with your Web app URL. This is your callback URL. Copy this URL.


Step 6: Connect Your Form


  • Navigate back to your form settings in the PDF Generator API dashboard.
  • Paste the Web app URL you just copied into the Callback URL field.
  • Ensure the "Send data and document to third party service" toggle is enabled.


Step 7: Collect Your Data! 🚀


That's it! Your integration is complete. Now, every time your form is submitted, the data will automatically appear as a new row in your Google Sheet. You can find a video tutorial below on how to collect data using PDF Generator API Forms integrated with Google Sheets.







Updated on: 04/09/2025

Was this article helpful?

Share your feedback

Cancel

Thank you!