@@ -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