Create and email spreadsheet files
This guide explains how to create a spreadsheet file (such as .csv
or .xlsx
) from a data query and send it via email.
This guide is needed because the agent disallows writing a file to disk for security reasons. Thus, a permission error will be thrown when trying to create a CSV or Excel file via a Python or JavaScript step. Instead, follow this guide for steps to create a file and send via email.
-
Create a new Backend API, Workflow, or Scheduled Job.
-
Query and return the data that should be included in the spreadsheet file via a database step. For example, you can use the
[Demo] Orders
Postgres database and return data with the following query:SELECT * FROM orders LIMIT 10
-
Create a Python step and use the following code to return the data as a string in csv format:
from pandas import DataFrame
df = DataFrame(Step1.output)
return df.to_csv() -
Create an email step, insert an email in the To section, add a Subject and Body, and update the "File Attachments Object Array" with the following code snippet to create a CSV file (see note below for
.xlsx
format) at execution time:{{ [{name:"my_csv.csv", contents:Step2.output, type: "text/csv"}] }}
You can also create other file types, such as Excel files (.xlsx
format), by using the following code block in Step 4 above:
{{ [{name:"my_xlsx.xlsx", contents:Step2.output, type: "text/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}] }}