title: Using Google Apps Script as service to retrieve data from spreadsheet
body: <p>I'm new to this technique and have been researching for a long time with limited success. (There are a lot of instructions and tutorials out there but a lot of it is outdated and no longer relevant or works.) What I am trying to do is create a GAS that delivers data as an object from a google sheet to a javascript request. I have several limitations - the JS resides on an internal only intranet, my Chrome (v58.0) settings restricts XMLHttpRequest (which works perfectly in my version of IE10), I cannot access the developer console in Chrome (I can in IE) and I cannot use JQuery or any other plugin. Everything on the GAS side seems to work and delivers what it should, but the JS seems to be the problem.</p>
  
  <p>When I access the page direct (via what is produced by doGet(), the text result of the object is delivered. All data shows up like it should but as plain text.</p>
  
  <p>Here's my GAS (execute as me, anonymous):</p>
  
  <pre><code>function doGet() {
  var oReturnLinks = [];
  var sLinks = SpreadsheetApp.openById("secret").getSheetByName("links");
  var oLinks = sLinks.getSheetValues(2, 1, -1, -1);
  
  //loop over data and determine what to return
  for (a=0;a&lt;oLinks.length;a++) {
      if (oLinks[a][0] == true &amp;&amp; oLinks[a][1].trim() != '' &amp;&amp; oLinks[a][2].trim() != '' &amp;&amp; oLinks[a][3].trim() != '' &amp;&amp; oLinks[a][4].trim() != '' ) {
  
          oReturnLinks.push(oLinks[a]);
  
      }
  
  }
  
  if (oReturnLinks.length &gt; 0) {
  
      return ContentService.createTextOutput(JSON.stringify(oReturnLinks)).setMimeType(ContentService.MimeType.JAVASCRIPT);
  
  } else {
  
      return ContentService.createTextOutput(JSON.stringify("no data returned")).setMimeType(ContentService.MimeType.JAVASCRIPT);
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Then in my JS on my intranet (this works in IE10):</p>
  
  <pre><code>var xhttp = new XMLHttpRequest();
  xhttp.open("GET", "https://script.google.com/macros/s/secret/exec", false);
  xhttp.setRequestHeader("Content-type", "application/json");
  xhttp.send();
  var response = JSON.parse(xhttp.responseText);
  </code></pre>
  
  <p>I've also tried this:</p>
  
  <pre><code>var s
  s = document.createElement("script");
  s.src = "https://script.google.com/macros/s/secret/exec";
  var oResult = JSON.stringify(s);
  alert(oResult);
  </code></pre>
  
  <p>...but it produces </p>
  
  <blockquote>
    <blockquote>
      <p>{}</p>
    </blockquote>
  </blockquote>
  
  <p>as the result.</p>
  
  <p>If I add this to the output content:</p>
  
  <pre><code>'(' + JSON.stringify(oReturnLinks) + ')'
  </code></pre>
  
  <p>I get this:</p>
  
  <blockquote>
    <blockquote>
      <p>[Object HTMLScriptElement]</p>
    </blockquote>
  </blockquote>
  
  <p>...as the output.</p>
  
  <p>If I change the mimetype in GAS to JSON, I get a SEC7112 error in IE (was blocked due to mime type mismatch) and some error in Chrome that I can't see.</p>
  
  <p>There has to be a cross browser solution to this? I feel like it's something simple I am missing.</p>