Get Data from SQL to Google Sheet

I need to create reports for different parties in my work organization. The easiest way to process the data in a spreadsheet is to do this. Recently, I started to explore Google Script and found an article on how to import data from mySQL/SQL server to a Google sheet. Here’s an example of what I write in the script.
function YourFunction() var server = ‘[SERVER_ADDRESS_OF_YOUR_SQLSERVER]’;var port = [YOUR_DB_PORT];var dbName = ‘[DATABASE_NAME]’;var username = ‘[YOUR_USERNAME]’;var password = ‘[YOUR_PASSWORD]’;var url = ‘jdbc:sqlserver://’+server+’:’+port+’;databaseName=’+dbNamevar conn = Jdbc.getConnection(url, username, password)var stmt = conn.createStatement();var results = stmt.executeQuery(‘[YOUR_QUERY]’);var metaData=results.getMetaData();var numCols = metaData.getColumnCount();var spreadsheet = SpreadsheetApp.getActive();var sheet = spreadsheet.getSheetByName(‘[YOUR_SHEET_NAME]’);sheet.clearContents();var arr=[];for (var col = 0; col < numCols; col++) arr.push(metaData.getColumnName(col + 1));sheet.appendRow(arr);while (results.next()) arr=[];for (var col = 0; col < numCols; col++) arr.push(results.getString(col + 1));sheet.appendRow(arr);reslts.close();stmt.close();sheet.autoResizeColumns(1, numCols+1);//} Your query results will be saved to your Sheet. You can then use your Favorite Spreadsheet Application.

Related Posts