This repository was archived by the owner on Aug 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathHome.js
More file actions
223 lines (178 loc) · 7.64 KB
/
Home.js
File metadata and controls
223 lines (178 loc) · 7.64 KB
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
/// <reference path="/Scripts/FabricUI/MessageBanner.js" />
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
(function () {
"use strict";
var accessToken = {};
var dlg;
var messageBanner = null;
Office.initialize = function (reason) {
$(document).ready(function () {
$('#btnSignIn').click(signIn);
$('#btnSignOut').click(signOut);
$('#btnGetExpenses').click(getExpenses);
$('#btnCreateChart').click(addChart);
$("#welcomePanel").show();
$("#actionsPanel").hide();
// Initialize the FabricUI notification mechanism and hide it
messageBanner = new fabric.MessageBanner($(".ms-MessageBanner")[0]);
messageBanner.hideBanner();
checkSignIn();
});
};
function signIn() {
var signInUrl = "https://appcenter.intuit.com/Connect/SessionStart?datasources=quickbooks&grantUrl="
+ encodeURIComponent("https://localhost:44300/OAuthManager.aspx?connect=true");
Office.context.ui.displayDialogAsync(signInUrl,
{ height: 70, width: 40},
function (result) {
dlg = result.value;
dlg.addEventHandler("dialogMessageReceived", processMessage);
});
}
function processMessage(arg) {
//Work around for Mac:
try {
dlg.close();
OSF.ClientHostController.closeDialog();
} catch (e) { }
accessToken = JSON.parse(arg.message);
$.get("/api/setToken?" + $.param(accessToken))
.done(function (data) {
console.log(data);
});
$("#welcomePanel").hide();
$("#actionsPanel").show();
}
function signOut() {
$.get("/api/clearToken", function (data, status) {
accessToken = null;
$("#welcomePanel").show();
$("#actionsPanel").hide();
});
}
function checkSignIn() {
if (typeof accessToken.token == "undefined") {
$.get("api/getToken", function (data, status) {
$("#welcomePanel").hide();
$("#actionsPanel").fadeIn("slow");
});
}
}
function getExpenses() {
var url = "/api/getExpenses?n=100";
$.get(url, function (data, status) {
var tableBody = getFormattedArray(data);
addExpensesSheet(tableBody);
});
}
// Import sample transactions into the workbook
function addExpensesSheet(tableBody) {
// Run a batch operation against the Excel object model
Excel.run(function (ctx) {
// Add a new worksheet to store the transactions
var dataSheet = ctx.workbook.worksheets.add("Expenses");
// Fill white color in the sheet for improved look
dataSheet.getRange("A2:M1000").format.fill.color = "white";
// Add Sheet Title
var range = dataSheet.getRange("B1:E1");
range.values = "Contoso Expenses";
range.format.font.name = "Corbel";
range.format.font.size = 30;
range.format.font.color = "white";
range.merge();
// Fill color in the brand bar
dataSheet.getRange("A1:M1").format.fill.color = "#41AEBD";
// Queue a command to add a new table
var startRowNumber = 2;
var masterTableAddress = 'Expenses!B' + startRowNumber + ':G' + (startRowNumber + tableBody.length).toString();
var masterTable = ctx.workbook.tables.add(masterTableAddress, true);
masterTable.name = "ExpensesTable";
// Queue a command to get the newly added table
masterTable.getHeaderRowRange().values = [["DATE", "AMOUNT", "MERCHANT", "CATEGORY", "TYPEOFDAY", "MONTH"]];
masterTable.getDataBodyRange().formulas = tableBody;
masterTable.columns.getItem("AMOUNT").getRange().numberFormat = '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)';
// Format the table header and data rows
range = dataSheet.getRange('Expenses!B' + startRowNumber + ':G' + startRowNumber);
range.format.font.name = "Corbel";
range.format.font.size = 10;
range.format.font.bold = true;
range.format.font.color = "black";
range = dataSheet.getRange(masterTableAddress);
range.format.font.name = "Corbel";
range.format.font.size = 10;
range.format.borders.getItem('EdgeBottom').style = 'Continuous';
range.format.borders.getItem('EdgeTop').style = 'Continuous';
// Sort by most recent transactions at the top (Date, descending order)
var sortRange = masterTable.getDataBodyRange().getColumn(0).getUsedRange();
sortRange.sort.apply([
{
key: 0,
ascending: false,
},
]);
// Auto-fit columns and rows
dataSheet.getUsedRange().getEntireColumn().format.autofitColumns();
dataSheet.getUsedRange().getEntireRow().format.autofitRows();
// Set the sheet as active
dataSheet.activate();
// Run the queued-up commands, and return a promise to indicate task completion
return ctx.sync();
}).catch(errorHandler);
}
function getFormattedArray(expenses) {
var result = [];
$.each(expenses, function (i, item) {
//var tmpDate = new Date(item.txnDateField);
//var date = tmpDate.getMonth()+1 + "/" + tmpDate.getDate() + "/" + tmpDate.getFullYear();
var date = item.txnDateField.substr(0, 10);
var type = "Unspecified";
switch (item.paymentTypeField) {
case 0:
type = "Cash";
break;
case 1:
type = "Check";
break;
case 2:
type = "Credit Card";
break;
}
var payee = "";
if (item.entityRefField)
payee = item.entityRefField.nameField;
var cat = "";
if (item.lineField.length > 0) {
switch (item.lineField[0].detailTypeField) {
case 5:
cat = item.lineField[0].itemField.accountRefField.nameField;
break;
case "itemBasedExpenseLineDetailField":
cat = item.lineField[0].itemBasedExpenseLineDetailField.itemRefField.nameField;
break;
}
}
var amount = item.totalAmtField;
result.push([date, amount, payee, cat, '=IF(OR((TEXT([DATE], "dddd") = "Saturday"), (TEXT([DATE], "dddd") = "Sunday")), "Weekend", "Weekday")', '=TEXT([DATE], "mmm - yyyy")']);
});
return result;
}
// Helper function for treating errors
function errorHandler(error) {
// Always be sure to catch any accumulated errors that bubble up from the Excel.run execution
showNotification("Error", error);
console.log("Error: " + error);
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
}
// Helper function for displaying notifications
function showNotification(header, content) {
$("#notificationHeader").text(header);
$("#notificationBody").text(content);
messageBanner.showBanner();
messageBanner.toggleExpansion();
}
})();