7

I have been wondering about this for a while. We are using Google Spreadsheets a lot with my colleagues and the thing is that we always have to write down data that we already have in our MySQL database.

I wonder if there is a possibility to connect your spreadsheet with MySQL server and fetch the data to specific rows via executable script or something.

5

You can use the JDBC services of Google Apps Scripts.

You will have to write a script that populates your spreadsheet with data from the JDBC service.

Not sure exactly what you want to do, but you could start with the example from the documentation:

function foo() {
  var conn = Jdbc.getConnection('jdbc:mysql://<host>:3306/<instance>', 'user', 'password');
  var stmt = conn.createStatement();
  stmt.setMaxRows(100);
  var start = new Date();
  var rs = stmt.executeQuery('select * from person');

  var doc = SpreadsheetApp.getActiveSpreadsheet();
  var cell = doc.getRange('a1');
  var row = 0;
  while (rs.next()) {
    for (var col = 0; col < rs.getMetaData().getColumnCount(); col++) {
      cell.offset(row, col).setValue(rs.getString(col + 1));
    }
    row++;
  }
  rs.close();
  stmt.close();
  conn.close();
  var end = new Date();
  Logger.log('Time elapsed: ' + (end.getTime() - start.getTime()));
}
1
  • How to do this with integrated authentication? – FMFF Dec 17 '19 at 17:18
1

Use the Google AppScript JDBC

Here is some sample code that can get you started.

 // Replace the variables in this block with real values.
    var address = 'Your DB Address Here';
    var user = 'Enter USER';
    var userPwd = 'YOUR PASSWORD';
    var db = 'DATABASE NAME';

    var dbUrl = 'jdbc:mysql://' + address + '/' + db;

    // Read up to 1000 rows of data from the table and log them.
   function readFromTable() {
      var conn = Jdbc.getConnection(dbUrl, user, userPwd);

      var start = new Date();
      var stmt = conn.createStatement();
      // Read up to 1000 rows of data from the table and log them.
      // stmt.setMaxRows(1000);
      var results = stmt.executeQuery('SELECT * FROM YOURTABLE');

      var sheet = SpreadsheetApp.getActiveSpreadsheet();
      var cell = sheet.getRange('A1');
      var numCols = results.getMetaData().getColumnCount();
      var row =0;

      while (results.next()) {
        var rowString = '';
        for (var col = 0; col < numCols; col++) {
          rowString += results.getString(col + 1) + '\t';
          cell.offset(row, col).setValue(results.getString(col +1 ));
        }
        row++
       Logger.log(rowString)
      }

      results.close();
      stmt.close();
      conn.close();

      var end = new Date();
      Logger.log('Time elapsed: %sms', end - start);
    }
1

SeekWell allows you to connect to databases and write SQL queries directly in Sheets and MySQL is included in the free tier:

A few other features:

  • Quickly view all tables and columns in a database and get summary stats on a column with one click
  • Query from the sidebar, a large pop-out window, or from within a cell
  • Results can be sent to a specific cell, scratch sheet or directly to a pivot table
  • Your query history is saved and viewable if you need to re-execute an older query
  • You can save a set of queries on a “Run Sheet” to update multiple reports at once

Disclaimer: I made this.

0
select * from table_name
into outfile '/somelocation/data.csv'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Launch google spreadsheet. Open the data.csv file Your data is imported for use in Google.

Or do you want a real time connection?

2
  • Well real time connection would be better, but I am not sure if it is possible. – Jan Alfréd Richter Apr 10 '13 at 8:53
  • I have not done it with google docs, but I have done it with google calendar. If you run a webserver with php it might be possible. Download the API get the developer ID from google and a bunch of other stuff. – cybernard Apr 10 '13 at 21:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.