Skip to content

Instantly share code, notes, and snippets.

@MakotoE
Last active May 22, 2020 22:25
Show Gist options
  • Save MakotoE/b0b33defbf7128845bdc600bab4c081a to your computer and use it in GitHub Desktop.
Save MakotoE/b0b33defbf7128845bdc600bab4c081a to your computer and use it in GitHub Desktop.
document.ConvertCsv(2); // This assumes your Tab format is the second one on the CSV/Sort bar
function parseDate(s) {
var split = s.split('/');
return new Date(split[2], split[1] - 1, split[0]);
}
var data = [];
// Read the file
var numberOfLines = document.GetLines();
if (numberOfLines >= 2 && document.GetLine(2) === '') {
numberOfLines = 1; // CSV document only has header without data
}
for (var line = 1; line < numberOfLines; line++) {
var rowData = [
document.GetCell(line + 1, 1, eeCellIncludeNone),
parseDate(document.GetCell(line + 1, 2, eeCellIncludeNone)),
];
data.push(rowData);
}
// Separate combined users
var separated = [];
for (var row = 0; row < data.length; row++) {
var split = data[row][0].split(';');
for (var i = 0; i < split.length; i++) {
separated.push([split[i], data[row][1]]);
}
}
// Group by user
// {[key: string]: {data: [[]], earliest: Date, latest: Date, mostOccurrence: Date, occurrence: number, total: number}}
var users = {};
for (var row = 0; row < separated.length; row++) {
if (!(separated[row][0] in users)) {
users[separated[row][0]] = {data: []};
}
users[separated[row][0]].data.push(separated[row]);
}
// At this point, we have parsed the file into useful data.
// alert(JSON.stringify(users, null, ' ')); // To check
// Data analysis
for (var userKey in users) {
var sorted = users[userKey].data.sort(function(a, b) {
return a[1].getTime() - b[1].getTime();
});
users[userKey].earliest = sorted[0][1];
users[userKey].latest = sorted[sorted.length - 1][1];
// Count dates
var dates = {}; // {[key: number]: number}
for (var i = 0; i < sorted.length; ++i) {
if (!(sorted[i][1].getTime() in dates)) {
dates[sorted[i][1].getTime()] = 0;
}
dates[sorted[i][1].getTime()] += 1;
}
var mostOccurrence = {date: [], occurrence: -1};
for (var k in dates) {
if (dates[k] > mostOccurrence.occurrence) {
mostOccurrence = {date: [k], occurrence: dates[k]}
} else if (dates[k] === mostOccurrence.occurrence) {
mostOccurrence.date.push(k);
}
}
users[userKey].mostOccurrence = [];
for (var i = 0; i < mostOccurrence.date.length; i++) {
var date = new Date();
date.setTime(mostOccurrence.date[i]);
users[userKey].mostOccurrence.push(date);
}
users[userKey].occurrence = mostOccurrence.occurrence;
users[userKey].total = sorted.length;
}
// Format the numbers and output to document
editor.NewFile();
document.selection.Text = 'User\tEarliestDate\tLatestDate\tDates_with_Most_Occurences\tMost_Occurence_Number\tTotal';
for (var _ in users) {
document.selection.Text += '\r\n';
}
document.ConvertCsv(2);
function formatDate(d) {
return d.getDate() + '/' + (d.getMonth() + 1) + '/' + d.getFullYear();
}
var line = 2;
for (var userKey in users) {
document.SetCell(line, 1, userKey, eeAutoQuote);
document.SetCell(line, 2, formatDate(users[userKey].earliest), eeAutoQuote);
document.SetCell(line, 3, formatDate(users[userKey].latest), eeAutoQuote);
var mostOccurrenceStr = '';
for (var i = 0; i < users[userKey].mostOccurrence.length; i++) {
mostOccurrenceStr += formatDate(users[userKey].mostOccurrence[i]) + ';';
}
document.SetCell(line, 4, mostOccurrenceStr.substring(0, mostOccurrenceStr.length - 1), eeAutoQuote);
document.SetCell(line, 5, users[userKey].occurrence, eeAutoQuote);
document.SetCell(line, 6, users[userKey].total, eeAutoQuote);
line++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment