Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Form data to store in cloud SQL database

I'm new to Google Forms and Google Apps Script. I have ten Google Forms, and on submitting forms they populate a corresponding Google Sheet.

Now, here is what I want, on submitting form I want that information to also be stored in the cloud SQL database. What steps will accomplish that?

like image 222
Mehroz Irshad Avatar asked Nov 18 '15 18:11

Mehroz Irshad


3 Answers

The normal data flow for a Google Form that is accepting responses and replicating them to a Spreadsheet looks like this:

msc

You've got two opportunities for a Form Submission Trigger to replicate the form responses in your Cloud SQL database; you can either trigger from the Google Form Form submit event or the Google Sheets Form submit event.

Either way, you will have a script that gets called for every form submission, and an event object that contains the response values. Your trigger function should use the JDBC to connect to the database - that link includes an intro that walks you through the highlights.

Example

Say we have a form asking two questions, "Name" and "Age". That would result in 3 columns in our spreadsheet; "Timestamp" plus one for each question.

To match that, we have a Cloud SQL database set up with the same three columns.

A Google Sheets Form submit trigger function that wrote to a Cloud SQL database would look like this untested code:

// Replace the variables in this block with real values.
var address = 'database_IP_address';
var user = 'user_name';
var userPwd = 'user_password';
var db = 'database_name';

var dbUrl = 'jdbc:mysql://' + address + '/' + db;

// Receive form response and replicate to a row in SQL table
function handleFormSubmit( event ) {
  var conn = Jdbc.getConnection(dbUrl, user, userPwd);

  var stmt = conn.prepareStatement('INSERT INTO entries '
      + '(Timestamp, Name, Age) values (?, ?, ?)');
  stmt.setString(1, event.namedValues['Timestamp']);
  stmt.setString(2, event.namedValues['Name']);
  stmt.setString(3, event.namedValues['Age']);
  stmt.execute();
}
like image 145
Mogsdad Avatar answered Sep 24 '22 05:09

Mogsdad


Posing an example of a solution I have working. I modified Mogsdad's script to use the most up to date parameters & connection function, and fixed the syntax errors.

//Event is automatially passed on form submission when executed as a trigger. 
//It contains objects that the user submitted
function writeToCloudSQL(event) {
//Note we get subname from the 'Instance Connection Name' of the Overview tab in the CloudSQL portal
var subname = 'see image';
var user = 'user';
var userPwd = 'pwd';
var db = 'db_name';

var dbUrl = 'jdbc:google:mysql://' + subname + '/' + db;

var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);

var stmt = conn.prepareStatement('INSERT INTO tbl_users (Timestamp, DL_ID, DOB, Last4SSN, HTMLGathered) values (NOW(), ?, ?, ?, \'No\');');
stmt.setString(1, event.namedValues['DL_ID']);
stmt.setString(2, event.namedValues['DOB']);
stmt.setString(3, event.namedValues['Last4SSN']);

stmt.execute();

}

Locating the subname: Locating the subname

Read more about the Event parameter here:

https://developers.google.com/apps-script/guides/triggers/events

Read more about the connection string here:

https://developers.google.com/apps-script/reference/jdbc/jdbc#getCloudSqlConnection(String,String,String)

Also be sure you've allowed network access to google scripts

https://developers.google.com/apps-script/guides/jdbc?hl=en#using_jdbcgetconnectionurl

like image 31
Michael Capobianco Avatar answered Sep 24 '22 05:09

Michael Capobianco


Zapier with their SQL Server and Google Sheets integrations can do this. I'm not affiliated. https://zapier.com/apps/sql-server/integrations

like image 20
Michael Davidson Avatar answered Sep 22 '22 05:09

Michael Davidson