forked from OfficeDev/Excel-Add-in-JavaScript-SalesTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelpers.js
529 lines (431 loc) · 23.5 KB
/
Helpers.js
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
(function () {
"use strict";
// Create a namespace to hold application-wide settings with primitive data types.
var SalesTrackerApp = window.SalesTrackerApp || {};
SalesTrackerApp.deleteWorksheets = function (worksheetsBefore, strSalesSheet, strTempWorksheetName) {
// Delete the sales or temp worksheet if they already exist.
for (var i = 0; i < worksheetsBefore.items.length; i++) {
if ((worksheetsBefore.items[i].name == strSalesSheet) || (worksheetsBefore.items[i].name == strTempWorksheetName)) {
worksheetsBefore.items[i].delete();
}
}
}
// Ensures that all table data fields are selected in the UI.
SalesTrackerApp.reinitializeUI = function () {
$.each(SalesTrackerApp.CheckBoxElements, function () {
this.check(); // Use the check method on the Office UI Fabric Checkbox component to check the checkbox.
});
var colorRadios = $('.colorChoiceRadio'); // Uses CSS class to identify the radio buttons.
colorRadios.each(function () {
var _this = $(this);
_this.removeClass('is-checked');
});
$("#colorChoice-blue").siblings('label').addClass('is-checked');
}
SalesTrackerApp.setChartColorSettings = function (chartColor, fontColor, catFontColor, valFontColor, chartSettings) {
// Saves chart colors.
chartSettings.solidColor = chartColor;
chartSettings.fontColor = fontColor;
chartSettings.categoryAxisFontColor = catFontColor;
chartSettings.valueAxisFontColor = valFontColor;
}
SalesTrackerApp.changeChartColor = function (chart, chartSettings) {
// Changes the chart colors.
chart.format.fill.setSolidColor(chartSettings.solidColor);
chart.title.format.font.color = chartSettings.fontColor;
chart.axes.categoryAxis.format.font.color = chartSettings.categoryAxisFontColor;
chart.axes.valueAxis.format.font.color = chartSettings.valueAxisFontColor;
}
SalesTrackerApp.setChartColorTheme = function (event) {
var themeChoice = event.data.chartColor;
Excel.run(function (ctx) {
// Get the active worksheet and load its charts.
var activeWorksheet = ctx.workbook.worksheets.getActiveWorksheet();
activeWorksheet.load('charts');
return ctx.sync()
// Then change color theme of the chart.
.then(function () {
var chart = activeWorksheet.charts.getItem(event.data.chartSettings.name);
switch (themeChoice) {
case 'white':
SalesTrackerApp.setChartColorSettings('White', "#41AEBD", '#000000', '#000000', event.data.chartSettings);
SalesTrackerApp.changeChartColor(chart, event.data.chartSettings);
break;
case 'gray':
SalesTrackerApp.setChartColorSettings('DarkSlateGray', "#FFFFFF", '#FFFFFF', '#FFFFFF', event.data.chartSettings);
SalesTrackerApp.changeChartColor(chart, event.data.chartSettings);
break;
case 'blue':
SalesTrackerApp.setChartColorSettings('RoyalBlue', "#FFFFFF", '#FFFFFF', '#FFFFFF', event.data.chartSettings);
SalesTrackerApp.changeChartColor(chart, event.data.chartSettings);
break;
default:
throw new Error(themeChoice + " is not a valid choice.");
}
return ctx.sync();
});
})
.catch(SalesTrackerApp.errorHandler);
}
// This function is called to set up the chart on first data load, and to reinitialize the chart
// when users change chart options in the chart UI.
SalesTrackerApp.changeChart = function (SalesHistory, tempTable, strDateColumn, chartSettings) {
// Queue commands to add a new chart and format it.
var dateColumnRange = tempTable.columns.getItem(strDateColumn).getDataBodyRange();
var dataSourceColumnRange = tempTable.columns.getItem(chartSettings.dataSourceDisplayed).getDataBodyRange();
var chartDataRange = dateColumnRange.getBoundingRect(dataSourceColumnRange);
var chart = SalesHistory.charts.add(chartSettings.type, chartDataRange, Excel.ChartSeriesBy.auto);
chart.setPosition(chartSettings.upperLeftCorner, chartSettings.lowerRightCorner);
chart.title.text = chartSettings.title;
SalesTrackerApp.changeChartColor(chart, chartSettings);
chart.name = chartSettings.name;
}
SalesTrackerApp.setChartDataSource = function (event) {
var sourceChoice = event.data.chartDataSource || 'totalProductSales';
Excel.run(function (ctx) {
// First get the active worksheet.
var activeWorksheet = ctx.workbook.worksheets.getActiveWorksheet();
activeWorksheet.load('charts');
var tempDataSheet = ctx.workbook.worksheets.getItem(event.data.strTempWorksheetName);
return ctx.sync()
.then(function () {
var existingChart = activeWorksheet.charts.getItem(event.data.chartSettings.name);
existingChart.delete();
var tempTable = ctx.workbook.tables.getItem(event.data.strTempTableName);
var SalesHistory = ctx.workbook.worksheets.getItem(event.data.strSalesSheet);
// Change the chart's data source, and then show/hide columns in tempTable. The chart uses tempTable as its
// data source. If several columns are visible at once in tempTable, the chart shows several series.
tempTable.columns.getItem(event.data.chartSettings.dataSourceDisplayed).getDataBodyRange().columnHidden = true;
switch (sourceChoice) {
case 'totalNumberOfUnits':
event.data.chartSettings.dataSourceDisplayed = event.data.strTotalNumberOfUnits;
break;
case 'averageUnitPrice':
event.data.chartSettings.dataSourceDisplayed = event.data.strAverageUnitPriceColumn;
break;
case 'totalTax':
event.data.chartSettings.dataSourceDisplayed = event.data.strTotalTaxColumn;
break;
case 'totalDiscount':
event.data.chartSettings.dataSourceDisplayed = event.data.strTotalDiscountColumn;
break;
case 'totalProductSales':
event.data.chartSettings.dataSourceDisplayed = event.data.strTotalProductSalesColumn;
break;
case 'totalServiceSales':
event.data.chartSettings.dataSourceDisplayed = event.data.strTotalServiceSalesColumn;
break;
default:
throw new Error(sourceChoice + " is not a valid choice.");
}
tempTable.columns.getItem(event.data.chartSettings.dataSourceDisplayed).getDataBodyRange().columnHidden = false;
SalesTrackerApp.changeChart(SalesHistory, tempTable, event.data.strDateColumn, event.data.chartSettings);
return ctx.sync();
});
})
.catch(SalesTrackerApp.errorHandler);
}
SalesTrackerApp.setChartType = function (event) {
var typeChoice = event.data.ctype || 'line';
Excel.run(function (ctx) {
// Get the active worksheet.
var activeWorksheet = ctx.workbook.worksheets.getActiveWorksheet();
activeWorksheet.load('charts');
return ctx.sync()
// Then delete the old chart and create new one of the chosen type.
.then(function () {
var existingChart = activeWorksheet.charts.getItem(event.data.chartSettings.name);
existingChart.delete();
var tempTable = ctx.workbook.tables.getItem(event.data.strTempTableName);
var SalesHistory = ctx.workbook.worksheets.getItem(event.data.strSalesSheet);
// Queue commands to add a new chart and format it.
switch (typeChoice) {
case 'line':
event.data.chartSettings.type = "Line";
break;
case 'column':
event.data.chartSettings.type = "ColumnClustered";
break;
case 'area':
event.data.chartSettings.type = "Area";
break;
default:
throw new Error(typeChoice + " is not a valid choice.");
}
SalesTrackerApp.changeChart(SalesHistory, tempTable, event.data.strDateColumn, event.data.chartSettings);
return ctx.sync();
});
})
.catch(SalesTrackerApp.errorHandler);
}
SalesTrackerApp.setSelectedDate = function () {
// Convert date from date picker format to ISO8601 format.
var date = new Date($(this).val());
date = date.toISOString();
date = date.split("T")[0];
SalesTrackerApp.pStartDate = date;
}
SalesTrackerApp.setTableColor = function (event) {
var colorChoice = event.data.color || 'blue';
// Need to create a binding for the table before you can change its style properties.
var fullyQualifiedTableName = event.data.strSalesSheet + "!" + event.data.tableName;
Office.context.document.bindings.addFromNamedItemAsync(fullyQualifiedTableName, "table", { id: 'salesTable' }, function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log('Action failed. Error: ' + asyncResult.error.message);
} else {
console.log('Added new binding with type: ' + asyncResult.value.type + ' and id: ' + asyncResult.value.id);
// Map color choice to one of the built-in Excel table styles. These values come from
// the Excel UI. The style picker on the ribbon has style names on the pattern
// "<color>, Table Style <weight> <integer>"; for example "White, Table Style Medium 1".
// To get the programmatic equivalent, ignore the color and comma and remove the spaces,
// so this example would be "TableStyleMedium1". (The color is built into the style
// definition.)
var styleNumber = '';
switch (colorChoice) {
case 'black':
styleNumber = '1';
break;
case 'blue':
styleNumber = '2';
break;
case 'gray':
styleNumber = '4';
break;
case 'green':
styleNumber = '7';
break;
case 'orange':
styleNumber = '3';
break;
default:
throw new Error(colorChoice + " is not a valid choice.");
}
var styleName = "TableStyleMedium" + styleNumber;
// Set a new table format.
var newTableFormat = { style: styleName };
Office.select("bindings#salesTable").setTableOptionsAsync(newTableFormat, function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log('Action failed. Error: ' + asyncResult.error.message);
}
});
}
})
}
SalesTrackerApp.toggleColumnVisibility = function (event) {
// Get ID of column checkbox that was changed.
var columnName = event.target.id;
Excel.run(function (ctx) {
// Change the column object to a range object that has a visibility property.
var columnRange = ctx.workbook.tables.getItem(event.data.tableName).columns.getItem(columnName).getDataBodyRange();
columnRange.load('columnHidden');
return ctx.sync()
.then(function () {
if (columnRange.columnHidden === true) {
columnRange.columnHidden = false;
columnRange.format.autofitColumns();
}
else {
columnRange.columnHidden = true;
}
return ctx.sync();
});
})
.catch(SalesTrackerApp.errorHandler);
}
// Navigates to a different DIV when users click on the tab bar.
SalesTrackerApp.newPage = function () {
if (this.id === 'DateTab') {
$('.DateSettings').show().siblings().hide();
}
else if (this.id === 'TableTab') {
$('.TableSettings').show().siblings().hide();
}
else {
$('.ChartSettings').show().siblings().hide();
}
};
SalesTrackerApp.getTodayAsISO8601Date = function () {
var today = new Date();
var pYear = today.getFullYear();
var pMonth = today.getMonth() + 1;
var pDate = today.getDate();
// Convert date to ISO8601 format.
if (pDate < 10) {
pDate = '0' + pDate;
}
if (pMonth < 10) {
pMonth = '0' + pMonth;
}
var pEndDate = '' + pYear + '-' + pMonth + '-' + pDate;
return pEndDate;
}
// Gets sales data, and displays the data in a table and chart.
SalesTrackerApp.getSalesData = function (event) {
// Initially, the table and chart options are hidden.
document.getElementById("ChartTab").hidden = false;
document.getElementById("TableTab").hidden = false;
var pEndDate = SalesTrackerApp.getTodayAsISO8601Date();
$.ajax({
url: 'SampleSalesData.json',
dataType: "json"
})
// Process returned data.
.then(function (data) {
return Excel.run(function (ctx) {
// Define variables that are global to the Excel.run,
// so they are accessible to subsequent "then" calls.
var SalesHistory;
var tempDataSheet;
var tempTable;
var startRowNumber;
var endRowNumber;
var columnHeaderNames;
var masterTableAddress;
var masterTable;
var tempTableAddress;
// Load the worksheets.
var worksheetsBefore = ctx.workbook.worksheets;
worksheetsBefore.load();
return ctx.sync()
// Then, if data was previously loaded, first we need to delete those worksheets, and
// reset the UI.
.then(function () {
SalesTrackerApp.deleteWorksheets(worksheetsBefore, event.data.strSalesSheet, event.data.strTempWorksheetName);
if (event.data.isDataLoaded) {
SalesTrackerApp.reinitializeUI();
}
})
.then(ctx.sync)
// Then (re)create the worksheets.
.then(function () {
SalesHistory = ctx.workbook.worksheets.add(event.data.strSalesSheet);
// tempDataSheet is a hidden sheet with data for the chart.
tempDataSheet = ctx.workbook.worksheets.add(event.data.strTempWorksheetName);
})
.then(ctx.sync)
// Then, create and populate the main and temporary tables.
.then(function () {
startRowNumber = 2;
endRowNumber = startRowNumber;
columnHeaderNames = [[event.data.strDateColumn,
event.data.strTotalNumberOfUnits, event.data.strAverageUnitPriceColumn, event.data.strTotalTaxColumn,
event.data.strTotalDiscountColumn, event.data.strTotalProductSalesColumn,
event.data.strTotalServiceSalesColumn]];
masterTableAddress = event.data.strSalesSheet + '!B' + startRowNumber + ':H' + startRowNumber;
masterTable = SalesTrackerApp.createTable(ctx, event.data.tableName, masterTableAddress, columnHeaderNames);
tempTableAddress = event.data.strTempWorksheetName + '!A1:G1';
tempTable = SalesTrackerApp.createTable(ctx, event.data.strTempTableName, tempTableAddress, columnHeaderNames)
// Add JSON data to an array, and then assign the array to the table.
var tableBodyData = [];
data.SalesTransactions.forEach(function (sale) {
if (sale.Date >= SalesTrackerApp.pStartDate) // Filter JSON data by date.
{
// If the user does NOT enter a product name, add all values to the array.
// Otherwise, filter the JSON data by product name.
if (document.getElementById("product-name").value.length == 0
||
((document.getElementById("product-name").value.length > 0) && (sale.Product == document.getElementById("product-name").value))) {
if (Office.context.requirements.isSetSupported('ExcelApi', 1.2)) {
// If ExcelApi 1.2 is supported, then we can bulk add rows - first to an array, and then to the table.
tableBodyData.push([sale.Date, sale.TotalNumberOfUnits, sale.AverageUnitPrice, sale.TotalTax, sale.TotalDiscount, sale.TotalProductSales, sale.TotalServiceSales]);
}
else {
// Rows must be added to the table one at a time.
masterTable.rows.add(null, [[sale.Date, sale.TotalNumberOfUnits, sale.AverageUnitPrice, sale.TotalTax, sale.TotalDiscount, sale.TotalProductSales, sale.TotalServiceSales]]);
tempTable.rows.add(null, [[sale.Date, sale.TotalNumberOfUnits, sale.AverageUnitPrice, sale.TotalTax, sale.TotalDiscount, sale.TotalProductSales, sale.TotalServiceSales]]);
}
}
endRowNumber++;
}
});
if (tableBodyData.length == 0) {
$("#txtNoDataFound").text('No data matches the values entered. Please try again. For example, enter Keyboard.');
}
else {
$("#txtNoDataFound").text('Click on Table or Charts to change table or chart display options.');
}
if (Office.context.requirements.isSetSupported('ExcelApi', 1.2)) {
masterTable.rows.add(null, tableBodyData);
tempTable.rows.add(null, tableBodyData);
}
// Queue a command to sort by date in descending order.
var sortRange = masterTable.getDataBodyRange();
sortRange.sort.apply([
{
key: 0,
ascending: false,
},
]);
})
.then(ctx.sync)
// Then create the chart.
.then(function () {
// Queue command to add a new chart and format it.
SalesTrackerApp.changeChart(SalesHistory, tempTable, event.data.strDateColumn, event.data.chartSettings);
// Activate the worksheet so the table on it can be accessed.
SalesHistory.activate();
})
.then(ctx.sync)
// Finally, format main table columns, suppress non-data-source columns in temp table,
// and hide the temp worksheet.
.then(function () {
var rangeName = "D3:H" + (endRowNumber);
var currencyFormat = "$#,##0.00";
SalesTrackerApp.formatRangeColumns(SalesHistory, rangeName, currencyFormat);
// To display just one data trend on a chart, the chart is based on a temporary table
// that has all other columns hidden.
var columnsToHide = [
event.data.strTotalNumberOfUnits,
event.data.strTotalProductSalesColumn, event.data.strAverageUnitPriceColumn,
event.data.strTotalTaxColumn, event.data.strTotalDiscountColumn,
event.data.strTotalServiceSalesColumn
];
SalesTrackerApp.suppressNonDataSourceColumns(tempTable, event.data.chartSettings.dataSourceDisplayed, columnsToHide);
tempDataSheet.visibility = 'hidden';
})
.then(function (data) {
console.log("Data has been loaded into worksheet");
event.data.isDataLoaded = true;
});
}) // end Excel.run
.catch(SalesTrackerApp.errorHandler);
})
.fail(function (xhr, textStatus, errorThrown) {
$('#product-data').append("Unable to get data: " + errorThrown);
});
}
SalesTrackerApp.createTable = function (excelContext, tableName, tableAddress, columnHeaderNames) {
var table = excelContext.workbook.tables.add(tableAddress, true);
table.name = tableName;
table.getHeaderRowRange().values = columnHeaderNames;
return table;
}
SalesTrackerApp.formatRangeColumns = function (worksheet, rangeName, currencyFormat) {
// Set the currency formats.
var objRange = worksheet.getRange(rangeName);
objRange.numberFormat = currencyFormat;
// Set the columns to fit the data in them.
worksheet.getUsedRange().getEntireColumn().format.autofitColumns();
worksheet.getUsedRange().getEntireRow().format.autofitRows();
}
SalesTrackerApp.suppressNonDataSourceColumns = function (table, dataSourceDisplayed, columnsToHide) {
// Hide all columns except the data source. "true" means hide.
for (var i = 0; i < columnsToHide.length; i++) {
// Change the visibility on the temp table to control what gets shown on the chart.
table.columns.getItem(columnsToHide[i]).getDataBodyRange().columnHidden = true;
}
// Change the visibility on the temp table to control what gets shown on the chart.
table.columns.getItem(dataSourceDisplayed).getDataBodyRange().columnHidden = false;
}
SalesTrackerApp.errorHandler = function (error) {
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
}
window.SalesTrackerApp = SalesTrackerApp;
})();