-
Notifications
You must be signed in to change notification settings - Fork 120
/
data-types-custom-functions.yaml
334 lines (321 loc) · 11.4 KB
/
data-types-custom-functions.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
order: 6
id: excel-data-types-custom-functions
name: 'Data types: Custom functions'
description: This sample shows how to write custom functions that return entity value data types.
host: EXCEL
api_set:
CustomFunctionsRuntime: 1.4
script:
content: |
/**
* Search for products that match a given substring. Try =SCRIPTLAB.DATATYPESCUSTOMFUNCTIONS.PRODUCTSEARCH("chef", false).
* @customfunction
* @param {string} query The string to search for in the sample JSON data.
* @param {boolean} [completeMatch] Define whether the search should be a match of the whole product name or part of the product name. If omitted, completeMatch = false.
* @return {Promise<any[][]>} Search results as one or more data type entity values.
*/
async function productSearch(query: string, completeMatch?: boolean): Promise<any[][]> {
// This function searches a set of sample JSON data for the string entered in the
// custom function, and then returns the search result as one or more entity values.
// Set up an error to use if a matching product doesn't exist in the JSON data.
const notAvailableError = {
type: "Error",
errorType: "NotAvailable"
};
// Search the sample JSON data for matching product names.
try {
if (completeMatch === null) {
completeMatch = false
}
console.log(`Searching for ${query}...`);
const searchResult = await searchProduct(query, completeMatch);
// If the search result is empty, return the error.
if (searchResult.length == 0) {
return [[notAvailableError]];
}
// Create product entities for each of the products returned in the search result.
const entities = searchResult.map((product) => [makeProductEntity(product)]);
return entities;
} catch (error) {
console.error(error);
}
}
// Helper function to create entities from product properties.
function makeProductEntity(product?: any) {
const entity: Excel.EntityCellValue = {
type: "Entity",
text: product.productName,
properties: {
"Product ID": {
type: "String",
basicValue: product.productID.toString() || ""
},
"Product Name": {
type: "String",
basicValue: product.productName || ""
},
"Quantity Per Unit": {
type: "String",
basicValue: product.quantityPerUnit || ""
},
// Add Unit Price as a formatted number.
"Unit Price": {
type: "FormattedNumber",
basicValue: product.unitPrice,
numberFormat: "$* #,##0.00"
},
Discontinued: {
type: "Boolean",
basicValue: product.discontinued || false
}
},
layouts: {
card: {
title: { property: "Product Name" },
sections: [
{
layout: "List",
properties: ["Product ID"]
},
{
layout: "List",
title: "Quantity and price",
collapsible: true,
collapsed: false,
properties: ["Quantity Per Unit", "Unit Price"]
},
{
layout: "List",
title: "Additional information",
collapsed: true,
properties: ["Discontinued"]
}
]
}
}
};
// Add image property to the entity and then add it to the card layout.
if (product.productImage) {
entity.properties["Image"] = {
type: "WebImage",
address: product.productImage || ""
};
entity.layouts.card.mainImage = { property: "Image" };
}
return entity;
}
// Helper function to search the sample JSON product data.
function searchProduct(query: string, completeMatch: boolean): any {
const queryUpperCase = query.toUpperCase();
if (completeMatch === true) {
return products.filter((p) => p.productName.toUpperCase() === queryUpperCase);
} else {
return products.filter((p) => p.productName.toUpperCase().indexOf(queryUpperCase) >= 0);
}
}
/** Sample JSON product data. */
const products = [
{
productID: 1,
productName: "Chai",
supplierID: 1,
categoryID: 1,
quantityPerUnit: "10 boxes x 20 bags",
unitPrice: 18,
discontinued: false,
productImage: "https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/Masala_Chai.JPG/320px-Masala_Chai.JPG"
},
{
productID: 2,
productName: "Chang",
supplierID: 1,
categoryID: 1,
quantityPerUnit: "24 - 12 oz bottles",
unitPrice: 19,
discontinued: false,
productImage: ""
},
{
productID: 3,
productName: "Aniseed Syrup",
supplierID: 1,
categoryID: 2,
quantityPerUnit: "12 - 550 ml bottles",
unitPrice: 10,
discontinued: false,
productImage: "https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Maltose_syrup.jpg/185px-Maltose_syrup.jpg"
},
{
productID: 4,
productName: "Chef Anton's Cajun Seasoning",
supplierID: 2,
categoryID: 2,
quantityPerUnit: "48 - 6 oz jars",
unitPrice: 22,
discontinued: false,
productImage:
"https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Kruidenmengeling-spice.jpg/193px-Kruidenmengeling-spice.jpg"
},
{
productID: 5,
productName: "Chef Anton's Gumbo Mix",
supplierID: 2,
categoryID: 2,
quantityPerUnit: "36 boxes",
unitPrice: 21.35,
discontinued: true,
productImage:
"https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/Okra_in_a_Bowl_%28Unsplash%29.jpg/180px-Okra_in_a_Bowl_%28Unsplash%29.jpg"
},
{
productID: 6,
productName: "Grandma's Boysenberry Spread",
supplierID: 3,
categoryID: 2,
quantityPerUnit: "12 - 8 oz jars",
unitPrice: 25,
discontinued: false,
productImage:
"https://upload.wikimedia.org/wikipedia/commons/thumb/1/10/Making_cranberry_sauce_-_in_the_jar.jpg/90px-Making_cranberry_sauce_-_in_the_jar.jpg"
},
{
productID: 7,
productName: "Uncle Bob's Organic Dried Pears",
supplierID: 3,
categoryID: 7,
quantityPerUnit: "12 - 1 lb pkgs.",
unitPrice: 30,
discontinued: false,
productImage: "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/DriedPears.JPG/120px-DriedPears.JPG"
},
{
productID: 8,
productName: "Northwoods Cranberry Sauce",
supplierID: 3,
categoryID: 2,
quantityPerUnit: "12 - 12 oz jars",
unitPrice: 40,
discontinued: false,
productImage:
"https://upload.wikimedia.org/wikipedia/commons/thumb/0/07/Making_cranberry_sauce_-_stovetop.jpg/90px-Making_cranberry_sauce_-_stovetop.jpg"
},
{
productID: 9,
productName: "Mishi Kobe Niku",
supplierID: 4,
categoryID: 6,
quantityPerUnit: "18 - 500 g pkgs.",
unitPrice: 97,
discontinued: true,
productImage: ""
},
{
productID: 10,
productName: "Ikura",
supplierID: 4,
categoryID: 8,
quantityPerUnit: "12 - 200 ml jars",
unitPrice: 31,
discontinued: false,
productImage: ""
},
];
const categories = [
{
categoryID: 1,
categoryName: "Beverages",
description: "Soft drinks, coffees, teas, beers, and ales"
},
{
categoryID: 2,
categoryName: "Condiments",
description: "Sweet and savory sauces, relishes, spreads, and seasonings"
},
{
categoryID: 3,
categoryName: "Confections",
description: "Desserts, candies, and sweet breads"
},
{
categoryID: 4,
categoryName: "Dairy Products",
description: "Cheeses"
},
{
categoryID: 5,
categoryName: "Grains/Cereals",
description: "Breads, crackers, pasta, and cereal"
},
{
categoryID: 6,
categoryName: "Meat/Poultry",
description: "Prepared meats"
},
{
categoryID: 7,
categoryName: "Produce",
description: "Dried fruit and bean curd"
},
{
categoryID: 8,
categoryName: "Seafood",
description: "Seaweed and fish"
}
];
const suppliers = [
{
supplierID: 1,
companyName: "Exotic Liquids",
contactName: "Charlotte Cooper",
contactTitle: "Purchasing Manager"
},
{
supplierID: 2,
companyName: "New Orleans Cajun Delights",
contactName: "Shelley Burke",
contactTitle: "Order Administrator"
},
{
supplierID: 3,
companyName: "Grandma Kelly's Homestead",
contactName: "Regina Murphy",
contactTitle: "Sales Representative"
},
{
supplierID: 4,
companyName: "Tokyo Traders",
contactName: "Yoshi Nagase",
contactTitle: "Marketing Manager",
address: "9-8 Sekimai Musashino-shi"
},
{
supplierID: 5,
companyName: "Cooperativa de Quesos 'Las Cabras'",
contactName: "Antonio del Valle Saavedra",
contactTitle: "Export Administrator"
},
{
supplierID: 6,
companyName: "Mayumi's",
contactName: "Mayumi Ohno",
contactTitle: "Marketing Representative"
},
{
supplierID: 7,
companyName: "Pavlova, Ltd.",
contactName: "Ian Devling",
contactTitle: "Marketing Manager"
},
{
supplierID: 8,
companyName: "Specialty Biscuits, Ltd.",
contactName: "Peter Wilson",
contactTitle: "Sales Representative"
}
];
language: typescript
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
core-js@2.4.1/client/core.min.js
@types/core-js