-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Metabase - Add export query with format action #19144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jcortes
merged 4 commits into
PipedreamHQ:master
from
AryanBagade:feature/metabase-export-query-with-format
Nov 21, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
components/metabase/actions/export-query-with-format/export-query-with-format.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import app from "../../metabase.app.mjs"; | ||
|
|
||
| export default { | ||
| key: "metabase-export-query-with-format", | ||
| name: "Export Query with Format", | ||
| description: "Execute a saved question/card with parameters and export results in the specified format (CSV, JSON, XLSX, or API). [See the documentation](https://www.metabase.com/docs/latest/api/card#post-apicardcard-idqueryexport-format).", | ||
| version: "0.0.1", | ||
| annotations: { | ||
| destructiveHint: false, | ||
| openWorldHint: true, | ||
| readOnlyHint: true, | ||
| }, | ||
| type: "action", | ||
| props: { | ||
| app, | ||
| cardId: { | ||
| propDefinition: [ | ||
| app, | ||
| "cardId", | ||
| ], | ||
| }, | ||
| exportFormat: { | ||
| type: "string", | ||
| label: "Export Format", | ||
| description: "The format to export the query results in", | ||
| options: [ | ||
| { | ||
| label: "CSV", | ||
| value: "csv", | ||
| }, | ||
| { | ||
| label: "JSON", | ||
| value: "json", | ||
| }, | ||
| { | ||
| label: "XLSX", | ||
| value: "xlsx", | ||
| }, | ||
| { | ||
| label: "API", | ||
| value: "api", | ||
| }, | ||
| ], | ||
| }, | ||
| formatRows: { | ||
| type: "boolean", | ||
| label: "Format Rows", | ||
| description: "Whether to format rows for display", | ||
| optional: true, | ||
| default: false, | ||
| }, | ||
| pivotResults: { | ||
| type: "boolean", | ||
| label: "Pivot Results", | ||
| description: "Whether to pivot the results", | ||
| optional: true, | ||
| default: false, | ||
| }, | ||
| parameters: { | ||
| type: "string[]", | ||
| label: "Parameters", | ||
| description: "Query parameters as JSON objects. Each parameter should be a JSON string with the parameter properties. Example: `{\"type\": \"category\", \"target\": [\"variable\", [\"template-tag\", \"parameter_name\"]], \"value\": \"your_value\"}`", | ||
| optional: true, | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const { | ||
| app, | ||
| cardId, | ||
| exportFormat, | ||
| formatRows, | ||
| pivotResults, | ||
| parameters, | ||
| } = this; | ||
|
|
||
| // Parse parameters from JSON strings to objects | ||
| const parsedParameters = parameters?.map((param, index) => { | ||
| if (typeof param === "string") { | ||
| try { | ||
| return JSON.parse(param); | ||
| } catch (error) { | ||
| throw new Error(`Invalid JSON in parameter at index ${index}: ${error.message}`); | ||
| } | ||
| } | ||
| return param; | ||
| }); | ||
|
|
||
| const response = await app.exportCardQuery({ | ||
| $, | ||
| cardId, | ||
| exportFormat, | ||
| data: { | ||
| format_rows: formatRows, | ||
| pivot_results: pivotResults, | ||
| ...(parsedParameters && parsedParameters.length > 0 && { | ||
| parameters: parsedParameters, | ||
| }), | ||
| }, | ||
| }); | ||
|
|
||
| $.export("$summary", `Successfully exported query results as ${exportFormat.toUpperCase()}`); | ||
|
|
||
| return response; | ||
| }, | ||
jcortes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.