Follow this step-by-step explanation to learn how to automatically retrieve data from your SQL database and import it into Google Sheets with a script that you can copy and paste right into Google Apps.
How to automatically import data from SQL to Google Sheets

Create a Google Sheets script to import a SQL table (here with MySQL)
If you want to connect your Google spreadsheet to MySQL, here I’ll share with you the code to read data from your SQL database (here with MySQL) and import the whole table in Google Sheets.
1. Create a new Google Sheets by typing sheet.new in your browser toolbar

2. Name your spreadsheet
Double click on the default name Untitled spreadsheet on the top left hand corner and replace it with the name you want. For instance, I chose to name it Test here.


3. Click on Tools > Script editor, this will open a new tab with a url starting with https://script.google.com/.


4. Name your Google script
To name your script, click on the default name Untitled project on the top left hand corner. A little window will pop up where you can write the name you choose. For instance, I chose to name it like my spreadsheet: Test. Then, click on the OK button.

5. Whitelist those addresses
Apps Script can connect to external databases through the JDBC service. It supports Google Cloud SQL, MySQL, Microsoft SQL Server, and Oracle databases. To update an external database with JDBC, your script must open a connection to the database and then make changes by sending SQL statements. In order to create a database connection using the JDBC service you must whitelist certain IP ranges in your database settings to allow Apps Script to access it. These are the address ranges you'll need to whitelist:
- 64.18.0.0 - 64.18.15.255
- 64.233.160.0 - 64.233.191.255
- 66.102.0.0 - 66.102.15.255
- 66.249.80.0 - 66.249.95.255
- 72.14.192.0 - 72.14.255.255
- 74.125.0.0 - 74.125.255.255
- 173.194.0.0 - 173.194.255.255
- 207.126.144.0 - 207.126.159.255
- 209.85.128.0 - 209.85.255.255
- 216.239.32.0 - 216.239.63.255

6. In the code area, you’ll see some code already written:

7. Delete this code:

8. Copy and paste the code below in the code area:

9. Replace the following information with your own in the code:

10. Click on the Run button (the arrow pointing to the bug on its right). Or, in the menu, on Run > Run function > readData.
.%2520Or%252C%2520in%2520the%2520menu%252C%2520on%2520Run%2520_%2520Run%2520function%2520_%2520readData_blurred.jpeg)
11. A little window will pop up entitled Authorization required. Click on the Review Permissions button.

12. You might need to click in order to choose the Google account you are using to run your script. Click on the Allow button.

13. On top of your script, you’ll see a little message stating Running function readData. Be patient, as it might take a while to appear.

14. When the message disappears, go back to your sheet and… VOILA! You’ll have retrieved your SQL data in your spreadsheet :)



Run a Google Sheets script automatically every minute to retrieve SQL data
Now I’ll show you the code to fetch and refresh your data every minute. Each time, it clears the existing content in the chosen sheet and replaces it with the database data while keeping the format you gave to the table in Google Sheets.
1. Just add this piece of code at the end of your current code:
ScriptApp.newTrigger('readData')
.timeBased()
.everyMinutes(1)
.create();

2. Click on the Run button (the arrow pointing to the bug on its right). If a small window pops up titledAuthorization required, allow it again.
.%2520Or%252C%2520in%2520the%2520menu%252C%2520on%2520Run%2520_%2520Run%2520function%2520_%2520readData_blurred%2520copy.jpeg)
That’s it! Now, you will import and automatically sync your data between your SQL database and Google Sheets. Yes, so simple and yet so powerful…

Conclusion
The reason you want to push SQL data from your database to Google Sheets is probably because you want to make this data accessible to team members who are confortable in a spreadsheet but maybe not confortable with SQL or with a Business Intelligence tool.
If that's the case, you should definitely try out Actiondesk. Actiondesk is the easiest way for startup teams to make better decisions with their data. You can access your company key data without code or writing SQL, you'll just need basic spreadsheet skills.
Actiondesk looks like a spreadsheet and works like a spreadsheet. Users used to Google Sheets and Excel will be able to learn Actiondesk very fast.
While writing a script or using a Google Sheets add on can be a good one off solution, they're usually not durable. The overwhelming feedback we got from the market is that these solutions are not reliable and you always have to debug them.
If you're looking for a more solid solution, check out Actiondesk, you can try it for free.