Member-only story
#001 Download {Type: Google Script}
You can write data from MySQL database to Google spreadsheet auto with just click of a button or scheduled time. This is great use for retrieving data in spreadsheet format.
Please find below script:
function myMySQLFetchData()
{
var conn = Jdbc.getConnection('jdbc:mysql://127.0.0.1:3306/employee_db', 'username', 'pass');
Replace the place holder credentials for MySQL, Also change MySQL query as per your MySQL database.
host : 127.0.0.1
database: employee_db
username: username
pass: password
var stmt = conn.createStatement();
var start = new Date();
capture script starting time
var rs = stmt.executeQuery('SELECT id,emp_name, emp_code FROM employee_details GROUP BY 1 LIMIT 1000');
It sets the limit of the maximum number of rows in a Result Set object
Change table name as per your database structure
var doc = SpreadsheetApp.getActiveSpreadsheet();
Returns the currently active spreadsheet, or you can hardcode this.
var cell = doc.getRange('a1');
var row = 0;
var getCount = rs.getMetaData().getColumnCount();
This code will count Mysql table column name.
for (var i = 0; i < getCount; i++)
{
cell.offset(row, i).setValue(rs.getMetaData().getColumnName(i+1));
}