How to connect Google Sheets to Database

前端 未结 8 1688
[愿得一人]
[愿得一人] 2020-12-23 02:31

I\'m trying to auto-populate some raw data on a sheet in my google sheets file with a query.

It doesn\'t look like sheets has any built in functionality to do so lik

相关标签:
8条回答
  • 2020-12-23 02:56

    The answer really depends on the database you are using. I'll mention the solutions not already addressed above:

    1. Zapier provides nifty integrations with databases like Typeform, Gmail emails, facebook leads, Trello cards, Slack, Mailchimp, Clickfunnels, Survey Monkey, Asana, Airtable data, Twitter, and many many more etc.

    2. If you want to connect to Mongodb, use Mongodb stitch, as mentioned here: https://www.mongodb.com/blog/post/stitching-sheets-using-mongodb-stitch-to-create-an-api-for-data-in-google-sheets

    3. Postgreql to Sheets: Zapier integration (https://zapier.com/apps/google-sheets/integrations/postgresql) or actiondesk/kloud integration (https://www.actiondesk.io/blog/4-tools-to-connect-postgresql-to-google-sheets)

    4. If it is a custom. DB, you can always develop your own plugin using the APIs mentioned here: https://developers.google.com/sheets/api

    Which database do you want to connect to?

    0 讨论(0)
  • 2020-12-23 02:57

    As referred here, 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.

    Read from the database

    This example demonstrates how to read a large number of records from the database, looping over the result set as necessary.

    // Replace the variables in this block with real values.
    var address = 'database_IP_address';
    var user = 'user_name';
    var userPwd = 'user_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();
      stmt.setMaxRows(1000);
      var results = stmt.executeQuery('SELECT * FROM entries');
      var numCols = results.getMetaData().getColumnCount();
    
      while (results.next()) {
        var rowString = '';
        for (var col = 0; col < numCols; col++) {
          rowString += results.getString(col + 1) + '\t';
        }
        Logger.log(rowString)
      }
    
      results.close();
      stmt.close();
    
      var end = new Date();
      Logger.log('Time elapsed: %sms', end - start);
    }
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-23 03:04

    You might want to have a look at this list of detailed article "the best 7 ways to connect MySQL to Google Sheets in 2020. Some of them require no code whatsoever and others are more technical, so that you can make a choice based on your needs and skills.

    Here is the list of solutions (you'll find a presentation of each in the article):

    1. Google Apps Script (there are 2 main steps here: 1. create a Google Sheets script to import a SQL table ; 2. run a Google Sheets script automatically every minute to retrieve SQL data => I wrote a step-by-step tutorial on how to automatically retrieve data from my SQL database to Google Sheets with a script — with the actual code to make it work)
    2. Zapier
    3. Blockspring
    4. Actiondesk
    5. Kloudio
    6. SeekWell
    7. QueryClips

    I hope this thorough presentation will help you find a solution that will simplify your life with MySQL and help make you and your team more efficient and productive!

    0 讨论(0)
  • 2020-12-23 03:06

    This code works well: to connect Azure database and grab data from table

    function onOpen() {
    
    var spreadsheet = SpreadsheetApp.getActive();
    
    var menuItems = [
        {name: 'Get Data', functionName: 'readData'}
    ];
    	spreadsheet.addMenu('Report', menuItems);
    }
    
    
    // Replace the variables in this block with your values.
     var hostName = 'SERVER.database.windows.net:1433;'
     var db = 'DBNAME;';
     var user = 'USER@SERVER';
     var userPwd = 'PASSWORD';
    
    
    
     var dbUrl = 'jdbc:sqlserver://'+hostName + 'databaseName='+db; 
    
    
    function readData() {
     var conn = Jdbc.getConnection(dbUrl, user, userPwd);
     var stmt = conn.createStatement();
    
      
     // Place your query below
     var results = stmt.executeQuery('SELECT TOP (10) * FROM [dbo].[NAME]');
     var metaData=results.getMetaData();
     var numCols = metaData.getColumnCount();
     var sheet = SpreadsheetApp.getActiveSheet();
    
     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);
    
    }
    
    results.close();
    stmt.close();
    
    sheet.autoResizeColumns(1, numCols+1);
    
    }

    0 讨论(0)
  • 2020-12-23 03:09

    This is the sample code to how read data from a SQL Server instance and insert them in Google Sheet. The code creates a menu item to re-load data, and each time clears the content while it keeps the format.

    function onOpen() {
    
    var spreadsheet = SpreadsheetApp.getActive();
    
    var menuItems = [
        {name: 'Get Data', functionName: 'readData'}
    ];
    spreadsheet.addMenu('My Functions', menuItems);
    }
    
    
    // Replace the variables in this block with real values.
    var address = 'ip-address:port'; //ex. '10.1.1.1:1433'
    var user = 'db-username';
    var userPwd = 'db-password';
    var db = 'db-name';
    
    var dbUrl = 'jdbc:sqlserver://' + address + ';databaseName=' + db;
    
    
    function readData() {
     var conn = Jdbc.getConnection(dbUrl, user, userPwd);
    
    
     var stmt = conn.createStatement();
    
     var results = stmt.executeQuery('SELECT * FROM [dbo].[User]');
     var metaData=results.getMetaData();
     var numCols = metaData.getColumnCount();
    
     var spreadsheet = SpreadsheetApp.getActive();
     var sheet = spreadsheet.getSheetByName("Sheet1");
     //you can use the following line to get the active sheet
     //var sheet = SpreadsheetApp.getActiveSheet();
    
    
    
     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);
    
    }
    
    results.close();
    stmt.close();
    
    sheet.autoResizeColumns(1, numCols+1);
    
    
    }
    
    0 讨论(0)
  • 2020-12-23 03:09

    Abielita answer works nice

    I'm only add some extra lines in order to write the data into the active sheet.

    // Replace the variables in this block with real values.
    var address = 'host';
    var user = 'user';
    var userPwd = 'password';
    var db = 'dbname';
    
    var dbUrl = 'jdbc:sqlserver://'+address+";databaseName="+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();
      stmt.setMaxRows(1000);
      var results = stmt.executeQuery('select * from TableName');
      var numCols = results.getMetaData().getColumnCount();
    
       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();
    
      var end = new Date();
      Logger.log('Time elapsed: %sms', end - start);
    }
    
    0 讨论(0)
提交回复
热议问题