Save for a few legacy projects that still use Fusion Tables I don't actively use this script anymore. This update hopefully solves the OAuth issue that cropped up once Google depcricated the Client Login method used by the prior version.
As always, your mileage may vary, and I welcome someone -- Google or otherwise -- to offer a long-term maintained solution.
The following is largely cribbed from a Google example here. I try to explain some of the API settings that must be enabled in the Developer's Console and elsewhere
- Head to Google docs, create a spreadsheet and add some data to it. Simple enough right?
- Now I'm going to create a Fusion Table based off my spreadsheet. I do this either by importing from Google Docs or downloading the spreadsheet as a csv and uploading it to Fusion Tables. It is important to note it's best practice for the column names of the spreadsheet and the table to match. It's not a deal breaker, but why not be consistent? For good measure I make sure any new columns are also in the same order.
-
After my Fusion Table is created, I need to get the Encrypted Table ID in order to make sure the spreadsheet can access it. To find the Encrypted Table ID I click File --> About this table. I'll copy this somewhere in a text file until I need it.
-
I'm almost ready to make this happen, but I'm going to need to enable the Google Fusion Tables API in two places.
-
First, head to the Developer's Console and log in with your Google account if prompted.
-
Click create a project and give it a name.
-
Choose the Enable and manage APIs under the Use Google APIs tile
-
Find Fusion Tables API and click the "Enable API button"
-
Click Go to Credentials. You will be "Calling Fusion Tables API from a web browser"
-
Create the OAuth 2.0 client ID
-
For Product name shown to users I put "Sync Spreadsheet To Fusion Tables"
- Click Done
-
Now we're ready to add our script to our spreadsheet. Back at your spreadsheet, go to Tools --> Script Editor and paste the script code. On Line 9 I add my Fusion Table's Table ID...
// Add the encrypted table ID of the fusion table here var TABLE_ID = '17xnxY......';
-
Optional: if you have multiple header rows, put the row number of the first data row on this line:
var FIRST_DATA_ROW = 2;
-
Optional: if you want to allow the spreadsheet to have different columns than the table, change the line 15 value to "false":
var REQUIRE_SAME_COLUMNS = true;
-
Click save. You will be prompted to give the project a name. "Update Fusion Tables" works. Click the save icon or go to File --> Save.
-
Click Resources --> Developer's Console Project. Enter the Project Number for the project you just created.
- Reload the spreadsheet and you will see a new menu item next to help. Mine reads "Sync Spreadsheet To Fusion Table." Click the menu item and you will see an option to "Update Fusion Table."
- Now just add some new information to your spreadsheet and click "Update Fusion Table." The application will ask you to authenticate. Once you click OK, your spreadsheet data should be synced with your Fusion Table.
- Sit back and enjoy this moment …
This script can now be combined with other script functions to make the integration between Google spreadsheets and Fusion Tables more powerful. Or perhaps you want to add a trigger to sync data between the spreadsheet and the table. Such a trigger can be added to run minute by minute, or hourly and those changes will be reflected on the table.
Of course, unless the script didn't work... Mention me in the comments so I'm notified and we'll see what we can do...
Trying to log error messages and the possible cause. These are not confirmed. Just best guesses
...
Request failed for https://www.googleapis.com/fusiontables/v1/query?key=AIzaSyCBbVMnJwhD5xPYJpcvOT8vOUlLs9jYv5U& returned code 400. Truncated server response: { "error": { "errors": [ { "domain": "fusiontables", "reason": "badQueryCouldNotParse", "message": "Invalid query: Parse error ne... (use muteHttpExceptions option to examine full response)
The following is likely caused by your API key not being recognized.
Request failed for https://www.googleapis.com/fusiontables/v1/query?key=MY API CODE& returned code 401. Truncated server response: { "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "head... (use muteHttpExceptions option to examine full response) (line 79, file "Code")
Which might just mean that your API key isn't active yet… Or you might receive an error because you didn't authenticate with the application when prompted. Be sure you have activated the Fusion Tables API on the Google API console and added the key to the script in the proper place.
The following is likely caused by either not authenticating or a corrupt authentication token
Request failed for https://www.google.com/accounts/ClientLogin returned code 403. Truncated server response: Error=BadAuthentication Url=https://www.google.com/accounts/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbutVGIXKvt9Nhj84zteA6Qk3RgCkgkQzOPygJE4aWgRrWXJ8_N... (use muteHttpExceptions option to examine full response
Hi, thanks for the explanation.
Is there any way to sync from fusion table to google spreadsheet and not the other way around?
Thanks a lot!