-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamfinav.gs
94 lines (76 loc) · 2.35 KB
/
amfinav.gs
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
/**
* @OnlyCurrentDoc
*/
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Mutual Fund NAV')
.addItem('Scheme data', 'getSchemes')
.addItem('Refresh data', 'getMutualFundNav')
.addItem("Get historical data", 'getSchemeHistorical')
.addSeparator()
.addItem("Clear sheet", 'clearSheet')
.addToUi();
}
/**
* @OnlyCurrentDoc
*/
function getSchemeHistorical() {
var ui = SpreadsheetApp.getUi();
var userProperties = PropertiesService.getUserProperties();
var api_key = userProperties.getProperty("")
var result = ui.prompt('Get data for an scheme','',ui.ButtonSet.OK_CANCEL);
var button = result.getSelectedButton();
var user_input = result.getResponseText().replace(/\s+/g, '');
if (button == ui.Button.OK) {
if (user_input) {
ui.alert('Data populated','',ui.ButtonSet.OK);
}
}
}
function getMutualFundNav() {
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
var url = "https://www.amfiindia.com/spages/NAVAll.txt";
var html = UrlFetchApp.fetch(url).getContentText();
var lines = html.split('\n');
var sheet = []
sheet.push(lines[0].split(';'))
lines.forEach(function(line) {
if (!isNaN(line[0]) && line[0] != " " && line[0] != "\r"){
var scheme = line.split(';')
sheet.push(scheme)
}
});
activeSheet.getRange(1, 1, sheet.length, sheet[0].length).setValues(sheet);
activeSheet.setFrozenRows(1);
}
function getSchemes(){
var url = "http://portal.amfiindia.com/DownloadSchemeData_Po.aspx?mf=0"
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var activeSheet = ss.getSheetByName("Schemes");
if (activeSheet == null){
activeSheet = ss.insertSheet("Schemes")
}
var html = UrlFetchApp.fetch(url).getContentText();
var lines = html.split('\n');
var sheet = []
var len = lines[0].length;
lines.forEach(function(line) {
if (line[0] != " " && line[0] != "\r"){
var scheme = line.split(',')
if (scheme.length > 7){
sheet.push(scheme)
}
}
});
activeSheet.getRange(1, 1, sheet.length, sheet[1].length).setValues(sheet)
activeSheet.setFrozenRows(1);
}
function clearSheet(){
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
activeSheet.clear();
}