Skip to content

Instantly share code, notes, and snippets.

@chgrossMSFT
Created March 25, 2022 19:26
Show Gist options
  • Select an option

  • Save chgrossMSFT/ec94cc8529756b6672eda13048e8a761 to your computer and use it in GitHub Desktop.

Select an option

Save chgrossMSFT/ec94cc8529756b6672eda13048e8a761 to your computer and use it in GitHub Desktop.
Create a new snippet from a blank template.
name: PTCG Functions
description: Create a new snippet from a blank template.
host: EXCEL
api_set: {}
script:
content: |
/**
* Returns an array of pokemon.
* @customfunction
* @param queryString string name of the repository.
* @returns {any[][]} an array of meme images.
*/
function pokemoncardstest(queryString) {
return new Promise(function(resolve, reject) {
var dummyValue = {
type: "Entity",
text: "MTG CARD",
properties: {}
};
var queryParams = queryString != undefined ? encodeURI(queryString) : "";
var url = "https://api.pokemontcg.io/v2/cards/?q=" + queryParams;
//"https://api.magicthegathering.io/v1/cards";
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(responseJSON) {
var cards = [];
var responseLength = responseJSON.data.length;
for (var x = 0; x < responseLength; x++) {
var card = createCard(responseJSON.data[x]);
cards.push([card]);
}
resolve(cards);
});
});
}
/**
* Returns an array of pokemon.
* @customfunction
* @param queryString string name of the repository.
* @returns {any[][]} an array of meme images.
*/
function pokemoncards(queryString) {
return new Promise(function(resolve, reject) {
var dummyValue = {
type: "Entity",
text: "MTG CARD",
properties: {}
};
var queryParams = queryString != undefined ? encodeURI(queryString) : "";
var url = "https://api.pokemontcg.io/v2/cards/?q=" + queryParams;
//"https://api.magicthegathering.io/v1/cards";
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(responseJSON) {
var cards = [];
var responseLength = responseJSON.data.length;
for (var x = 0; x < responseLength; x++) {
var card = createCard(responseJSON.data[x]);
cards.push([card]);
}
resolve(cards);
});
});
}
/**
* Returns an array of pokemon.
* @customfunction
* @param queryString string name of the repository.
* @returns {any[][]} an array of meme images.
*/
function pokemoncardsets(queryString?) {
return new Promise(function(resolve, reject) {
var dummyValue = {
type: "Entity",
text: "MTG CARD",
properties: {}
};
var queryParams = queryString != undefined ? encodeURI(queryString) : "";
var url = "https://api.pokemontcg.io/v2/sets/?q=" + queryParams;
console.log(url);
//"https://api.magicthegathering.io/v1/cards";
//(url);
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(responseJSON) {
var cards = [];
var responseLength = responseJSON.data.length;
for (var x = 0; x < responseLength; x++) {
var card = createCard(responseJSON.data[x]);
cards.push([card]);
}
resolve(cards);
});
});
}
function createProperty(propertyName, property) {
var outputProp;
const typeFieldName = "type";
var propertyType = Object.prototype.toString.call(property);
var overrides = specialProperties();
var override = overrides[propertyName];
if (override != undefined) {
outputProp = override(property);
} else {
switch (propertyType) {
case "[object String]":
outputProp = {
type: "String",
basicValue: property
};
break;
case "[object Array]":
if (property.length > 0) {
outputProp = {
type: "Array",
elements: []
};
for (var x = 0; x < property.length; x++) {
var entityJSON = property[x];
var newArrayItem;
if (typeof entityJSON == "string") {
newArrayItem = {
type: "String",
basicValue: entityJSON
};
} else {
var displayString = displayStringMap()[propertyName];
newArrayItem = createCard(entityJSON, displayString);
}
outputProp.elements.push([newArrayItem]);
}
}
break;
case "[object Number]":
outputProp = {
type: "Double",
basicValue: property
};
break;
case "[object Object]":
//assuming nesting
outputProp = createCard(property);
break;
case "[object Null]":
//just skip the property
break;
default:
console.log("error parsing: ");
console.log(propertyType); // outputProp[typeFieldName] = "error";
}
}
return outputProp;
}
function createCardsFromResult(result) {
var cards = [{}];
return cards;
}
function createCard(cardJSON, nameOverride?) {
var providerLogo = "https://mp-assets.tcgplayer.com/img/TCGplayer-logo-primary@2x.png";
var returnObject = {
type: "Entity",
text: displayString,
layouts: {},
properties: {},
provider: {
description: "TCG Player Data",
logoSourceAddress: providerLogo,
logoTargetAddress: "https://tcgplayer.com/"
}
};
var displayString;
if (nameOverride != undefined) {
displayString = cardJSON[nameOverride];
} else if (cardJSON.name != undefined) displayString = cardJSON.name;
else displayString = "Item";
returnObject.text = displayString;
Object.keys(cardJSON).forEach((key) => {
var property = createProperty(key, cardJSON[key]);
var specialKey = propertyNameRemapping()[key];
if (specialKey != undefined) {
returnObject["properties"][specialKey] = property;
//add card view metadata
returnObject["layouts"] = {
card: {
mainImage: {
property: specialKey
}
}
};
} else returnObject["properties"][key] = property;
});
//console.log(returnObject);
return returnObject;
}
function specialProperties() {
const propertiesAndTypes = [];
var dollarFNV = {
type: "FormattedNumber",
basicValue: 0.0,
numberFormat: "$#,##0.00_);($#,##0.00)"
};
propertiesAndTypes["images"] = function(property) {
//check if its the card picture image array or set
var imageProp = property.large;
if (imageProp == undefined) imageProp = property.logo;
var createdProperty = {
type: "WebImage",
address: imageProp
};
return createdProperty;
};
propertiesAndTypes["low"] = function(property) {
var dollarAmount = dollarFNV;
dollarAmount.basicValue = property;
return dollarAmount;
};
propertiesAndTypes["mid"] = function(property) {
var dollarAmount = dollarFNV;
dollarAmount.basicValue = property;
return dollarAmount;
};
propertiesAndTypes["high"] = function(property) {
var dollarAmount = dollarFNV;
dollarAmount.basicValue = property;
return dollarAmount;
};
propertiesAndTypes["market"] = function(property) {
var dollarAmount = dollarFNV;
dollarAmount.basicValue = property;
return dollarAmount;
};
propertiesAndTypes["directLow"] = function(property) {
var dollarAmount = dollarFNV;
dollarAmount.basicValue = property;
return dollarAmount;
};
return propertiesAndTypes;
}
function propertyNameRemapping() {
const remap = [];
remap["images"] = "image";
return remap;
}
function displayStringMap(jsonData?) {
const remap = [];
//remap["legalities"] = "format";
//remap[""]
return remap;
}
language: typescript
libraries: |
https://appsforoffice.microsoft.com/lib/beta/hosted/office.js
https://appsforoffice.microsoft.com/lib/beta/hosted/office.d.ts
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
core-js@2.4.1/client/core.min.js
@types/core-js
jquery@3.1.1
@types/jquery@3.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment