-
Notifications
You must be signed in to change notification settings - Fork 118
/
data-types-references.yaml
345 lines (311 loc) · 13.5 KB
/
data-types-references.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
335
336
337
338
339
340
341
342
343
344
345
order: 7
id: excel-data-types-references
name: 'Data types: Entity values with references'
description: 'This sample shows how to create entity values with references to other entity values. An entity value is a container for data, and this container can reference (or contain) other entities within the original entity. One entity can contain multiple additional entities.'
host: EXCEL
api_set:
ExcelApi: '1.16'
script:
content: |
$("#setup").on("click", () => tryCatch(setup));
$("#add-entities-to-table").on("click", () => tryCatch(addEntitiesToTable));
async function addEntitiesToTable() {
// This function creates entity values with references to other entity values.
// It retrieves data for each of the employees in the table on the worksheet,
// creates entity values for each of those employees, and adds the entities
// to the table. Each employee entity references (or contains) other
// employee entities. In this scenario, the purpose of one employee
// referencing another is to show a managerial hierarchy.
await Excel.run(async (context) => {
const employeesTable = context.workbook.tables.getItem("EmployeesTable");
// Refresh the table column with the entity values.
employeesTable.columns.getItemOrNullObject("Employee").delete();
const employeeColumn = employeesTable.columns.add(0, null, "Employee");
// Get employee data from the table.
const dataRange = employeesTable.getDataBodyRange();
dataRange.load("values");
await context.sync();
// Create the entities by mapping the employee IDs in table to the sample
// JSON employee data.
const entities = dataRange.values.map((rowValues) => {
// Get employee properties.
const employee = getEmployee(rowValues[1] /* Employee ID */);
// Collect all the references that are needed to make the employee entity.
const references = collectReferences(employee);
// Create employee entity from employee properties.
const employeeEntity = makeEmployeeEntity(
rowValues[2], /* Last name */
rowValues[3], /* First name */
employee,
references
);
// Collect reference values for the employee entity.
const referencedValues: any[] = [{ type: "Root" }];
references.slice(1).forEach((id) => {
const referencedEmployee = getEmployee(id);
referencedValues.push(
makeEmployeeEntity(referencedEmployee.lastName, referencedEmployee.firstName, referencedEmployee, references)
);
});
employeeEntity.referencedValues = referencedValues;
return [employeeEntity];
});
// Add the complete entities to the Employees Table.
employeeColumn.getDataBodyRange().valuesAsJson = entities;
employeeColumn.getRange().format.autofitColumns();
await context.sync();
});
}
// Helper function to collect all the references for the employee entity.
// A reference means one entity contains another entity.
function collectReferences(employee) {
const references: number[] = [];
collectManagerReferences(employee, references);
return references;
}
// Helper function to collect manager and direct report references for each employee entity.
function collectManagerReferences(employee, references: number[]) {
// Confirm references haven't already been collected for this employee.
if (references.indexOf(employee.employeeID) >= 0) {
return;
}
// Record the reference for the employee.
ensureReferenceExist(references, employee.employeeID);
// Record the reference for the manager, if the employee has a manager.
if (employee.reportsTo != null) {
// Get the manager.
const manager = getEmployee(employee.reportsTo);
// Collect references for the manager.
collectManagerReferences(manager, references);
}
// Collect references for each of the direct reports of the employee, if any.
const directReports = employee.directReports || getDirectReports(employee.employeeID);
if (directReports.length > 0) {
directReports.forEach((direct) => collectManagerReferences(direct, references));
}
}
// Helper function to check whether a specific reference ID exists in a list of IDs.
// If the ID doesn't exist, add it to the list.
function ensureReferenceExist(list: number[], id: number) {
if (list.indexOf(id) < 0) {
list.push(id);
}
}
// Create entities from employee properties.
function makeEmployeeEntity(lastName: string, firstName: string, employee: any, references: number[]): Excel.EntityCellValue {
const entity: Excel.EntityCellValue = {
type: Excel.CellValueType.entity,
text: `${firstName} ${lastName}`,
properties: {
"Employee ID": {
type: Excel.CellValueType.string,
basicValue: employee.employeeID.toString()
},
"Last Name": {
type: Excel.CellValueType.string,
basicValue: lastName
},
"First Name": {
type: Excel.CellValueType.string,
basicValue: firstName
},
Name: {
type: Excel.CellValueType.string,
basicValue: `${firstName} ${lastName}`
},
Title: {
type: Excel.CellValueType.string,
basicValue: employee.title
}
},
layouts: {
card: {
title: { property: "Name" },
sections: [
{
layout: "List",
properties: ["Employee ID", "First Name", "Last Name", "Title"]
}
]
}
}
};
// Add manager reference, if the employee has a manager.
if (employee.reportsTo != null) {
entity.properties["Manager"] = {
type: "Reference",
reference: references.indexOf(employee.reportsTo)
};
entity.layouts.card.sections[0].properties.push("Manager");
}
// Add references for direct reports, if any.
if (employee.directReports.length > 0) {
entity.properties["Direct Reports"] = {
type: Excel.CellValueType.array,
elements: employee.directReports.map((direct) => {
return [
{
type: "Reference",
reference: references.indexOf(direct.employeeID)
}
];
}),
};
entity.layouts.card.sections[0].properties.push("Direct Reports");
}
return entity;
}
// Get employee properties.
function getEmployee(employeeID: number): any {
// Find the employee in the sample data.
const employee = employees.find((e) => e.employeeID === employeeID);
// Add direct reports for the employee.
employee["directReports"] = getDirectReports(employeeID);
return employee;
}
// Get direct reports of employee.
function getDirectReports(employeeID: number): any {
return employees.filter((e) => e.reportsTo === employeeID);
}
/** Set up Sample worksheet with a table of employee data. */
async function setup() {
await Excel.run(async (context) => {
context.workbook.worksheets.getItemOrNullObject("Sample").delete();
const sheet = context.workbook.worksheets.add("Sample");
const employeesTable = sheet.tables.add("A1:D1", true /* hasHeaders */);
employeesTable.name = "EmployeesTable";
employeesTable.getHeaderRowRange().values = [["Employee", "EmployeeID", "LastName", "FirstName"]];
employeesTable.rows.add(
null /* Add at the end. */,
employees.map((e) => [null, e.employeeID, e.lastName, e.firstName])
);
sheet.getUsedRange().format.autofitColumns();
sheet.getUsedRange().format.autofitRows();
sheet.activate();
await context.sync();
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
/** Sample JSON employee data. */
const employees = [
{
employeeID: 1,
lastName: "Davolio",
firstName: "Nancy",
title: "Vice President, Sales",
reportsTo: null
},
{
employeeID: 2,
lastName: "Fuller",
firstName: "Andrew",
title: "Sales Representative",
reportsTo: 1
},
{
employeeID: 3,
lastName: "Leverling",
firstName: "Janet",
title: "Sales Representative",
reportsTo: 4
},
{
employeeID: 4,
lastName: "Peacock",
firstName: "Margaret",
title: "Sales Manager",
reportsTo: 1
},
{
employeeID: 5,
lastName: "Buchanan",
firstName: "Steven",
title: "Sales Representative",
reportsTo: 4
},
{
employeeID: 6,
lastName: "Suyama",
firstName: "Michael",
title: "Sales Representative",
reportsTo: 4
},
{
employeeID: 7,
lastName: "King",
firstName: "Robert",
title: "Sales Representative",
reportsTo: 4
},
{
employeeID: 8,
lastName: "Callahan",
firstName: "Laura",
title: "Inside Sales Coordinator",
reportsTo: 1
},
{
employeeID: 9,
lastName: "Dodsworth",
firstName: "Anne",
title: "Sales Representative",
reportsTo: 4
}
];
language: typescript
template:
content: |
<section class="ms-font-m">
<p>This sample shows how to create entity values with references to other entity values. An entity value is a container for data, and this container can reference (or contain) other entities within the original entity. One entity can contain multiple additional entities.</p>
</section>
<section class="setup ms-font-m">
<h3>Set up</h3>
<button id="setup" class="ms-Button">
<span class="ms-Button-label">Create table</span>
</button>
</section>
<section class="samples ms-font-m">
<h3>Try it out</h3>
<p>To see referenced entities within an entity, take the following steps.</p>
<ol>
<li>Select <strong>Add entity values</strong> to add entity values to the table.
<p><button id="add-entities-to-table" class="ms-Button">
<span class="ms-Button-label">Add entity values</span>
</button></p></li>
<li>Open the <strong>Andrew Fuller</strong> entity card by selecting the icon to the left of this name in the <strong>Employee</strong> column.</li>
<li>In the <strong>Andrew Fuller</strong> entity card, select the <strong>Manager</strong> field. This opens the referenced entity for <strong>Nancy Davolio</strong>.</li>
<li>The referenced <strong>Nancy Davolio</strong> entity itself contains multiple referenced entities in the <strong>Direct Reports</strong> field. Select the <strong>Direct Reports</strong> field to explore the referenced entities for all the employees that report to <strong>Nancy Davolio</strong>.</li>
<li>To navigate back to the original <strong>Andrew Fuller</strong> entity, select the <strong>Back</strong> arrow in the top left corner of the entity card.</li>
</ol>
<p>You can also use the <strong>Extract to grid</strong> button in the card modal window, to the right of the <strong>Manager</strong> or <strong>Direct Reports</strong> fields, to add the referenced entities to the table in new columns.</p>
</section>
language: html
style:
content: |
section.samples {
margin-top: 20px;
}
section.samples .ms-Button, section.setup .ms-Button {
display: block;
margin-bottom: 5px;
margin-left: 20px;
min-width: 80px;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
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