Skip to content

Instantly share code, notes, and snippets.

@jiju-MS
Created November 4, 2021 09:28
Show Gist options
  • Save jiju-MS/8a09a3f1d8060f6afc6186a94f196cb2 to your computer and use it in GitHub Desktop.
Save jiju-MS/8a09a3f1d8060f6afc6186a94f196cb2 to your computer and use it in GitHub Desktop.
Calculates the volume of a sphere.
name: Basic custom function (1)
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: "My Entity",
properties: {
TestDouble: { type: "Double", basicType: "Double", basicValue: 1 },
TestString: { type: "String", basicType: "String", basicValue: "Test" }
}
};
}
case 2: {
return {
type: "FormattedNumber",
basicValue: 1.23,
numberFormat: "0.00%"
};
}
case 3: {
return {
type: "WebImage",
basicType: "Error",
basicValue: "#VALUE!",
address: "https://upload.wikimedia.org/wikipedia/en/c/cd/Marioparty1.jpg"
};
}
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 == "Entity") {
oneRow.push(item.text);
} else if (item.type == "FormattedNumber") {
oneRow.push(item.basicValue);
} else if (item.type == "Error") {
oneRow.push(item.basicValue);
} else if (item.type == "WebImage") {
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) {
if (value.type == "Entity") {
if (attribute == "text") return value.text;
else {
return value.properties[attribute].basicValue;
}
} else if (value.type == "FormattedNumber") {
return value[attribute];
} else if (value.type == "Error") {
return value[attribute];
} else if (value.type == "WebImage") {
return value[attribute];
} else {
return JSON.stringify(value);
}
}
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