The Fusion Tables service allows you to use the Google Fusion Tables API in Apps Script. This API gives users the ability to store, share, and query their data tables.
Reference
For detailed information on this service, see the reference documentation for the Fusion Tables API. Like all advanced services in Apps Script, the Fusion Tables service uses the same objects, methods, and parameters as the public API.
Sample code
The sample code below uses version 1 of the API.
List tables
This sample lists Fusion Tables that the user has access to.
function listTables() {
var tables = FusionTables.Table.list();
if (tables.items) {
for (var i = 0; i < tables.items.length; i++) {
var table = tables.items[i];
Logger.log('Table with name "%s" and ID "%s" was found.',
table.name, table.tableId);
}
} else {
Logger.log('No tables found.');
}
}
Run query
This sample queries for the first 100 rows in the given Fusion Table and saves the results to a new spreadsheet.
function runQuery(tableId) {
var sql = 'SELECT * FROM ' + tableId + ' LIMIT 100';
var result = FusionTables.Query.sqlGet(sql, {
hdrs: false
});
if (result.rows) {
var spreadsheet = SpreadsheetApp.create('Fusion Table Query Results');
var sheet = spreadsheet.getActiveSheet();
// Append the headers.
sheet.appendRow(result.columns);
// Append the results.
sheet.getRange(2, 1, result.rows.length, result.columns.length)
.setValues(result.rows);
Logger.log('Query results spreadsheet created: %s',
spreadsheet.getUrl());
} else {
Logger.log('No rows returned.');
}
}