how to easily connect dozens of bigquery table to gsheet? - google-sheets

I have a dataset in bigquery containing dozens of tables. The business wants to access these tables through a connected gsheet
The gsheet UI only allows connecting tables one by one, this is a bit tedious. Is there an alternative way to connect all the tables in the dataset to individual sheets with the same gsheet document in one go?

The UI only allows one by one but you can also use the Sheets API to add data sources. You could build your own app and send the requests to the API as AddDataSourceRequest. An easier way would be to use Apps Script since it's already connected with Sheets and it also has a DataSourceTable class. A quick example of how to add a data source in Apps Script would look like this:
function addConnectedTable() {
SpreadsheetApp.enableBigQueryExecution();
var ss = SpreadsheetApp.getActive()
var spec = SpreadsheetApp.newDataSourceSpec()
.asBigQuery()
.setProjectId('your-billing-project') //set your own project ID here
.setRawQuery('select * from `bigquery-public-data.new_york_trees.tree_species`')
.build();
ss.insertDataSourceSheet(spec).asSheet().setName("Some Name"); //sets the name of the new sheet
}
As a sample I used a query from a public data set, but you can just write your own queries for each of your tables and run the script with them to quickly add them.
Sources:
Sheets API AddDataSourceRequest
Connected Sheets in Apps Script
Apps Script DataSourceTable

Related

Google drive sheets importrange

I am looking for a way to import the data from one google drive sheet to another using ImportRange formula. However, I want the data to be synced once per day at a certain time instead of automatically updating as the formulas seems to do. Any help would really be appreciated
Formula used:
={IMPORTRANGE(B2,"sheet1!$A$1");IMPORTRANGE(B3,"sheet1!$A$1");IMPORTRANGE(B4,"sheet1!$A$1"); IMPORTRANGE(B5,"sheet1!$A$1");
IMPORTRANGE(B6,"sheet1!$A$1")
}
You can create a Script (Google Apps Script) that copies the data automatically with Time-driven Triggers (https://developers.google.com/apps-script/guides/triggers).
function copyData() {
// Gets data
var data = SpreadsheetApp.openById("ID1").getSheetByName("SheetName").getRange("A1:B2").getValues();
// Copies data
SpreadsheetApp.openById("ID2").getSheetByName("SheetName").getRange("A1:B2").setValues(data);
}

Google data studio - Use multiple datasheet with same data keys/headers

So I've been stuck in this for some days, tryed a lot of search terms but all of them seems to bring me the same answers and i really need this:
I have a demand to join two different company's datas from the same owner, all of them have the same data sources (excel data sheets from FB ADS).
So they all share the same (keys/headers), like this:
COMPANY(1)'S ADS DATA
COMPANY(2)'S ADS DATA
So this way I need to put then togheter without having to join both of then on excel every time and also give him some nice data manipulation power.
The results should be something like this
By now I was trying to join data from the two companys but I couldn't really figure out how to properly do this so far I've made some tests and tryed reading a couple of articles and google data studio's help files. The merging data function seems to mess everything.
As a result of this merge, GDS gives me this fields:
Shouldn't I see like only one field labeled as cnt and cmp? I've noticed that GDS creates not one, but two data fields. If I try adding all data I need as key the left sheet turns all "0s". What Am I doing wrong here?
I have read your descriptions. It seems that you are looking for a solution to append both tables instead of merging the tables.
Do note that the data blending in GDS is a left outer join.
Hence, instead of doing the blending in GDS, I'd suggest you to append both datasets in Google Sheet in a separate tab before importing to GDS for visualisation. (assuming you don't mind copy-pasting the data into the Google Sheet).
Here is the formula to append both datasets in Google Sheets:
= {QUERY(A!A1:D1000,"SELECT A,B,C,D WHERE A <> ''",1);QUERY(B!A2:D, "SELECT A,B,C,D WHERE A <> '' ")}
I've created some dummy data in this google sheets and appended the data using the formula provided , you may take a look to understand further.
If you are unclear on the difference between merge and append, you may take a look in the Google Sheet documentation as well.
On a side note, I've screencast the process of answering this question and posted on my youtube channel. You may take a look if needed. (Thanks for the question and inspiration you provided for the video)

Getting data from Google Sheets to BigQuery

I need to pull data from a Google Sheet into BigQuery. So far I have created a Table in BQ using the CSV option and all of the data imported fine. But now I need to automatically update the BQ table with the data in the Google Sheet's tab. Can anyone point me in the right direction? Thanks in advance.
With BigQuery you can directly query your data from the Google Sheet by creating an external table from the console.
Given your data is properly formatted, you just have to "create table", from "Drive", provide your Google Sheet uri, fill-in some additional settings and that's it !
Any changes in the spreadsheet will be immediately accessible in BigQuery as well.
Documentation for reference.
I looked at BQ when they released and quickly dismissed it as it is free for a short period and then we have to pay. I have my own domain/website and it comes with MySQL, FTP, and so much more.
Anyway, I have many projects that go the opposite direction, pull the data down from the SQL DB to sheets, but which direction does not really matter.
Google sheets has Triggers, I have triggers set to launch at certain times, usually every day, you get to set up the trigger to fire off a function you specify.
https://developers.google.com/apps-script/reference/script/clock-trigger-builder
You can also set triggers via scripts
/**
* Creates time-driven triggers
*
* https://developers.google.com/apps-script/reference/script/clock-trigger-builder
*/
function createTimeDrivenTriggers() {
// Trigger every day at 04:00AM CT.
ScriptApp.newTrigger('csvDaily')
.timeBased()
.everyDays(1)
.atHour(4)
.create();
}

Google-Sheet - query multiple unknown sheets?

Context: I need to develop a Google Sheet to manage yearly cost and revenues from the Charity where I volunteer. This file will be updated from volunteers, so I need to develop a structure which they can use with easiness.
My idea:
have a front sheet with a summary from activities and charts
let volunteers duplicate a template event cost and revenue and add their c&r
Result would have as much sheets as many Events and Charity or Fundraising events/campaign.
But, since the number of sheets is unknown till the end of the year, how can I have a summary sheet?
E.G. How can I sum all costs and all rev? Can I query to search in **all existing sheets the cell that follows an exact string (e.g. Total Rev)?**
You can write an Apps Script - a Google Script service based on Javascript
It is relatively simple to learn and use and it will allow you to loop dynamically through all the sheets without knowing in advance how many there will be.
The following sample script iterates through all sheets minus the master sheet of the spreadsheet to which it is bound and sums the values of the cells "A10" (if this is the cell where you have your costs) together. Finally it sets the value of the sum into the cell "A10" fo the master sheet.
function myFunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheets=ss.getSheets();
var sum=0;
for (var i=0; i<sheets.length; i++){
if(sheets[i].getName()!="Master"){
var value=sheets[i].getRange("A10").getValue();
sum=sum+value;
}
}
ss.getSheetByName("Master").getRange("A10").setValue(sum);
}
If you take some time to get familiar with Apps Script, you will be able to easily adapt the sample above to your needs.
I would recommend that the volunteers don't fill out anything on the sheet at all.
You can collect all you need from the volunteers via a google Form. That form will dump all the data into a single tab in google sheets from which it will be easy to analyze and summarize the data that you want to see and share with the whole team.

Put results of two forms in two sheets of one spreadsheet

I have a system for accepting and reviewing grant applications. There are two forms:
form submitted by an applicant with a grant request
form sent by me to three reviewers to evaluate a grant request
I'd like to have the results of both forms in one spreadsheet:
Sheet 1 lists the data from the application forms (with an auto-generated serial number).
sheet 2 lists the data from all the reviews for all the applications (with a column for the serial number of the application that was reviewed).
I'm having trouble with:
1 - Specifying that both Forms' results go into the same spreadsheet, on different sheets.
2 - Adding a serial number to the applications and then adding the same serial number to the results of the three reviewers.
Any suggestions?
It is impossible to insert data of two Google Forms directly to a single spreadsheet using GAS without involving another two spreadsheets, because now the Forms are able to deploy data only to own spreadsheet and GAS has no any service to access to the Forms.
I'm going to be a little general as your asking a rather broad question, but I will do my best. I would consider using UiApp to build forms from the ground up. It will allow you more flexibility in function, and simple a smoother flow of information but since your question is about forms, I will stick to that.
Question 1:You'd want to create an OnSubmit function within each spreadsheet associated with the form 1 and 2 that copy the data you want to another spreadsheet.
function onFormSubmit() { // add an onsubmit trigger
var emailAddress = e.values[1];
var fullName = e.values[2];
var newSS = SpreadsheetApp.openById("Add New SS ID").getSheetByName('Sheet1');//one form should be 'Sheet2'
var range = sheet.getRange(sheet.getLastRow()+1,1,1,2);
range.setValues([[emailAddress, fullName]]);
Question 2: Assign a serial number. To my mind, you only want to assign a number to the applications, and then give that number to the reviewers for them to enter into the form. To assign this number, you can follow the Tutorial: Automating a Help Desk Workflow. Instead, I simply use the get last row, as I never delete rows in an active application so the row numbers are unique.
var serialNum = newSS.getLastRow()+1;
Then you can simply add that variable as you did the rest. I've not tested this code, so you may need to clean it up for your purposes.

Resources