Skip to content

Instantly share code, notes, and snippets.

@jiju-MS
Last active October 12, 2021 13:50
Show Gist options
  • Save jiju-MS/322125b8508ce8a5f4d69d64c2306506 to your computer and use it in GitHub Desktop.
Save jiju-MS/322125b8508ce8a5f4d69d64c2306506 to your computer and use it in GitHub Desktop.
name: Basic custom function (1)
description: Calculates the volume of a sphere.
host: EXCEL
api_set: {}
script:
content: |
/**
* Create Entity for raw data.
* @customfunction
* @param {string} productID
* @param {string} name
* @param {string} category
* @param {string} picture
* @param {number[][]} years
* @param {number[][]} purchasePrices
* @param {number[][]} sellprices
* @param {number[][]} purchaseAmounts
* @param {number[][]} inventorys
* @returns The volume of the sphere.
*/
function createEntity3(
productID,
name,
category,
picture,
years,
purchasePrices,
sellprices,
purchaseAmounts,
inventorys
) {
var entity = {
type: "Entity",
basicType: "Error",
basicValue: "#VALUE!",
text: name
};
var elements = [];
var sumProfit = 0;
years.forEach((element, index) => {
var profit = (sellprices[index][0] - purchasePrices[index][0]) * purchaseAmounts[index][0];
sumProfit += profit;
var yearEntity = {
type: "Entity",
basicType: "Error",
basicValue: "#VALUE!",
text: element[0].toString()
};
yearEntity["properties"] = {
"Purhcase Price": {
type: "FormattedNumber",
basicValue: purchasePrices[index][0],
numberFormat: '_($* #,##0.00_);_($* (#,##0.00);_($* " -"??_);_(@_)'
},
"Sell Price": {
type: "FormattedNumber",
basicValue: sellprices[index][0],
numberFormat: '_($* #,##0.00_);_($* (#,##0.00);_($* " -"??_);_(@_)'
},
"Purchase Amount": {
type: "Double",
basicValue: purchaseAmounts[index][0]
},
Inventory: {
type: "Double",
basicValue: inventorys[index][0]
},
Profit: {
type: "FormattedNumber",
basicValue: profit,
numberFormat: '_($* #,##0.00_);_($* (#,##0.00);_($* " -"??_);_(@_)'
}
};
elements.push(yearEntity);
});
entity["properties"] = {
category: {
type: "String",
basicValue: category
},
picture: {
type: "WebImage",
basicType: "Error",
basicValue: "#VALUE!",
address: picture
},
years: {
type: "Array",
basicType: "Error",
basicValue: "#VALUE!",
elements: [elements]
},
"Amount of Profit": {
type: "FormattedNumber",
basicValue: sumProfit,
numberFormat: '_($* #,##0.00_);_($* (#,##0.00);_($* " -"??_);_(@_)'
}
};
return entity;
}
language: typescript
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
[email protected]/client/core.min.js
name: Custom Function Sample
description: ''
host: EXCEL
api_set: {}
script:
content: |-
/**
* Create Entity for raw data.
* @customfunction
* @param {string} display
* @param {string} image
* @param {string} productID
* @param {string} typeOfProduct
* @param {string} category
* @param {number[][]} years
* @param {number[][]} purchasePrices
* @param {number[][]} sellprices
* @param {number[][]} purchaseAmounts
* @param {number[][]} inventorys
* @returns The volume of the sphere.
*/
function createEntity(
display,
image,
productID,
typeOfProduct,
category,
years,
purchasePrices,
sellprices,
purchaseAmounts,
inventorys
) {
var entity = {
type: "Entity",
basicType: "Error",
basicValue: "#VALUE!",
text: display + "-" + typeOfProduct
};
var elements = [];
var sumProfit = 0;
years.forEach((element, index) => {
var profit = (sellprices[index][0] - purchasePrices[index][0]) * purchaseAmounts[index][0];
sumProfit += profit;
var yearEntity = {
type: "Entity",
basicType: "Error",
basicValue: "#VALUE!",
text: element[0].toString()
};
yearEntity["properties"] = {
"Purhcase Price": {
type: "FormattedNumber",
basicValue: purchasePrices[index][0],
numberFormat: '_($* #,##0.00_);_($* (#,##0.00);_($* " -"??_);_(@_)'
},
"Sell Price": {
type: "FormattedNumber",
basicValue: sellprices[index][0],
numberFormat: '_($* #,##0.00_);_($* (#,##0.00);_($* " -"??_);_(@_)'
},
"Purchase Amount": {
type: "Double",
basicValue: purchaseAmounts[index][0]
},
Inventory: {
type: "Double",
basicValue: inventorys[index][0]
},
Profit: {
type: "FormattedNumber",
basicValue: profit,
numberFormat: '_($* #,##0.00_);_($* (#,##0.00);_($* " -"??_);_(@_)'
}
};
elements.push(yearEntity);
});
entity["properties"] = {
display: {
type: "String",
basicValue: display
},
typeOfProduct: {
type: "String",
basicValue: typeOfProduct
},
category: {
type: "String",
basicValue: category
},
picture: {
type: "WebImage",
basicType: "Error",
basicValue: "#VALUE!",
address: image
},
years: {
type: "Array",
basicType: "Error",
basicValue: "#VALUE!",
elements: [elements]
},
"Amount of Profit": {
type: "FormattedNumber",
basicValue: sumProfit,
numberFormat: '_($* #,##0.00_);_($* (#,##0.00);_($* " -"??_);_(@_)'
}
};
return entity;
}
/**
* get entities of top.
* @customfunction
* @param {any[][]} entities
* @param {number} year
* @param {string} category
* @param {number} top
* @returns {any[][]}The volume of the sphere.
*
*/
function getTopProift(
entities,
category,
year,
attribution,
top
)
{
entities = entities.map(x => x[0]).filter(x => x.properties.category.basicValue == category);
entities.sort((a, b)=>{
return b.properties.years.elements[year - 2017][0].properties.Profit.basicValue - a.properties.years.elements[year - 2017][0].properties.Profit.basicValue;
})
return entities.slice(0,top).map(x=>[x]);
}
language: typescript
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
[email protected]/client/core.min.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment