Skip to content

Instantly share code, notes, and snippets.

@tfoldi
Last active April 22, 2022 00:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tfoldi/1a617c1bab095469e2cd to your computer and use it in GitHub Desktop.
Save tfoldi/1a617c1bab095469e2cd to your computer and use it in GitHub Desktop.
# CoffeeScript version of Google Spreadsheet Driver for Tableau Data Web Connector
init = ->
if !tableau
alert 'init- tableau NOT defined!'
return
tableau.scriptVersion = '1.0'
tableau.log 'init'
tableau.initCallback()
shutdown = ->
tableau.shutdownCallback()
getConnectionURL = (connectionData) ->
"http://spreadsheets.google.com/feeds/list/#{connectionData['key']}/#{connectionData['tab']}/public/values?alt=json"
getColumnHeaders = ->
tableau.log 'spreadsheets - getColumnHeaders connectionData=' + tableau.connectionData
tableau.addWhiteListEntry 'http', 'spreadsheets.google.com'
$.ajax
url: getConnectionURL( JSON.parse tableau.connectionData )
dataType: 'json'
success: (res) ->
entry = res.feed.entry[0]
fieldNames = []
fieldTypes = []
for key of entry
if entry.hasOwnProperty(key) and key[0..3] == 'gsx$'
fieldVal = entry[key].$t
if parseInt(fieldVal).toString() == fieldVal
fieldType = 'int'
else if parseFloat(fieldVal).toString() == fieldVal
fieldType = 'float'
else if isFinite(new Date(fieldVal).getTime())
fieldType = 'date'
else
fieldType = 'string'
fieldNames.push key[4..]
fieldTypes.push fieldType
tableau.headersCallback fieldNames, fieldTypes
getTableData = (lastRecordNumber) ->
tableau.log 'spreadsheets - getColumnHeaders connectionData=' + tableau.connectionData
if lastRecordNumber
tableau.dataCallback [], lastRecordNumber
return
$.ajax(
url: getConnectionURL( JSON.parse tableau.connectionData )
dataType: 'json'
success: (data) ->
toRet = []
for entry in data.feed.entry
row = {}
for key of entry
if entry.hasOwnProperty(key) and key[0..3] == 'gsx$'
fieldName = key[4..]
fieldVal = entry[key].$t
row[fieldName] = fieldVal
toRet.push row
tableau.dataCallback toRet, -1
)
$(document).ready ->
$('#inputForm').submit ->
# This event fires when a button is clicked
event.preventDefault()
key = $('#key')
tab = $('#tab')
return if !key or !tab or !key.length or !tab.length
tableau.connectionData = JSON.stringify {key: key[0].value, tab: tab[0].value }
tableau.connectionName = 'Google Spreadsheet Data'
tableau.submit()
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="GoogleSpreadsheetEspresso.js" type="text/javascript"></script>
<style>
body {
font: 12px Helvetica, Arial, sans-serif;
}
.button {
float: left;
padding-bottom: 20px;
clear: both;
color: #6e6e6e;
font: bold 12px Helvetica, Arial, sans-serif;
text-decoration: none;
padding: 7px 12px;
position: relative;
display: inline-block;
text-shadow: 0 1px 0 #fff;
-webkit-transition: border-color .218s;
-moz-transition: border .218s;
-o-transition: border-color .218s;
transition: border-color .218s;
background: #f3f3f3;
background: -webkit-gradient(linear,0% 40%,0% 70%,from(#F5F5F5),to(#F1F1F1));
background: -moz-linear-gradient(linear,0% 40%,0% 70%,from(#F5F5F5),to(#F1F1F1));
border: solid 1px #dcdcdc;
border-radius: 2px;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
margin-right: 10px;
}
.button:hover {
color: #333;
border-color: #999;
-moz-box-shadow: 0 2px 0 rgba(0, 0, 0, 0.2);
-webkit-box-shadow:0 2px 5px rgba(0, 0, 0, 0.2);
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.15);
}
.button:active {
color: #000;
border-color: #444;
}
label{
float: left;
width: 120px;
font-weight: bold;
}
input{
width: 180px;
margin-bottom: 5px;
}
br{
clear: left;
}
img.title {
maring-right: 10px;
}
.title {
font: bold 36px Helvetica, Arial, sans-serif;
vertical-align: text-top;
}
</style>
</head>
<body>
<div class="title">
<img src="spreadsheet.png ">
Google Spreadsheet Connector
</div>
<br/>
<form id="inputForm" action="">
<label for="documentKey">Spreadsheet Key</label>
<input type="text" id="key" name="documentKey" style="width: 400px;" />
<br/>
<label for="documentKey">Sheet name</label>
<input type="text" id="tab" name="sheetName" value="default" style="width: 200px;" />
<br/>
<input type="submit" value="Get spreadsheet" class="button">
<br/>
</form>
<p>Please copy paste the document key from the File -> Publish menu to Spreadsheet key. </p>
<p>Accepted values for sheet name: default, the sheet name as string, numeric position (first sheet is 1)</p>
<p>Click <a href="#">here</a> for additional help. </p>
<p>Use this key to access sample spreadsheet: <code>0AonYZs4MzlZbdEIycWhwSnNBSTBTUDl3WWdDSE1VbHc</core></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment