Hi All - I currently have a JS script which I'm using to read data from a CSV file, then to add additional columns to the lister using the filename as a lookup. The CSV is loaded into a Map down the chain from the OnAddColumns method.
Its possible that the user will modify the CSV file, at which point I want to be able to trigger the script to reload the CSV into the Map. From within the script I can call "Script.InitColumns".
Is there a way for me to call the scripts "InitColumns" method from a button on the toolbar? I've tried to create a new internal command "ToolRefresh" using the following code - but it hasn't worked and I'm not convinced this is the best way, despite it showing up in the command editor 'command' dropdown.
function OnAddCommands(addCmdData)
{
var cmd = addCmdData.AddCommand();
cmd.name = "ToolRefresh";
cmd.method = "OnExternalRefresh";
cmd.desc = "Refresh the data file loaded into Tool";
cmd.label = "ToolRefresh";
}
function OnExternalRefresh(scriptCmdData) {
Script.InitColumns
}
That code looks correct, as long as it is part of the same script that adds the columns.
Have you tried adding some DOpus.Output logging into the command function to confirm it is being called, and into the column init function to check it is then called as a result?
Thank you Leo. I do have logging at the start of all functions so I know when they've been fired. Here is some more information and some stripped out code (notably the csv to array function is missing).
The command which powers the 'refresh manually' button. Note that the command 'AToolRefresh' has made it into the command dropdown
This output shows the tool initialising when enabled, then the columns being build after an F5. This is followed by an error when the above button is pressed.
// Init the script
function OnInit(initData) {
DOpus.OutputString("# fnc_OnInit");
initData.name = "ATool";
initData.desc = "Allows the insertion of meta data as additional columns.";
initData.log_prefix = "ATool";
initData.default_enable = true;
initData.config_desc = DOpus.NewMap();
initData.config_desc("OverlayDataFile") = "Path to the overlay data file.";
initData.config.OverlayDataFile = "";
initData.config_desc("RefreshDataOnNavigation") = "Should the overlay data file reload every time you navigate or refresh the lister? (Not recommended for large data files).";
initData.config.RefreshDataOnNavigation = false;
}
// Called to add our columns
function OnAddColumns(addColData){
DOpus.OutputString("# fnc_OnAddColumns");
// Load gMap
Script.vars.set("gmMap",loadOverlay());
if(DOpus.TypeOf(Script.vars.get("gmMap")) == "bool" && Script.vars.get("gmMap") == false){
DOpus.OutputString("ATool is Exiting", true);
return;
}
// For each column, set up function
for(var i = 1; i<h.length; i++){
if(h[i]!=undefined){
var cmd = addColData.AddColumn();
cmd.name = h[i];
cmd.method = "BuildColumns";
cmd.label = h[i];
cmd.autogroup = false;
cmd.autorefresh = true;
cmd.justify = "left";
}
}
}
// Load overlay data from source file and return as reference map
function loadOverlay(){
DOpus.OutputString("# fnc_loadOverlay");
// Global map to hold our overlay data in
var ggMap = DOpus.Create.Map();
// Init file system object
var fso = new ActiveXObject("Scripting.FileSystemObject");
// Where are we grabbing the overlay from?
if(fso.FileExists(Script.config.OverlayDataFile)){
var fStream = fso.OpenTextFile(Script.config.OverlayDataFile, 1); // RO
DOpus.OutputString("CSV Load Successful: (" + Script.config.OverlayDataFile + ")");
}else{
DOpus.OutputString("Data File Load Failed: (" + Script.config.OverlayDataFile + ")", true);
return false;
}
// Grab the header and present
h = csvToArr(fStream.ReadLine());
h = h[0];
// Load main CSV body data
csvArray = csvToArr(fStream.ReadAll());
// For each line in the overlay text file (Excluding header)
for(var i = 0; i < csvArray.length; i++){
// Create a temporary map to contain all pairs of column > value for this line
var lMap = DOpus.Create.Map();
// Get current line (i = row number)
var lineSplit = csvArray[i];
// Process the columns
for(var ii = 1; ii<h.length; ii++){
if(h[ii]!=undefined){
// Add current column pairing to current line buffer
lMap.merge(DOpus.Create.Map(h[ii],lineSplit[ii]));
}
}
// Save line to global map with filename as index
ggMap.merge(DOpus.Create.Map(lineSplit[0],lMap));
}
// Tidy up after yourself
fStream.Close();
return ggMap;
}
// Called per line per column
function BuildColumns(scriptColData) {
DOpus.OutputString("# fnc_BuildColumns");
gMap = Script.vars.get("gmMap");
if (scriptColData.item.is_dir) {
scriptColData.value = "";
scriptColData.sort = 1;
scriptColData.group = "Directory";
}else{
pk = scriptColData.item.name;
if(gMap.exists(pk)){
scriptColData.value = gMap(pk)(scriptColData.col);
}else{
scriptColData.value = "";
}
}
}
function OnBeforeFolderChange(BeforeFolderChangeData){
DOpus.OutputString("# fnc_OnBeforeFolderChange");
if(Script.config.RefreshDataOnNavigation == true){
DOpus.OutputString("# fnc_BeforeFolderChange - Refreshing");
Script.InitColumns
}
}
function OnAddCommands(addCmdData)
{
DOpus.OutputString("# fnc_OnAddCommands");
var cmd = addCmdData.AddCommand();
cmd.name = "AToolRefresh";
cmd.method = "OnExternalRefresh";
cmd.desc = "Refresh the data file loaded into Tool";
cmd.label = "AToolRefresh";
}
function OnExternalRefresh(scriptCmdData) {
DOpus.OutputString("# fnc_OnExternalRefresh");
Script.InitColumns
}