Authorizing Google Charts to access private spreadsheets - google-apps-script

I am trying to create a Web App using Google Apps Script to query data from my Google Sheet using Google Charts.
I have been able to successfully query the spreadsheet when the Google Sheet is publicly shared, however since the spreadsheet contains confidential info I would rather do it privately using authorizations.
The reason why I want to use Google Charts visualisation functions (as opposed to the server side Spreadsheet App) is because of the speed of querying large data sets.
I have tried following the steps in the above documentation. That is, creating a client id then using the gapi.auth library to authenticate myself but I continue to receive an error.
When i add the authorization library and first part of the code from the documentation (with console.log simply to see where it get's up to):
<script src="https://apis.google.com/js/auth.js?onload=init"></script>
<script>
var clientId = '1234.apps.googleusercontent.com';
var scopes = 'https://spreadsheets.google.com/feeds';
function init() {
console.log("here");
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true},handleAuthResult);
}
</script>
I receive the following error:
1289869776-mae_html_user_bin_i18n_mae_html_user.js:41 dropping
postMessage.. was from unexpected window
Any guidance is appreciated.

Since you are creating your web app using Google Apps Script, it's not necessary to "authorize Google Charts to access private charts" because you could use Google Apps Script services and methods to get the data from the spreadsheets and pass them to Google Charts.
On Converting from UiApp + Chart Service to Html Service + Google Visualization API it's shown how to convert the old dashboard example from from UiApp + Chart Service to HtmlService + Google Visualization API. This illustrates how to create a Google Apps Script web app that builds a chart from Google Spreadsheet data without "requiring authorization" as it's "implicitly" handled by Google Apps Script.
From the above link
Two functions cooperate to retrieve the dashboard’s data and display it. As soon as the visualization API is loaded, the sendQuery() function is invoked. Using the google.script.run facility, it sends its request to the server-side getSpreadsheetData() function. This is an asynchronous operation, so two callbacks are provided, a successHandler and a failureHandler. One or the other will receive the result of the server call, depending on the outcome.
H/T to jfllmartin, author of an answer to Converting my google dashboard app from the UI service to the HTML service where the above link was shared.
Related
How to create google dashboard including piechart and range select filter from a spreadsheet?
Display Spreadsheet data in Sites with Html Service

can I suggest you change from using Google sheets to using firebase with Google sheets or just Firebase, and then with Google appscript in the back end.
I often use a Google script to problematically update Firebase with data from the Google sheet. I then enjoy the speed and security of Firebase to deliver a super fast user experience.
There are two go to pages for using Firebase in appscript. The example page and the quick start.
Furthermore, I gave up using Google's own charting library and starting using high charts or chartJS, as they are more accessible.

Related

How to call an API running locally from Apps Script

I am building a Container Bound Script using AppsScript wrt Google Sheets.
I am taking input by adding a menu on the google sheet as shown below,
And when clicked, prompted with a HTML form, in which we can upload a PDF, which is saved on Google Drive and the information regarding the file upload in saved on the google sheets, but I also want to send to a FastAPI running locally on my machine.
File Information stored on Google Sheets
Code for saving on Google Drive
function uploadFilesToGoogleDrive(data,name,type){
var datafile = Utilities.base64Decode(data)
//create a new blob with decode data, name, type
var blob2 = Utilities.newBlob(datafile, type, name);
var folder = DriveApp.getFolderById("<url>");
//Create new file (property of final user)
var newFile = folder.createFile(blob2);
var rowData = [
newFile.getName(),
newFile.getUrl(),
newFile.getDateCreated()
];
SpreadsheetApp.getActive().getSheetByName("sheet1").appendRow(rowData);
return newFile.getUrl()
}
For sending data to external API I came across Run app script function from website/localhost and Upload files from google drive to external api using google apps script
I am not getting a method through which I can send to API running locally without localtunnel.
It's not possible to run a Google Apps Scripts project locally, Google Apps Scripts projects run on Google Servers.
You can find more information here.
In your case, the only viable way I can think of for you to can call an API being served locally, would be to expose the appropriate endpoints from your API to the web. That way, the Google Servers responsible to run your script will have access to your API.
I understand that perhaps this solution is against the purpose of having an API server locally (since it won't be locally anymore), however Google Apps Script is designed to work on the Cloud.
In any case, you may find the Apps Script Class UrlFetchApp useful for making HTTP requests for your own API of any other.

How to make Google App script triggers to run on script executions or API requests?

I am aware that installable triggers do not execute/run on Script executions or updates via API requests. For triggers to run, the change or edit has to be done 'manually'. Is there any work-around or solution to this?
Here is my usecase:
I have designed HTML UI that accepts some data and stores that in specific spreadsheet
Since I didn't want any users to have access to backend spreadsheet (to restrict view to other's submission), I have created REST service using TIBCO Cloud Integration > Google connector that writes data to Google spreadsheet
When user submit the response, UI internally invokes above-mentioned TIBCO API and that in turn updates the spreadsheet (using my Google credentials) with response submitted by user
With this arrangement, I don't have to share my Google spreadsheet with all users and expose all submissions. I could have used Google Form, but that is too basic and my UI requirements are quite complex.
Until this point, everything works fine, but as a part of some advance features, I want some functions (written in the script of backend spreadsheet) to trigger on 'onEdit' when my API call write to the spreadsheet and that is where I am stuck. So far, I have tried:
Exposing Google Script as an REST API
Exposing Google Script as Library function
but in all cases, I have to share the spreadsheet with user that invokes the calls, and I dont want that, thanks.

Implementing Google sheet API in html

I am planning to use Google sheet API v4 in my website .
I have custom form, I want the data of form to dumbed in Google sheet
I don't have any servers like Python, Apache, etc., in my host server
You may follow this SO thread on how to get the data in forms using Forms Service. Also check on this Spreadsheet Service on how to modify the spreadsheet using apps script. Then you can publish the script as a web app if the function returns an HTML service HtmlOutput object or a ontent service TextOutput object.
Here are the following tutorials which might help:
Saving Form Data to Google Spreadsheets Using PHP and the Google Docs API
Google Sheets as a Database – INSERT with Apps Script using POST/GET methods (with ajax example)

Using oAuth2 to send requests to Google Apps Script (i.e. GAS as google API)

I have been looking for a way to interact with my google sheets (and other services) from Excel (and other desktop applications).
For example:
I get a report from a work system and my boss asks me to update the information in the google sheets so that our site is up to date with the latest information.
What would be great (and easy for me) would be to use a VBA script in my spreadsheet to turn all my data in http requests and then do the heavy lifting with Google Apps Script in the cloud and finally updating the Google Sheets with the associated API.
When I look at the oAuth2 playground I can't find a way to enable Google Apps script, as I have successfully done so with other APIs.
(to be clear, I don't want to do oAuth2 with in GAS - which is a lot of the other questions that get raised)
Is this a hard problem or is there a solution?
There is currently no way of calling a Google Apps Script from an external program. You do have a few other options:
Import the relevant data into Google Sheets manually and use Google Apps Script to process it as needed, possibly making use of time-based triggers to handle the processing automatically.
Use the Google Spreadsheets API. Unlike Apps Script, this API is built to be called from other programs and can manipulate Google Sheets. However, it is a bit more difficult to use than Apps Script. Once data has been moved into a Google Sheet, a separate Apps Script can be used to manipulate it.

How to Access / Run Google Apps Script (GAS) Functions from ordinary non-GAS web apps?

This is an alternate to the Google Maps V3 Full Integration API issue in my previous question.
I would like to give the user the ability to click a button, that then creates a new Google Spreadsheet, within their Google Drive, that allows them to save the data (JSON) that has been created while playing with my Mapping Application, into the new spreadsheet.
Later, the user can go to their Google Drive, open the spreadsheet, click a menu, open and populate my Maps App with their data from the spreadsheet. The user can edit the data either in the Spreadsheet or the Map and the changes go both ways.
Basically, I am looking for two way data binding between Google Maps V3 API and Google Spreadsheet that is very easy for a non-technical user.
It would seem that I should be able to write a GAS function and access it from my standard web page. How do you do that?
If I could convert my Maps V3 API app to GAS, the issues would be trivial, but until then an alternate workflow between Maps and Google Drive would be very helpful to allow users to save / collaborate their work.
If you deploy your Apps Script project as a web app you can have your application make requests to it over HTTP. The doGet() and doPost() handlers can both read request parameters, and using the ContentService you can return JSON or XML data. Likewise, inside Apps Script you can use UrlFetchApp to make HTTP requests to your application.