title: Query a Google Sheet using Google Apps Script
body: <p>I have built a web app to track vehicle usage internal to my organization.  One of the features is that the app looks into the existing database (Google Sheets) to check if the Start mileage matches the last End mileage.  I do this using Gviz and queries in JavaScript.</p>
  <p>Unfortunately, this is done on the client side.  Because the potential for a savvy user to view the source code and access the Google Sheet, I would like to move this to the server side (in Google Script).  This way, they will only see the results of the query, when they are passed to the client from the Google Script.  The sample below is not taken from my Web App, it is what I am playing with to query a Google Sheet from Google Script...  I know how to pass values from GS to the client so my main problem is figuring out how to query a Google Sheet from Google Script.</p>
  <pre><code>function myFunction() {
  
      //Google Sheet  
    var sourceSheetString = 'https://docs.google.com/spreadsheets/d/116J**SheetID**w844/gviz/tq?sheet=Sheet1&amp;headers=1&amp;tq=';
    var query = 'select A,B where B = &quot;Brown&quot; order by A desc';
    var queryString = sourceSheetString + query;
   
    var response = UrlFetchApp.fetch(queryString);
    
    var parsedResponse = JSON.parse(response.getContentText());
    
    Logger.log(parsedResponse);
    
  }
  </code></pre>
  <p>Unfortunately my Google Apps domain does not allow sharing outside of the domain so I will be unable to share the files, etc.</p>
  <p>I have scoured the internet looking for a solution but the closest I could come up with us UrlFetchApp.  This is the error I get when I run the function:</p>
  <blockquote>
  <p>SyntaxError: Unexpected token: &lt; (line 10 (var parsedResponse), file &quot;Code&quot;)</p>
  </blockquote>
  <p>When I paste the complete url string into the browser window it works fine so I know it is not a problem with the query string itself.</p>
  <hr>
  <p>@SandyGood when I log the response I get this: <code>&lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt; &lt;meta charset=&quot;utf-8&quot;&gt; &lt;meta content=&quot;width=300, initial-scale=1&quot; name=&quot;viewport&quot;&gt; &lt;meta name=&quot;description&quot; content=&quot;Create a new spreadsheet and edit with others at the same time -- from your computer, phone or tablet. Get stuff done with or without an internet connection. Use Sheets to edit Excel files. Free from Google.&quot;&gt; &lt;meta name=&quot;google-site-verification&quot; content=&quot;LrdT**QXZs&quot;&gt; &lt;title&gt;Google Sheets - create and edit spreadsheets online, for free.&lt;/title&gt; &lt;style&gt; a bunch of CSS...</code></p>