Demonstration, Tutorial: Fetch data from a shared Google sheet, with Mechanic.

Mechanic is a development and ecommerce automation platform for Shopify. :)

Demonstration, Tutorial: Fetch data from a shared Google sheet

This task script demonstrates using the "http" action to retrieve data from a shared Google sheet, using a GET request. It also includes a basic alert email that will be sent to the configured recipients if Mechanic is unable to access the shared sheet as a CSV.

Runs Occurs when a user manually triggers the task and Occurs when a Mechanic action is performed. Configuration includes gsheet url and alert email recipients.

15-day free trial – unlimited tasks

Documentation

This task script demonstrates using the "http" action to retrieve data from a shared Google sheet, using a GET request. It also includes a basic alert email that will be sent to the configured recipients if Mechanic is unable to access the shared sheet as a CSV.

Review the tutorial for this task in the Mechanic docs to see the steps to share your own Google sheet.

Once the data has been successfully retrieved, it will be available in the mechanic/actions/perform event topic as an array of hashes, with the CSV column headers as the keys for each entry in the hash.

Sample Google sheet data:

| SKU | Quantity |
|--------|----------|
| ABC123 | 10 |
| DEF456 | 0 |

Sample retrieved data:

[
{
"SKU": "ABC123",
"Quantity": "10"
},
{
"SKU": "DEF456",
"Quantity": "0"
}
]

Developer details

Mechanic is designed to benefit everybody: merchants, customers, developers, agencies, Shopifolks, everybody.

That’s why we make it easy to configure automation without code, why we make it easy to tweak the underlying code once tasks are installed, and why we publish it all here for everyone to learn from.

(By the way, have you seen our documentation? Have you joined the Slack community?)

Open source
View on GitHub to contribute to this task
Subscriptions
mechanic/user/trigger
mechanic/actions/perform
Tasks use subscriptions to sign up for specific kinds of events. Learn more
Options
gsheet url (required), alert email recipients (array, required)
Code
{% assign gsheet_url = options.gsheet_url__required %}
{% assign alert_email_recipients = options.alert_email_recipients__array_required %}

{% if event.topic == "mechanic/user/trigger" %}
  {% comment %}
    -- begin by fetching data from the configured gSheet
  {% endcomment %}

  {% action "http" %}
    {
      "method": "get",
      "url": {{ gsheet_url | json }},
      "verify": "true",
      "error_on_5xx": "true"
    }
  {% endaction %}

  {% break %}

{% elsif event.topic == "mechanic/actions/perform" %}
  {% comment %}
    -- only respond to http actions
  {% endcomment %}

  {% unless action.type == "http" %}
    {% break %}
  {% endunless %}

  {% unless action.run.ok and action.run.result.status == 200 %}
    {% comment %}
      -- send alert email if there is an issue accessing the gsheet
    {% endcomment %}

    {% action "email" %}
      {
        "to": {{ alert_email_recipients | json }},
        "subject": "ALERT: Mechanic could not access the shared gSheet",
        "body": "Check the task run log and confirm the gSheet still exists and is shared properly.",
        "reply_to": {{ shop.customer_email | json }},
        "from_display_name": {{ shop.name | json }}
      }
    {% endaction %}

    {% break %}
  {% endunless %}

  {% assign gsheet_data = action.run.result.body | parse_csv: headers: true %}

  {% log gsheet_data %}

  {% comment %}
    -- add processing of the gsheet data below
  {% endcomment %}
{% endif %}
Task code is written in Mechanic Liquid, an extension of open-source Liquid enhanced for automation. Learn more
Defaults
Gsheet URL
https://docs.google.com/spreadsheets/d/e/2PACX-1vRFPR2f9l-w5dSTlcl_ld-mpyqmikI9mupNuc96YLJfmfVqK2c_CJe2_hJkSUVRS-u7jqh8T1jUk-OI/pub?gid=0&single=true&output=csv
Alert email recipients
["sample@example.com"]