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.
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.
function myFunction() {
//Google Sheet
var sourceSheetString = 'https://docs.google.com/spreadsheets/d/116J**SheetID**w844/gviz/tq?sheet=Sheet1&headers=1&tq=';
var query = 'select A,B where B = "Brown" order by A desc';
var queryString = sourceSheetString + query;
var response = UrlFetchApp.fetch(queryString);
var parsedResponse = JSON.parse(response.getContentText());
Logger.log(parsedResponse);
}
Unfortunately my Google Apps domain does not allow sharing outside of the domain so I will be unable to share the files, etc.
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:
SyntaxError: Unexpected token: < (line 10 (var parsedResponse), file "Code")
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.
@SandyGood when I log the response I get this: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta content="width=300, initial-scale=1" name="viewport"> <meta name="description" content="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."> <meta name="google-site-verification" content="LrdT**QXZs"> <title>Google Sheets - create and edit spreadsheets online, for free.</title> <style> a bunch of CSS...