|
| 1 | +import app from "../../metabase.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + key: "metabase-export-query-with-format", |
| 5 | + name: "Export Query with Format", |
| 6 | + 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#tag/apicard/post/api/card/%7Bcard-id%7D/query).", |
| 7 | + version: "0.0.1", |
| 8 | + annotations: { |
| 9 | + destructiveHint: false, |
| 10 | + openWorldHint: true, |
| 11 | + readOnlyHint: true, |
| 12 | + }, |
| 13 | + type: "action", |
| 14 | + props: { |
| 15 | + app, |
| 16 | + cardId: { |
| 17 | + propDefinition: [ |
| 18 | + app, |
| 19 | + "cardId", |
| 20 | + ], |
| 21 | + }, |
| 22 | + exportFormat: { |
| 23 | + type: "string", |
| 24 | + label: "Export Format", |
| 25 | + description: "The format to export the query results in", |
| 26 | + options: [ |
| 27 | + { |
| 28 | + label: "CSV", |
| 29 | + value: "csv", |
| 30 | + }, |
| 31 | + { |
| 32 | + label: "JSON", |
| 33 | + value: "json", |
| 34 | + }, |
| 35 | + { |
| 36 | + label: "XLSX", |
| 37 | + value: "xlsx", |
| 38 | + }, |
| 39 | + { |
| 40 | + label: "API", |
| 41 | + value: "api", |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + formatRows: { |
| 46 | + type: "boolean", |
| 47 | + label: "Format Rows", |
| 48 | + description: "Whether to format rows for display", |
| 49 | + optional: true, |
| 50 | + default: false, |
| 51 | + }, |
| 52 | + pivotResults: { |
| 53 | + type: "boolean", |
| 54 | + label: "Pivot Results", |
| 55 | + description: "Whether to pivot the results", |
| 56 | + optional: true, |
| 57 | + default: false, |
| 58 | + }, |
| 59 | + parameters: { |
| 60 | + type: "string[]", |
| 61 | + label: "Parameters", |
| 62 | + 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\"}`", |
| 63 | + optional: true, |
| 64 | + }, |
| 65 | + }, |
| 66 | + async run({ $ }) { |
| 67 | + const { |
| 68 | + app, |
| 69 | + cardId, |
| 70 | + exportFormat, |
| 71 | + formatRows, |
| 72 | + pivotResults, |
| 73 | + parameters, |
| 74 | + } = this; |
| 75 | + |
| 76 | + // Parse parameters from JSON strings to objects |
| 77 | + const parsedParameters = parameters?.map((param, index) => { |
| 78 | + if (typeof param === "string") { |
| 79 | + try { |
| 80 | + return JSON.parse(param); |
| 81 | + } catch (error) { |
| 82 | + throw new Error(`Invalid JSON in parameter at index ${index}: ${error.message}`); |
| 83 | + } |
| 84 | + } |
| 85 | + return param; |
| 86 | + }); |
| 87 | + |
| 88 | + const response = await app.exportCardQuery({ |
| 89 | + $, |
| 90 | + cardId, |
| 91 | + exportFormat, |
| 92 | + data: { |
| 93 | + format_rows: formatRows, |
| 94 | + pivot_results: pivotResults, |
| 95 | + ...(parsedParameters && parsedParameters.length > 0 && { |
| 96 | + parameters: parsedParameters, |
| 97 | + }), |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + $.export("$summary", `Successfully exported query results as ${exportFormat.toUpperCase()}`); |
| 102 | + |
| 103 | + return response; |
| 104 | + }, |
| 105 | +}; |
0 commit comments