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
![](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b05daa94f077c81dab8_5e39f1a5372fa32f3bd2cff8_STEP%25201.png)
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
![Create a new Google Sheets by typing sheet.new in your browser toolbar](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b05fc1bc63e64d631f4_5e472594cdb4887dff4034d9_Create%2520a%2520new%2520Google%2520Sheets%2520by%2520typing%2520sheet.new%2520in%2520your%2520browser%2520toolbar.png)
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.
![Script to automatically retrieve SQL data, Name your spreadsheet](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b05524b4ee12967fd90_5e4726306fdb67810ab7da9c_Doube%2520click%2520on%2520the%2520default%2520name%2520Untitled%2520spreadsheet%2520on%2520the%2520top%2520left%2520hand%2520corner.png)
![Script to automatically retrieve SQL data, Name your spreadsheet 2](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b05ba8866c767d8cfcb_5e47263a5d63258282d15873_Name%2520your%2520spreadsheet%2520by%2520replacing%2520it%2520with%2520the%2520name%2520you%2520want.png)
3. Click on Tools > Script editor, this will open a new tab with a url starting with https://script.google.com/.
![Script to automatically retrieve SQL data, editor](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b056521438da6d47dff_5e4726d95d6325002dd166ed_Click%2520on%2520Tools%2520%253E%2520Script%2520editor.png)
![Script to automatically retrieve SQL data, Editor](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b0533e73447fef7b785_5e4726e3eff2aa20af5a1104_This%2520will%2520open%2520a%2520new%2520tab%2520with%2520a%2520url%2520starting%2520with%2520script.google.com.png)
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.
![Script to automatically retrieve SQL data, Name your spreadsheet 3](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b0600a74d8d6d8cf189_5e47274deff2aa18595a12f7_Name%2520your%2520script%2520by%2520clicking%2520on%2520the%2520default%2520name%2520Untitled%2520project%2520on%2520the%2520top%2520left%2520hand%2520corner.%2520A%2520little%2520window%2520will%2520pop%2520up%2520where%2520you%2520can%2520write%2520the%2520name%2520you%2520choose.%2520Then%252C%2520click%2520on%2520the%2520OK%2520button.png)
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
![Script to automatically retrieve SQL data, Whitelist](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b0508173a2dcd70ad5e_5e47279dbc8c332944876844_In%2520order%2520to%2520create%2520a%2520database%2520connection%2520using%2520the%2520JDBC%2520service%2520you%2520must%2520whitelist%2520certain%2520IP%2520ranges%2520in%2520your%2520database%2520settings%2520to%2520allow%2520Apps%2520Script%2520to%2520access%2520it.png)
6. In the code area, you’ll see some code already written:
![Script to automatically retrieve SQL data, Code area](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b06207a8d8fb99562da_5e4727cceff2aaeba35a1481_In%2520the%2520code%2520area%252C%2520you%25E2%2580%2599ll%2520see%2520some%2520code%2520already%2520written.png)
7. Delete this code:
![Script to automatically retrieve SQL data, delete this code](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b06c2834540fc215805_5e474012eff2aa77bb5ace4b_Delete%2520this%2520code.png)
8. Copy and paste the code below in the code area:
![Script to automatically retrieve SQL data, copy paste sceenshot](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b06a3e50c22662a1491_5e4740b9bc8c3387d8880b05_Copy%2520and%2520paste%2520the%2520code%2520below%2520in%2520the%2520code%2520area.png)
9. Replace the following information with your own in the code:
![Script to automatically retrieve SQL data, replace code](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b06c54bd28091181484_5e474047bc8c336876880928_Copy%2520and%2520paste%2520the%2520code%2520below%2520in%2520the%2520code%2520area.jpeg)
10. Click on the Run button (the arrow pointing to the bug on its right). Or, in the menu, on Run > Run function > readData.
![Script to automatically retrieve SQL data, run](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b077eae5126d6fab9a1_5e585b7f7bf1d73e86557f9c_Click%2520on%2520the%2520Run%2520button%2520(the%2520arrow%2520pointing%2520to%2520the%2520bug%2520on%2520its%2520right).%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.
![Script to automatically retrieve SQL data, Review permissions](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b06bacc1e21fe2dc277_5e585bf3602883c91138a64a_A%2520little%2520window%2520will%2520pop%2520up%2520entitled%2520Authorization%2520required.%2520Click%2520on%2520the%2520Review%2520Permissions%2520button_blurred.jpeg)
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.
![Script to automatically retrieve SQL data, Click allow](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b0690512964bc7013da_5e585c877bf1d70684558508_You%2520might%2520need%2520to%2520click%2520in%2520order%2520to%2520choose%2520the%2520Google%2520account%2520you%2520are%2520using%2520to%2520run%2520your%2520script.%2520Click%2520on%2520the%2520Allow%2520button._blurred.jpeg)
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.
![Script to automatically retrieve SQL data, Running Function readData](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b060758707d28020aea_5e585cc76028834ea638b020_On%2520top%2520of%2520your%2520script%252C%2520you%25E2%2580%2599ll%2520see%2520a%2520little%2520message%2520stating%2520Running%2520function%2520readData%25E2%2580%25A6_blurred.jpeg)
14. When the message disappears, go back to your sheet and… VOILA! You’ll have retrieved your SQL data in your spreadsheet :)
![Script to automatically retrieve SQL data, results](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b06e125b84d0295271e_5e4742ef6fdb673ca0b8b684_You%25E2%2580%2599ll%2520have%2520retrieved%2520your%2520SQL%2520data%2520in%2520your%2520spreadsheet.png)
![excited. meme](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b0670b33a18aca3954a_5e4706429415a6b3a08a5e28_Create%2520a%2520Google%2520Sheets%2520script%2520to%2520import%2520a%2520SQL%2520table.gif)
![](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b0628e4eb28a3d01476_5e39f2310a4529160a89f5be_STEP%25202.png)
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();
![Script to automatically retrieve SQL data, add code to run every minute](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b06fc1bc68639d631f6_5e585d0b76125c25901f25b4_Just%2520add%2520this%2520piece%2520of%2520code%2520at%2520the%2520end%2520of%2520your%2520current%2520code_blurred.jpeg)
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.
![Script to automatically retrieve SQL data, run and authorize](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b06df34bc5b399a6cb9_5e585d8f7bf1d762a9558c17_Click%2520on%2520the%2520Run%2520button%2520(the%2520arrow%2520pointing%2520to%2520the%2520bug%2520on%2520its%2520right).%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…
![You got it. Meme](https://assets-global.website-files.com/5f8b0a1abe69652278dad51c/5f917b0706cbd8e3cd42b036_5e4706739415a6ddec8a5e8b_Run%2520a%2520Google%2520Sheets%2520script%2520automatically%2520every%2520minute%2520to%2520retrieve%2520SQL%2520data.gif)
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.