// ==UserScript==
// @name TSV Timesheet Importer
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Allow TSV Import for Timesheet
// @author Roshan Gonsalkorale (roshan.gonsalkorale@oracle.com)
// @include https://global-ebusiness.oraclecorp.com*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Ensure only runs on timesheets
if (document.title.indexOf("Time Entry: ") === 0) {
//*
/*
#################################
### A. ADD REQUIRED LIBRARIES ###
#################################
*/
// ADD ALERTIFY PLUGIN
var file_adder = function(type, src, callback) {
if (type === "css") {
var file = document.createElement("link");
file.setAttribute("rel", "stylesheet");
file.setAttribute("href", src);
} else {
var file = document.createElement(type);
file.setAttribute("src", src);
if (typeof callback !== "undefined") {
file.setAttribute("onload", callback());
};
}
document.body.appendChild(file);
};
// Alertify
file_adder("script", "https://cdn.rawgit.com/alertifyjs/alertify.js/v1.0.10/dist/js/alertify.js");
// jQuery
file_adder("script", "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.js");
//*/
/*
#####################################
### B. IMPORT TSV DIALOG FUNCTION ###
#####################################
*/
window.importTSV = function() {
// TRIGGER ENTRY PROMPT
alertify.defaultValue("PASTE TSV HERE").prompt("Please paste your correctly formatted TSV in here",
function(val, ev) {
// The click event is in the event variable, so you can use it here.
ev.preventDefault();
try {
// declare data object
window.tsv_data = {};
// The value entered is availble in the val variable.
window.tsv_data.all_data = val.split(' ');
/*
###########################
### 1. CREATE DATA ROWS ###
###########################
*/
// Split data into rows
window.tsv_data.header_vs_data = val.split("(END)");
window.tsv_data.number_of_columns = window.tsv_data.header_vs_data[0].split('\t').length;
window.tsv_data.data_only = window.tsv_data.header_vs_data[1].split('\t');
// clean and split data in columns
window.tsv_data.rows = {};
window.tsv_data.row_number = 0;
window.tsv_data.rows[window.tsv_data.row_number] = [];
// Declare dud cell finder
window.tsv_data.dud_cell_multipler = window.tsv_data.number_of_columns - 1;
window.tsv_data.dud_cell_finder = window.tsv_data.dud_cell_multipler;
for (var i = 0; i < window.tsv_data.data_only.length; i++) {
// Kill undefined cells
if (typeof window.tsv_data.data_only[i] === "undefined") {
window.tsv_data.data_only.splice(i, 1);
continue;
}
// Split end of row items (where missing tab)
if (i === window.tsv_data.dud_cell_finder) {
window.tsv_data.new_cells = window.tsv_data.data_only[i].split(/ (.+)?/); // create new cells by splitting old
window.tsv_data.data_only.splice(i, 1); // delete old cell
window.tsv_data.data_only.splice(i, 0, window.tsv_data.new_cells[0], window.tsv_data.new_cells[1]); // insert new cells
window.tsv_data.dud_cell_finder = window.tsv_data.dud_cell_multipler + window.tsv_data.dud_cell_finder + 1; // increment dud cell finder after new cell added
}
// clean data
window.tsv_data.data_only[i] = window.tsv_data.data_only[i].trim();
}
// delete first cell to clean up splitting below
window.tsv_data.data_only.splice(0, 1); // delete old cell
// calculate next row to split on
window.tsv_data.row_split_multipler = window.tsv_data.number_of_columns;
window.tsv_data.row_split = window.tsv_data.number_of_columns - 1;
// Loop through and split data into rows
for (var i = 0; i < window.tsv_data.data_only.length; i++) {
if (i === window.tsv_data.row_split) {
window.tsv_data.row_number++; // increase row number
window.tsv_data.rows[window.tsv_data.row_number] = [];
window.tsv_data.row_split = i + window.tsv_data.row_split_multipler;
}
window.tsv_data.rows[window.tsv_data.row_number].push(window.tsv_data.data_only[i]);
}
//delete first row
delete window.tsv_data.rows["0"];
/*
############################################
### 2. POPULATE FIELDS ON PAGE WITH DATA ###
############################################
*/
// Calculate Input Cell IDs
window.tsv_data.column_values = [3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6];
window.tsv_data.cell_names = ["A2~#N1display", "A2~#N1display", "A2~#N1display", "A2~#N1display", "A2~#N1display", "B22_#_~", "B22_#_~", "B22_#_~", "B22_#_~", "B22_#_~", "B22_#_~", "B22_#_~"];
var varName;
for (varName in window.tsv_data.rows) {
for (var i = 0; i < window.tsv_data.rows[varName].length; i++) {
var column_value = window.tsv_data.column_values[i];
var row_value = varName;
var cell_id = window.tsv_data.cell_names[i].replace(RegExp("~", "g"), column_value);
cell_id = cell_id.replace(RegExp("#", "g"), row_value);
jQuery("#" + cell_id).val(window.tsv_data.rows[varName][i]);
}
}
// Flag successfully imported TSV
alertify.success("TSV Imported Successfully");
} catch (err) {
// Flag import error
alertify.error("Error : " + err);
}
},
function(ev) {
// The click event is in the event variable, so you can use it here.
ev.preventDefault();
alertify.error("Import Cancelled");
}
);
};
/*
#########################
### C. INSERT BUTTONS ###
#########################
*/
window.buttonInsertCount = 1;
var buttonInsert = function() {
setTimeout(function() {
window.buttonInsertCount++;
if (typeof jQuery !== "function" && buttonInsertCount < 50) {
buttonInsert();
} else {
if (typeof jQuery === "function") {
// Import TSV Button
jQuery('#Hxctcafooter').children().children().children().append('<td><img src="/OA_HTML/cabo/images/swan/t.gif" width="5"></td><td><button id="Import TSV" title="Import TSV" class="x7n" style="background-image:url(/OA_HTML/cabo/images/swan/btn-bg1.gif)" onclick="event.preventDefault();importTSV();">Import TSV</button></td>');
// TSV Importer Help Button
jQuery('a[href="javascript:top.help_window()"]').first().parent().after('<td valign="bottom"><a class="x15" onclick="alertify.alert(window.tsvHelpMessage);">TSV Importer Help</a></td>');
}
}
}, 100);
};
buttonInsert(); // attempt to insert button
/*
#############################################
### D. LOG MESSAGE THAT EXTENSION RUNNING ###
#############################################
*/
// Help Box
window.tsvHelpMessage = "<h2>TSV Import Help</h2>" +
"<p>Make a copy of the following <a target='_blank' href='https://docs.google.com/spreadsheets/d/1U2onkbiDWB-MDqXeexbtLHMix43iSp5BxFO38k74VO8/edit?usp=sharing'>template</a>, fill it in and paste into the TSV box</p><br>" +
"<strong>Tips</strong><br>" +
"<br>" +
"<li>Watch this <a target='_blank' href='http://screencast.com/t/ps3cvkfFvlT'>video</a> for a demo</li><br>" +
"<li>Ensure you have enough blank rows in your Oracle timesheet template to populate with your TSV</li><br>" +
"<li>Ensure the values in each cell are <strong>exactly</strong> what the timesheet expects or it will error</li><br>" +
"<strong>Problems?</strong>" +
"<br>" +
"<p>Please contact <a href='mailto:roshan.gonsalkorale@oracle.com'>roshan.gonsalkorale@oracle.com</a></p>";
// Message
setTimeout(function() {
if (window.alertify && window.alertify.log) {
alertify.log('<h3>TSV Importer Extension Active</h3><p><a onClick="alertify.alert(window.tsvHelpMessage);">Click here for help </a></p>');
} else {
}
}, 500);
}
})();