Last active
June 27, 2024 10:16
-
-
Save siddhesh/6d803a7bed23526fc5acd103d091ccc6 to your computer and use it in GitHub Desktop.
Handy Google spreadsheet function to download and parse NAVs uploaded regularly by the AMFI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This code is in Public domain. | |
MF_SCHEME_NAME = 3; | |
MF_NAV = 4; | |
MF_DATE = 5; | |
CACHE_NAME = '_mfcache'; | |
function initNAV() { | |
var response = UrlFetchApp.fetch("http://portal.amfiindia.com/spages/NAVAll.txt"); | |
var txt = response.getContentText(); | |
data = Utilities.parseCsv(txt, ";"); | |
var newdata = []; | |
var earnings = SpreadsheetApp.openById(/* Put your Google spreadsheet ID here */); | |
mfcache = earnings.getSheetByName(CACHE_NAME); | |
if (mfcache == null) { | |
mfcache = earnings.insertSheet(CACHE_NAME); | |
} | |
// I have designated a column as a source for MF codes. | |
my_mfs = earnings.getSheetByName('My MF Sheet').getSheetValues(2,2,-1,1); | |
Logger.log(my_mfs); | |
// Filter out unnecessary gunk. | |
for (i = 0, j = 0; i < data.length; i++) | |
if (data[i][MF_NAV] != '') | |
for (k = 0; k < my_mfs.length;k++) | |
if (my_mfs[k][0] == data[i][0] && (j == 0 || newdata[j-1][0] != data[i][0])) | |
newdata[j++] = [data[i][0], data[i][MF_SCHEME_NAME], data[i][MF_NAV], data[i][MF_DATE]]; | |
data = newdata; | |
Logger.log(data); | |
mfcache.clear(); | |
// Delete all columns. | |
if (mfcache.getMaxColumns() > 4) | |
mfcache.deleteColumns(1,mfcache.getMaxColumns() - 4); | |
mfcache.appendRow(['Last Updated', new Date()]) | |
for (r = 0; r < data.length; r++) { | |
mfcache.appendRow(data[r]); | |
} | |
SpreadsheetApp.flush(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment