Guest User

Untitled

a guest
Nov 9th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 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 order_id, firstname AS first_name, lastname AS last_name, email, date_added AS order_date, CONCAT("£", FORMAT(total,2)) order_value, payment_address_1 AS billing_address_1, payment_address_2 AS billing_address_2, payment_city AS billing_city, payment_postcode AS billing_postcode, payment_country AS billing_country, items_purchased FROM (SELECT order_id, firstname, lastname, email, date_added, total, payment_address_1, payment_address_2, payment_city, payment_postcode, payment_country FROM ocbw_order GROUP BY order_id) AS A JOIN (SELECT order_id AS product_order_id, GROUP_CONCAT(name SEPARATOR ", ") AS items_purchased FROM ocbw_order_product GROUP BY product_order_id) AS B ON A.order_id=B.product_order_id WHERE date_added >= DATE_SUB(NOW(),INTERVAL 1 YEAR) AND firstname != ""'); // It sets the limit of the maximum nuber of rows in a ResultSet object
  12. //change table name as per your database structure
  13.  
  14. var doc = SpreadsheetApp.getActiveSpreadsheet(); // Returns the currently active spreadsheet
  15. var cell = doc.getRange('a1');
  16. var row = 0;
  17. var getCount = rs.getMetaData().getColumnCount(); // Mysql table column name count.
  18. var tempArray = []; // Create array to hold mysql data
  19.  
  20. // get row and column count for later
  21.  
  22. var colCount = getCount;
  23. rs.last();
  24. var rowCount = rs.getRow();
  25. rs.beforeFirst(); // resets rs cursor
  26.  
  27. // Build TempArray using MySQL data
  28. for (var i = 0; i < getCount; i++){
  29. tempArray[0][i] = rs.getMetaData().getColumnName(i+1);
  30. //cell.offset(row, i).setValue(rs.getMetaData().getColumnName(i+1)); // Mysql table column name will be fetch and added in spreadsheet.
  31. }
  32.  
  33. var row = 1;
  34. while (rs.next()) {
  35. for (var col = 0; col < rs.getMetaData().getColumnCount(); col++) {
  36. tempArray[row][col] = rs.getString(col + 1);
  37. //cell.offset(row, col).setValue(rs.getString(col + 1)); // Mysql table column data will be fetch and added in spreadsheet.
  38. }
  39. row++;
  40. }
  41.  
  42. // Fill Spreadsheet from tempArray
  43. for (var row = 0; row < rowCount; row++) {
  44. for (var col = 0; col < colCount; col++) {
  45. cell.offset(row, col).setValue(tempArray[row][col + 1]);
  46. }
  47. }
  48.  
  49. rs.close();
  50. stmt.close();
  51. conn.close();
  52. var end = new Date(); // Get script ending time
  53. Logger.log('Time elapsed: ' + (end.getTime() - start.getTime())); // To generate script log. To view log click on View -> Logs.
  54. }
Add Comment
Please, Sign In to add comment