Question

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.

Was it helpful?

Solution 2

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.

OTHER TIPS

Don't set each cell individually as in the example below unless you need to process each bit of data. Using this is about 10x faster:

var rows = o.length;
var columns = o[0].length;
cell.offset(<startrow>, <startcolumn>, rows, columns).setValues(o);

There's a nice article describing how to access Fusion Tables from Apps Scripts:

http://googleappsdeveloper.blogspot.com/2011/09/using-fusion-tables-with-apps-script.html

There's a link to some sample code, which you can adapt to import your Fusion Table data to a spreadsheet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top