Created
October 20, 2021 06:44
-
-
Save jiju-MS/b0a2f013703938a4394957cae1f92e21 to your computer and use it in GitHub Desktop.
Calculates the volume of a sphere.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Basic custom function | |
description: Calculates the volume of a sphere. | |
host: EXCEL | |
api_set: {} | |
script: | |
content: | | |
/** | |
* Calculates the volume of a sphere. | |
* @customfunction | |
* @param {number} entityCase | |
* @returns The volume of the sphere. | |
*/ | |
function SetEntity(entityCase) { | |
switch (entityCase) { | |
case 1: { | |
return { | |
type: "Entity", | |
basicType: "Error", | |
basicValue: "#VALUE!", | |
text: "Entity With API", | |
properties: { | |
TestDouble: { | |
type: "Double", | |
basicType: "Double", | |
basicValue: 1, | |
propertyMetadata: { excludeFrom: { cardView: true } } | |
}, | |
TestString: { type: "String", basicType: "String", basicValue: "Test" } | |
} | |
}; | |
} | |
case 2: { | |
return { | |
type: "Entity", | |
basicType: "Error", | |
basicValue: "#VALUE!", | |
text: "Supermetrics Entity", | |
properties: { | |
"Total spent": { type: "Double", basicType: "Double", basicValue: 1 }, | |
Impressions: { type: "Double", basicType: "Double", basicValue: 304626 }, | |
Clicks: { type: "Double", basicType: "Double", basicValue: 880 } | |
} | |
}; | |
} | |
case 3: { | |
return { | |
type: "Entity", | |
basicType: "Error", | |
basicValue: "#VALUE!", | |
text: "Entity With API", | |
properties: { | |
TestDouble: { | |
type: "Double", | |
basicType: "Double", | |
basicValue: 1, | |
propertyMetadata: { excludeFrom: { cardView: true } } | |
}, | |
TestString: { type: "String", basicType: "String", basicValue: "Test" } | |
} | |
}; | |
} | |
default: { | |
return "Invalid test case"; | |
} | |
} | |
} | |
/** | |
* Calculates the volume of a sphere. | |
* @customfunction | |
* @param {number} entityCase | |
* @returns {any[][]} The volume of the sphere. | |
*/ | |
function SetArray(entityCase) { | |
switch (entityCase) { | |
case 1: { | |
var entity = new CustomFunctions.Entity("First Entity", { | |
TestString: { | |
type: "String", | |
basicValue: "Test" | |
}, | |
TestDouble: { | |
type: "Double", | |
basicValue: 1 | |
} | |
}); | |
var formattedNumber = new CustomFunctions.FormattedNumber(1.234, "0.00"); | |
var error = new CustomFunctions.Error(CustomFunctions.ErrorCode.divisionByZero); | |
return [[entity], [formattedNumber], [error]]; | |
} | |
case 2: { | |
var Entity1 = new CustomFunctions.Entity("First Entity", { | |
TestString: { | |
type: "String", | |
basicValue: "Test" | |
}, | |
TestDouble: { | |
type: "Double", | |
basicValue: 1 | |
} | |
}); | |
var Entity2 = new CustomFunctions.Entity("Second Entity", { | |
TestString: { | |
type: "String", | |
basicValue: "Test2" | |
}, | |
TestDouble: { | |
type: "Double", | |
basicValue: 2 | |
} | |
}); | |
return [ | |
["First", Entity1], | |
["Second", Entity2] | |
]; | |
} | |
case 3: { | |
return [ | |
[ | |
{ | |
type: "Entity", | |
basicType: "Error", | |
basicValue: "#VALUE!", | |
text: "Entity With API", | |
properties: { | |
TestDouble: { | |
type: "Double", | |
basicType: "Double", | |
basicValue: 1, | |
propertyMetadata: { excludeFrom: { cardView: true } } | |
}, | |
TestString: { type: "String", basicType: "String", basicValue: "Test" } | |
} | |
} | |
], | |
[ | |
{ | |
type: "Entity", | |
basicType: "Error", | |
basicValue: "#VALUE!", | |
text: "Entity With API", | |
properties: { | |
TestDouble: { | |
type: "Double", | |
basicType: "Double", | |
basicValue: 1, | |
propertyMetadata: { excludeFrom: { cardView: true } } | |
}, | |
TestString: { type: "String", basicType: "String", basicValue: "Test" } | |
} | |
} | |
] | |
]; | |
} | |
case 4: { | |
} | |
default: { | |
return "Invalid entityCase"; | |
} | |
} | |
} | |
/** | |
* Calculates the volume of a sphere. | |
* @customfunction | |
* @param {any[][] } value | |
* @returns {any[][]} The volume of the sphere. | |
*/ | |
function getRichDataArray(value) { | |
var ret = []; | |
var oneRow = []; | |
if (value instanceof Array) { | |
for (var i = 0; i < value.length; ++i) { | |
for (var j = 0; j < value[i].length; ++j) { | |
var item = value[i][j]; | |
if (item.type == CustomFunctions.Entity.valueType) { | |
oneRow.push(item.text); | |
} else if (item.type == CustomFunctions.FormattedNumber.valueType) { | |
oneRow.push(item.basicValue); | |
} else if (item.type == CustomFunctions.Error.valueType) { | |
oneRow.push(item.basicValue); | |
} else if (item.type == CustomFunctions.WebImage.valueType) { | |
oneRow.push(item.address); | |
} else { | |
oneRow.push(item); | |
} | |
} | |
} | |
} | |
ret.push(oneRow); | |
return ret; | |
} | |
/** | |
* Calculates the volume of a sphere. | |
* @customfunction | |
* @param {any} value | |
* @param {string} attribute | |
* @returns {any} The volume of the sphere. | |
*/ | |
function getRichData(value, attribute) { | |
// return JSON.stringify(value); | |
if (value.type == CustomFunctions.Entity.valueType) { | |
if (attribute == "text") return value.text; | |
else { | |
return value.properties[attribute].basicValue; | |
} | |
} else if (value.type == CustomFunctions.FormattedNumber.valueType) { | |
return value[attribute]; | |
} else if (value.type == CustomFunctions.Error.valueType) { | |
return value[attribute]; | |
} else if (value.type == CustomFunctions.WebImage.valueType) { | |
if (attribute == "attribution" || attribute == "provider") return JSON.stringify(value[attribute]); | |
else return value[attribute]; | |
} else { | |
return "no richData detected"; | |
} | |
} | |
language: typescript | |
libraries: | | |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js | |
@types/office-js | |
core-js@2.4.1/client/core.min.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment