Skip to content

Commit edd9db7

Browse files
committed
New query for Magic sets as spreadsheet rows.
1 parent 3bf3318 commit edd9db7

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ As it can be difficult to describe how to use a function, here are some examples
4444
### Commander cards not available in foil, with name, set name, release date, color identity, URL, and oracle text, sorted by EDHREC popularity
4545
`=SCRYFALL("-in:foil game:paper legal:commander -is:reprint -is:reserved", "name set_name released_at color url oracle", 150, "edhrec")`
4646

47+
# Listing Sets
48+
49+
Create a spreadsheet of Magic sets.
50+
51+
## Usage
52+
53+
```
54+
SCRYFALLSETS(fields, types, num_results)
55+
56+
* `query`: Scryfall search query
57+
* `fields`: List of fields from a set object to return
58+
* `types`: List of set types to return from Scryfall, "all" set types is default
59+
* `num_results`: Number of results to return (maximum 700)
60+
```
61+
62+
### List set names
63+
`=SCRYFALLSETS("name")`
64+
65+
### List set details and limit the results to specific set types
66+
`=SCRYFALLSETS("name code release type", "core expansion commander", 700)`
67+
4768
# "Bugs"
4869

4970
Note that your search *must* return a result in 30 seconds or less. Asking for too many results can result in

scryfall-google-sheets.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,74 @@ const scryfallSearch_ = (params, num_results = MAX_RESULTS_) => {
139139

140140
return data;
141141
};
142+
143+
/**
144+
* Inserts the sets in Scryfall into your spreadsheet
145+
*
146+
* @param {"name code released_at"} fields List of fields to return from Scryfall, "name" is default
147+
* @param {"core expansion promo"} types List of set types to return from Scryfall, "all" set types is default
148+
* @param {150} num_results Number of results (default 150, maximum 700)
149+
* @return List of Scryfall sets
150+
* @customfunction
151+
*/
152+
const SCRYFALLSETS = (fields = "name", types = "all", num_results = 150) => {
153+
if (num_results > MAX_RESULTS_) {
154+
num_results = MAX_RESULTS_;
155+
}
156+
157+
// do some mapping of fields for convenience
158+
const field_mappings = {
159+
"type": "set_type",
160+
"release": "released_at",
161+
"date": "released_at",
162+
"flavor": "flavor_text",
163+
"parent_code": "parent_set_code",
164+
"url": "scryfall_uri",
165+
}
166+
167+
// allow spaces or comma separated for fields
168+
fields = fields.split(/[\s,]+/);
169+
fields = fields.map(field => field_mappings[field] === undefined ? field : field_mappings[field]);
170+
171+
// allow spaces or comma separated for types
172+
const filter_types = types !== "all";
173+
types = types.split(/[\s,]+/);
174+
175+
const sets = scryfallSets_(num_results);
176+
let output = [];
177+
178+
sets.splice(0, num_results).forEach(set => {
179+
if (filter_types && types.indexOf(set.set_type) < 0) {
180+
return ;
181+
}
182+
183+
let row = [];
184+
fields.forEach(field => {
185+
row.push(set[field] || "");
186+
});
187+
188+
output.push(row);
189+
});
190+
191+
return output;
192+
};
193+
194+
// query sets endpoint
195+
const scryfallSets_ = (num_results = MAX_RESULTS_) => {
196+
const scryfall_url = 'https://api.scryfall.com/sets';
197+
let data = [];
198+
199+
try {
200+
let response = JSON.parse(UrlFetchApp.fetch(`${scryfall_url}`).getContentText());
201+
202+
if (!response.data) {
203+
throw new Error("No sets from Scryfall");
204+
}
205+
206+
data.push(...response.data);
207+
} catch (error) {
208+
throw new Error(`Unable to retrieve sets from Scryfall: ${error}`);
209+
}
210+
211+
return data;
212+
};

0 commit comments

Comments
 (0)