Guest User

Untitled

a guest
Apr 20th, 2019
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. // MySQL to Google Spreadsheet By Pradeep Bheron
  2. // Support and contact at pradeepbheron.com
  3.  
  4. function myMySQLFetchData() {
  5.  
  6. var conn = Jdbc.getConnection('jdbc:mysql://127.0.0.1:3306/employee_db', 'username', 'pass'); // Change it as per your database credentials
  7.  
  8. var stmt = conn.createStatement();
  9. var start = new Date(); // Get script starting time
  10.  
  11. 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 nuber of rows in a ResultSet object
  12.  
  13. //change table name as per your database structure
  14.  
  15. var doc = SpreadsheetApp.getActiveSpreadsheet(); // Returns the currently active spreadsheet
  16. var cell = doc.getRange('a1');
  17. var row = 0;
  18. var getCount = rs.getMetaData().getColumnCount(); // Mysql table column name count.
  19.  
  20. for (var i = 0; i < getCount; i++){
  21. cell.offset(row, i).setValue(rs.getMetaData().getColumnName(i+1)); // Mysql table column name will be fetch and added in spreadsheet.
  22. }
  23.  
  24. var row = 1;
  25. while (rs.next()) {
  26. for (var col = 0; col < rs.getMetaData().getColumnCount(); col++) {
  27. cell.offset(row, col).setValue(rs.getString(col + 1)); // Mysql table column data will be fetch and added in spreadsheet.
  28. }
  29. row++;
  30. }
  31.  
  32. rs.close();
  33. stmt.close();
  34. conn.close();
  35. var end = new Date(); // Get script ending time
  36. Logger.log('Time elapsed: ' + (end.getTime() - start.getTime())); // To generate script log. To view log click on View -> Logs.
  37. }
Add Comment
Please, Sign In to add comment