Skip to content

upload_to_gsheet

upload_to_gsheet(data, spreadsheet_url, worksheet_name, write_mode)

Description

Upload data (a json array of objects) to a Google Sheet in write_mode.

  1. 💡 For this to work, share your Google Sheet in edit mode to 749389685934-compute@developer.gserviceaccount.com
  2. write_mode controls what is done if a worksheet with worksheet_name already exists. It must be one of:
    • write_truncate: if the sheet already exists, it will be recreated.
    • write_append: if the sheet already exists, data will be appended to it.
    • raise_error: if the sheet already exists, an error will be raised.
    • do_nothing: if the sheet already exists, nothing will be done.
    • null: same as write_truncate

Usage

Call or Deploy upload_to_gsheet ?
Call upload_to_gsheet directly

The easiest way to use bigfunctions

  • upload_to_gsheet function is deployed in 39 public datasets for all of the 39 BigQuery regions.
  • It can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • (You need to use the dataset in the same region as your datasets otherwise you may have a function not found error)

Public BigFunctions Datasets

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...
Deploy upload_to_gsheet in your project

Why deploy?

  • You may prefer to deploy upload_to_gsheet in your own project to build and manage your own catalog of functions.
  • This is particularly useful if you want to create private functions (for example calling your internal APIs).
  • Get started by reading the framework page

Deployment

upload_to_gsheet function can be deployed with:

pip install bigfunctions
bigfun get upload_to_gsheet
bigfun deploy upload_to_gsheet

Examples

select bigfunctions.eu.upload_to_gsheet(json '[{"col1": "row1", "col2": 1}, {"col1": "row2", "col2": 2}]', "https://docs.google.com/spreadsheets/d/xxxxxxxxx", "my worksheet", "write_truncate")
select bigfunctions.us.upload_to_gsheet(json '[{"col1": "row1", "col2": 1}, {"col1": "row2", "col2": 2}]', "https://docs.google.com/spreadsheets/d/xxxxxxxxx", "my worksheet", "write_truncate")
select bigfunctions.europe_west1.upload_to_gsheet(json '[{"col1": "row1", "col2": 1}, {"col1": "row2", "col2": 2}]', "https://docs.google.com/spreadsheets/d/xxxxxxxxx", "my worksheet", "write_truncate")
+---------------+
| result        |
+---------------+
| DATA_UPLOADED |
+---------------+

Use cases

A marketing team wants to analyze the performance of their recent social media campaigns. They have the campaign data stored in a BigQuery table. To share this data with non-technical stakeholders who primarily use Google Sheets, they can utilize the upload_to_gsheet function.

Here's a breakdown of the use case:

  1. Data Preparation in BigQuery: The marketing team creates a BigQuery query to aggregate the relevant campaign data, such as campaign name, impressions, clicks, conversions, and cost. Let's assume the query results in a table named campaign_performance.

  2. Converting to JSON: They use BigQuery's TO_JSON_STRING function to convert the results of the campaign_performance table into a JSON array of objects, where each object represents a row of campaign data.

SELECT TO_JSON_STRING(t)
FROM `project.dataset.campaign_performance` AS t;
  1. Uploading to Google Sheets: They use the upload_to_gsheet function within BigQuery to upload this JSON data directly to a designated Google Sheet.

SELECT bigfunctions.us.upload_to_gsheet(
    (
        SELECT TO_JSON_STRING(t)
        FROM `project.dataset.campaign_performance` AS t
    ),
    'https://docs.google.com/spreadsheets/d/YOUR_SPREADSHEET_ID',
    'Campaign Performance',
    'write_truncate'
);
This code snippet does the following: * Calls the upload_to_gsheet function from the appropriate regional dataset (e.g., bigfunctions.us). * Passes the JSON string generated in the subquery as the data argument. * Provides the URL of the target Google Sheet, replacing YOUR_SPREADSHEET_ID with the actual ID. * Specifies the worksheet name as 'Campaign Performance'. * Uses the write_truncate mode to overwrite the sheet if it already exists, ensuring they always have the latest data. Alternatively, they could use write_append to add new data to the existing sheet.

  1. Sharing and Analysis in Google Sheets: The Google Sheet is then shared with the non-technical stakeholders, who can easily access, visualize, and analyze the campaign performance data within their familiar spreadsheet environment. They can create charts, pivot tables, and use other Google Sheets features to gain insights.

This process automates the data transfer from BigQuery to Google Sheets, ensuring that stakeholders have up-to-date campaign performance data readily available for analysis and decision-making. It bridges the gap between technical data storage and non-technical data consumption, enabling broader access to critical business information.


Need help or Found a bug?
Get help using upload_to_gsheet

The community can help! Engage the conversation on Slack

We also provide professional suppport.

Report a bug about upload_to_gsheet

If the function does not work as expected, please

  • report a bug so that it can be improved.
  • or open the discussion with the community on Slack.

We also provide professional suppport.


Show your ❤ by adding a ⭐ on