Skip to content

bigfunctions > upload_to_gsheet

upload_to_gsheet

Call or Deploy upload_to_gsheet ?

✅ You can call this upload_to_gsheet bigfunction directly from your Google Cloud Project (no install required).

  • This upload_to_gsheet function is deployed in bigfunctions GCP project in 39 datasets for all of the 39 BigQuery regions. You need to use the dataset in the same region as your datasets (otherwise you may have a function not found error).
  • Function is public, so it can be called by anyone. Just copy / paste examples below in your BigQuery console. It just works!
  • You may prefer to deploy the BigFunction in your own project if you want 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). Discover the framework

Public BigFunctions Datasets:

Region Dataset
eu bigfunctions.eu
us bigfunctions.us
europe-west1 bigfunctions.europe_west1
asia-east1 bigfunctions.asia_east1
... ...

Description

Signature

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

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 |
+---------------+

Need help using upload_to_gsheet?

The community can help! Engage the conversation on Slack

For professional suppport, don't hesitate to chat with us.

Found a bug using 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.

For professional suppport, don't hesitate to chat with us.

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.

Spread the word

BigFunctions is fully open-source. Help make it a success by spreading the word!

Share on Add a on