Search code examples
urlfetchgoogle-sheets-apigoogle-fusion-tables

import private google fusion table to google docs spreadsheet


I want to build a chart to google fusion table. I know there is an option to do it with fusion table but I need to do that using google spreadsheet.

How do I import a private fusion table to a spreadsheet?

function getdata(authToken) {
      query = encodeURIComponent("SELECT * FROM tableid");
      var URL = "http://www.google.com/fusiontables/api/query?sql=" + query;
      var response = UrlFetchApp.fetch(URL, {
         method: "get",
         headers: {
              "Authorization": "GoogleLogin auth=" + authToken,
         }
      });
      return response.getContentText();
}

The code above gives me the table headers only.


Solution

  • After a deep research, finally i figured it out after a deep search and reading here. This is how it looks for the code google docs spreadsheet app script:

    function onOpen()
    {
      var tableID = '00000' // Add the table ID of the fusion table here
      var email = UserProperties.getProperty('email');
      var password = UserProperties.getProperty('password');
    
      if (email === null || password === null) {
        email = Browser.inputBox('Enter email');
        password = Browser.inputBox('Enter password');
        UserProperties.setProperty('email',email);
        UserProperties.setProperty('password', password);
      } else {
        email = UserProperties.getProperty('email');
        password = UserProperties.getProperty('password');
      }
    
      var authToken = getGAauthenticationToken(email,password);
      query = encodeURIComponent("SELECT * FROM tableID");
      var URL = "http://www.google.com/fusiontables/api/query?sql=" + query;
    
      var response = UrlFetchApp.fetch(URL, {
         method: "get",
         headers: {
              "Authorization": "GoogleLogin auth=" + authToken,
         }
      });
      var tableData = response.getContentText();
      var o  = Utilities.parseCsv(response.getContentText());
      var doc = SpreadsheetApp.getActiveSpreadsheet();
      var cell = doc.getRange('a1');
      var index = 0;
      for (var i in o) {
        var row = o[i];
        var col = 0;
        for (var j in row) {
          cell.offset(index, col).setValue(row[j]);
          col++;
        }
        index++;
      }
    }
    
    function getGAauthenticationToken(email, password) {
      password = encodeURIComponent(password);
      var response = UrlFetchApp.fetch("https://www.google.com/accounts/ClientLogin", {
          method: "post",
          payload: "accountType=GOOGLE&Email=" + email + "&Passwd=" + password + "&service=fusiontables&Source=testing"
      });
      var responseStr = response.getContentText();
      responseStr = responseStr.slice(responseStr.search("Auth=") + 5, responseStr.length);
      responseStr = responseStr.replace(/\n/g, "");
      return responseStr;
    }
    

    After that you can do whatever you want in the spreadsheet. BTW, I still think there is a simple way to import a private table into a spreadsheet automaticly.