Skip to content

Commit e1c0c87

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

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-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")`
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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,76 @@ 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 {700} 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 = 700) => {
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.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+
output = output.splice(0, num_results);
192+
193+
return output;
194+
};
195+
196+
// query sets endpoint
197+
const scryfallSets_ = (num_results = MAX_RESULTS_) => {
198+
const scryfall_url = 'https://api.scryfall.com/sets';
199+
let data = [];
200+
201+
try {
202+
let response = JSON.parse(UrlFetchApp.fetch(`${scryfall_url}`).getContentText());
203+
204+
if (!response.data) {
205+
throw new Error("No sets from Scryfall");
206+
}
207+
208+
data.push(...response.data);
209+
} catch (error) {
210+
throw new Error(`Unable to retrieve sets from Scryfall: ${error}`);
211+
}
212+
213+
return data;
214+
};

0 commit comments

Comments
 (0)