Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save weiw11/0f51d030929013caf2445abc32795b1c to your computer and use it in GitHub Desktop.
Save weiw11/0f51d030929013caf2445abc32795b1c to your computer and use it in GitHub Desktop.
Exports principal.com's 401k account history as a .csv file as they only export a .qfx file
// ==UserScript==
// @name Principal 401k Account History to CSV
// @version 0.01
// @description A userscript to export Principal's account history to a .csv file
// @author Wei Wang
// @match https://secure05.principal.com/RetirementServiceCenter/memberview?Contract=*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @run-at document-end
// @grant none
// ==/UserScript==
/* USAGE
To download:
Overview -> Account History -> View more account history
On this screen, pick the desired dates up to 92 days.
For Investment Options, select `Detail by each investment option`.
Contribution types is optional.
Click request detail.
There should be an `export list` button near the top of the screen.
*/
(function () {
'use strict';
let save = async() => {
var myTableArray = [];
let count = 0;
let headers = $("table#ResultTable tbody tr[class*='tableheadopt']");
let numTables = headers.length;
let incr = 0;
let rowBreak = $("table#ResultTable tbody[id*='tBody'] tr").length / numTables;
let fundName = "";
// Append column headers
var colHeadersData = $("table#ResultTable tbody tr[class*='tablesubheadopt7']")[0].cells;
console.log("colHeadersData: " + colHeadersData);
let colHeaders = [];
for (let i = 0; i < colHeadersData.length; i++) {
if (i === 1) {
colHeaders.push("Fund");
}
colHeaders.push(colHeadersData[i].innerText.trim().replace("# of ", ""));
}
myTableArray.push(colHeaders);
$("table#ResultTable tbody[id*='tBody'] tr").each(function(i) {
if ((i / rowBreak) === incr) {
fundName = headers[incr].innerText;
incr = incr + 1;
}
let arrayOfThisRow = [];
let tableData = $(this).find('td');
if (tableData.length > 0) {
tableData.each(function(j) {
if (j === 1) {
arrayOfThisRow.push(fundName);
}
arrayOfThisRow.push($(this).text().trim().replace("$", ""));
});
myTableArray.push(arrayOfThisRow);
}
});
let csvContent = "data:text/csv;charset=utf-8,";
myTableArray.forEach(function(rowArray) {
let row = rowArray.join(",");
csvContent += row + "\r\n";
});
// Add save link and simulate click
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
var fileName = `Principal - ${timestamp()}.csv`;
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
function timestamp() {
const date = new Date();
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} `
+ `${pad(date.getHours())}-${pad(date.getMinutes())}-${pad(date.getSeconds())}`;
}
function pad(n) {
return n < 10 ? `0${n}` : n;
}
};
console.log("Export Script loaded");
var btn = document.createElement("BUTTON");
btn.innerHTML = "Export List";
console.log("Button: " + btn.innerHTML);
btn.onclick = save;
document.getElementsByClassName('container')[0].append(btn);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment