r/GoogleAppsScript • • Sep 19 '20

Question Connecting Google Sheet via Apps Script to Google Cloud SQL Server

I am currently hosting a SQL Server (2017) in Google Cloud. I can connect from my home machine via SSMS to my cloud instance without issue. Obviously, I don't want to open this to users. As they're already doing their work in Google Sheets, I want the their experience to remain (mostly) the same.

Using Google Apps Script, I can NEVER establish a connection to the same instance. I followed this article. I understand that I need to use Jdbc.getConnection instead of getCloudSqlConnection . I also added the Apps Script IP ranges to my instance (I even found a 2nd article with two additional IP ranges that aren't included in the above article).

I'm using the Public IP for the connection as the instance name will result in a response that you need to use the cloud method.

Talking to others and reading posts some folks say to just use MySQL. As a test I created a MySQL instance as well and the root user is set to allow any host.

Regardless of Cloud Instance, Jdbc method, users, etc everything results in "Failed to establish database connection".

Thoughts or experience that have helped you overcome a similar situation?

Thanks!

Edit: I had the incorrect syntax for jdbc:sqlserver:// despite seeing it in other folks posts and projects. I had to use:

var user = 'userID'
var userPwd = 'userPwd'
var url = 'jdbc:sqlserver://publicIP;databaseName=dbName'

/**
* Create a new database within a Cloud SQL instance.
*/
function createTable()
{
var conn = Jdbc.getConnection(url, user, userPwd);
conn.createStatement().execute('create table test (id int)');
}

Also, I'm unsure how to update the post flair, if there's something for that..

6 Upvotes

1 comment sorted by

1

u/howMuchCheeseIs2Much Jan 13 '21

I found dealing with SQL in Sheets a big enough pain that I created SeekWell. It makes it dead simple to connect to your database and send data to Sheets (also handles scheduling refreshes).