-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.js
60 lines (49 loc) · 2.35 KB
/
main.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
/* ------------------- Main function -------------------- */
/**
* Imports mutual fund NAV or other data from Morningstar. See complete documentation at mufunds.com.
*
* @param {string} option Asset attribute: nav, date, change, currency, expenses or category.
* @param {string} id Asset identifier (ISIN, ticker or Morningstar ID).
* @param {string} source Source from which obtain the data (check documentation).
* @return The asked information from the fund, according to the selected source.
* @customfunction
*/
function muFunds(option, id, source) {
// First, check if option is valid
if (!(option == "nav" || option == "date" || option == "change" || option == "currency" || option == "expenses" || option == "category" || option == "source")) {
throw new Error( "You have selected an invalid option." );
}
if (!id) {
throw new Error( "Asset identifier is empty." );
}
if (!source || source === 'morningstar') {
return loadFromMorningstarScreener(option, id);
}
// Input already validated
if (source == "morningstar-au" || source == "morningstar-es" || source == "morningstar-de" || source == "morningstar-ie" || source == "morningstar-fr" || source == "morningstar-za" || source == "morningstar-at" || source == "morningstar-be" || source == "morningstar-dk" || source == "morningstar-fi" || source == "morningstar-gb" || source == "morningstar-uk" || source == "morningstar-ch" || source == "morningstar-is" || source == "morningstar-it" || source == "morningstar-pt" || source == "morningstar-no" || source == "morningstar-nl") {
var country = source.substr(12, 2).toLowerCase();
return loadFromMorningstar(option, id, country);
}
if (source == "quefondos") {
return loadFromQuefondos(option, id);
}
// If no compatible source is chosen, return error
throw new Error( "Source is not compatible. Please check the documentation for the compatibility list" );
}
/* ----------- Google Sheets add-on functions ----------- */
// Adds "About µFunds" menu
function onOpen(e) {
SpreadsheetApp.getUi().createAddonMenu()
.addItem('About µFunds', 'showAbout')
.addToUi();
}
// Installation
function onInstall(e) {
onOpen(e);
}
// Opens "About µFunds" page
function showAbout() {
var ui = HtmlService.createHtmlOutputFromFile('about')
.setTitle('About µFunds');
SpreadsheetApp.getUi().showSidebar(ui);
}