Mechanic Get Mechanic
Task library

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.

Try this task

When it runs

  • Occurs when a user manually triggers the task
  • Occurs when a Mechanic action is performed

What you choose

  • Gsheet URL
  • Alert email recipients

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"
}
]

Task code and subscriptions

You can edit the Liquid code after installing this task. View on GitHub or ask a question in the Mechanic Slack community.

Subscriptions

mechanic/user/trigger
mechanic/actions/perform
How task subscriptions work

Option types and flags

Task 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 %}
Mechanic Liquid reference

Default option values

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"]