How to access BigQuery generally from Google Apps Script - google-apps-script

I have a Google Apps Script application that currently accesses a GCP SQL database with JDBC.
Using the normal SQL database doesn't cut it, so I decided to try BigQuery.
Is there a way to access BigQuery from Google Apps Script without connecting with an account connected to the GCP project? I want guests who use my script to be able to get data.
I'm looking for either a general way (as in: IP, database, username, password and I manage the connection) or a client library way I can use with Apps Script.
Note, the BigQuery Apps Script plugin seem to only allow access to my own databases, so guests will get denied access.

As per this link , you can share your BigQuery dataset with specific users even if they're not part of the GCP project. Since they need to be able to retrieve data, they will need "Viewer" permission on the dataset. The steps to do this are described in here and this link will show you with an example how you can query your BigQuery table from Apps Script.

Related

insert MySQL from Google sheet

I need to insert a Google spreadsheet information into a MySQL table database automatically every 2 h. Can someone help me, please?
I have already used import tool in phpMyAdmin, but manually, and I need to do it automatically.
Maybe it can be with importmysql or load data infile script. But I need some guidelines, I'm not a programmer, however I can learn the basics to implement it.
Based from this forum: Google Apps Script to Export Spreadsheets to mySQL execute on multiple files:
If your MySQL instance is a public-facing instance, accessible from outside your local network, you could use Google Apps Script JDBC Service to connect to your MySQL instance and insert/update data from your google sheets. Please read the Setup for other databases section of JDBC guide for details on setting up your database for connection from Google Apps Script.
You can have one script that loops through all spreadsheets in a given folder and inserts data from each into your MySQL database.

How to receive emailed csv data into Google Cloud SQL

The goal is to set up a simple relational database in Google Cloud SQL (or BigQuery) that automatically receives/retrieves daily emailed reports. The data source, in this case, is Doubleclick but regardless of the source, I'd like to better understand how scheduled email reports (as attachments) can be sent to or ingested by Google Cloud SQL.
Is there some other app or service out there to make this connection? Is there a Google product like MS Visual Studio to run jobs? Sorry for the very beginner questions but none of the Google support articles are very helpful!
You will need to use Google Apps Scripts to tie the process together.
App Scripts can DoubleClick data and potentially read from a gmail account.
Once you have access to the data, you should be able to use BigQuery service in script to load the data
You can then create a scheduled project trigger within script to poll periodically for new files

Google spreadsheet script authorisation to BigQuery

I have a Google spreadsheet with a script that connects to BigQuery (using this tutorial - https://developers.google.com/apps-script/advanced/bigquery?hl=ar-AE).
It adds an extra menu option and users can run the script that executes a query to BigQuery.
It works fine for me and I want to share this spreadsheet with other users (who don't have access to BigQuery itself) and allow them to run my script. When I do it, first they get script authorisation dialog and are able to run the script after that. But BigQuery returns error:
Exception: Access Denied: Job XXX:job_NaMBWMRfbMHygS1n10AQXXXX: The user does not have permission to run a query in project XXXXX
I tried deploying the script as a web app and set it to execute under my account but it didn't change anything.
Is there any solution or workaround?
Unfortunately you can only allow access to scripts running as 'you' if it is running as a web app. The only way to run it as a webapp is if the doGet()/doPost() function is called by the browser. Running the doGet() as a function runs it as a normal script.
You can give access to run queries using the project access controls in developers console. Check out:
https://cloud.google.com/bigquery/access-control
The other option is to use the Oauth2 library and store your personal token in the scripts properties. You then can rewrite your script to use the BigQuery REST interface. There is a template for this at:
https://github.com/googlesamples/apps-script-templates/tree/master/sheets-import
The best way that I found is to simply change the configuration of the file appsscript.json. You can find by acessing View > Show project manifest. There you will find an option to change the option executeAs and if you change to USER_DEPLOYING the web app always will run as the person who deployed and most likely have the access to the BigQuery project. You can find the source of the documentation here: https://developers.google.com/apps-script/manifest

Is there a way to build an Google Apps Script application that uses BigQuery Service as a service account instead as a user?

What I trying to do here is an application that connects to bigquery, executes some query and writes a spreadsheet in current user's Google Drive.
I know that I can change the "Execute app as" option when deploying the app. But when I do this, and the user try to use the app, Google asks for permission to access the BigQuery.
I do not want to give permission to each user to access BigQuery.
We have about 50 ~ 100 users who will use (and this number can increase)
Some images:
When access the application's url: http://i.imgur.com/kGzJ3ps.png.
When the user clicks in the "continue" button:
http://i.imgur.com/4cRmhZ6.png
Hope I've been clear.
You would probably need an App, that had people sign in with their Google account using oAuth2
Google Accounts Authentication and Authorization
I think that this link gives an overview of what your options are:
Permissions and Types of Scripts
I guess you would need to have an App, that ran as User at the keyboard, so that the spreadsheet saved to their Google Drive. If you wanted the user to be able to download a file to their computer drive, that's a little different.
Just use HTML Service. It's basically just building a website. You just don't have a domain name. But the spreadsheet you are distributing doesn't have a domain name.
HTML Service Google Documentation
From an Apps Script HTML Service, you can write to Spreadsheets, Documents, and run HTTP Requests (urlFetch).
URL Fetch Service
The permissions in the Apps Script App can be set to run the Apps Script file as yourself, with access to anyone, even anonymous. That way, the user will never be asked for permission for any files that you own. You will need to authorize it once, the first time any script that needs access to your files is run.
Because anyone could run your Apps Script App, you might want to have a log in system to authenticate users.
You would need to use the Big Query API with Apps Script urlFetch to access Big Query.
So, with the HTML Service, you can do most everything you could do with HTML, CSS, and Javascript on a regular website. You can create custom input forms, have different pages and display data in tables. Plus it's served over HTTPS.
One solution that you can use if you don't want all your users to enable BigQuery is to use a webapp deployed as a service running as "you" and that will respond to an urlFetch coming from each user through the script they run as "themselves".
This service will be deployed as any other webapp using a doGet main function and return its data using contentService
Yes, that is possible. Check out this answer here: Using Advanced Google Services with Service Account and also check this awesome library: https://github.com/googlesamples/apps-script-oauth2

Connect Google Drive to Could SQL

I am looking to try and combine Google's Cloud SQL service with google drive. Essentially I want to use Google forms for the user to easily input data, and then have that data feed into the Cloud SQL environment (from which I can do reporting and analysis).
My question is, has anyone done this already, or have any ideas of how this might be accomplished? I already have google forms writing to spreadsheets, and that works fine. I am familiar with SQL so creating reports and pulling data from the Cloud SQL environment shouldn't be hard.....but I don't know how to connect the two.
Ideally I would like something to run on a schedule (maybe on a nightly basis) to pull the data from a google spreadsheet, update the Cloud SQL database, and clear the old data from the spreadsheet.
I think the best way to do this would be to use Google Apps Script.
Apps Script allows you, for a given form, to define an onSubmit() method that will be called when someone submitted an answer to the form.
Then you can use the JDBC service to connect to Cloud SQL.