10

How to take the first cell of a Google spreadsheet and output it as a string with JavaScript to an HTML document?

I have an HTML document labeled: "Website.html"

<!DOCTYPE html>
<html>
     <head>
        <script>
          var textDisplay = Some code to import spreadsheet cell text;
          document.getElementById("display").innerHTML = textDisplay;
        </script>
     </head>
    <body>
        <p id="display"></p>
    </body>
</html>

and another file in the same folder labeled: "Spreadsheet.xcel" (or whatever file type that is). The first cell contains the text: "Hello".

How do I make the text in the spreadsheet import into the JavaScript of the HTML document?

Community
  • 1
  • 1
CTOverton
  • 616
  • 2
  • 9
  • 20
  • Easiest would be something like `="var text = "'" & A1 & "';"` – Tim Williams May 10 '13 at 16:10
  • How? Meaning I have a javascript code in an html doc called "Website" in a folder. In the same folder I have another file called "spreadsheet.excel" or whatever file type that is. How do I import it – CTOverton May 10 '13 at 19:38
  • You need to update your question with more detail if you really want usable answers. What server-side platform you're using would be a good start, but don't limit yourself to just that... – Tim Williams May 10 '13 at 19:41
  • Ok I will update it and please take a look. I thank you for your help! – CTOverton May 10 '13 at 20:16
  • Still lacking needed details. Where are these files - on your local machine/on a web server/somewhere else? What event triggers this extraction? What language/platform do you want to use to do this? – Tim Williams May 10 '13 at 21:43

1 Answers1

16

Your solution will depend on your data source; you included , so here's an answer about that. Just stating the obvious to start: A google spreadsheet would not be a file on your server, instead it would be in "the cloud" in a Google Drive.

You can retrieve contents of google spreadsheets from your javascript, and there are examples here in SO:

Basic idea for retrieving one cell of data as a string:

Example

function loadData() {
  var url="https://docs.google.com/spreadsheet/pub?key=p_aHW5nOrj0VO2ZHTRRtqTQ&single=true&gid=0&range=A1&output=csv";
  xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status==200){
      document.getElementById("display").innerHTML = xmlhttp.responseText;
    }
  };
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}
<html>
  <body>
    <button type="button" onclick="loadData()">Load Spreadsheet Data</button>
    <div id="display"></div>
  </body>
</html>

You can also see this as a jsfiddle here.

Thanks to GPSVisualizer, who were kind enough to publish a public google spreadsheet I could use for this example.

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • How do we query more than one cell per request? – ayjay Aug 13 '14 at 18:51
  • @ayjay - just use a different range in the URL. The range needs to be contiguous. For A1:B2, say, you'd use `var url="https://docs.google.com/spreadsheet/pub?key=p_aHW5nOrj0VO2ZHTRRtqTQ&single=true&gid=0&range=A1:B2&output=csv";`. – Mogsdad Dec 09 '14 at 03:26