Text to Table

Posted by @dudasimre, Last update 4 months ago

Convert “comma” separated text to Markdown table.
You can add column head titles. Enter column names also separate width “comma”.
If you don’t add column names, the script create 3 empty name columns.

Steps

  • script

    // configurable default variables
    let configFile = "TextToTable.json";
    let config =
    {
    	colsIndex: 2,
    	fieldsAutoCaps: true
    };
    
    let defaultColumnNumbers = ["1","2","3","4","5"];
    let defaultColumnTitles = "";
    let cell = "|";
    let div = "|:--";
    let textDelimiter = ",";
    
    let f = () => {
    	// Check selection
    	let [lnStart, lnLen] = editor.getSelectedLineRange();
    	let lnText = editor.getTextInRange(lnStart, lnLen);
    
    	if (!lnText.length)
    	{
    		let ePrompt = Prompt.create();
    		ePrompt.title = "Error message";
    		ePrompt.message = "First, must be select a text.";
    		ePrompt.isCancellable = false;
    		ePrompt.addButton("Close");
    		ePrompt.show();
    		return false;
    	}
    
    	// create prompt and ask for column names
    	let cfg = readConfig(config);
    	
    	let p = Prompt.create();
    	p.title = "Convert to Table";
    	p.message = "Convert selected text to MultiMarkdown table.";
    	p.isCancellable = true;
    	
    	
    	p.addTextField("titles", "Column titles", defaultColumnTitles, {
    		"placeholder": "Enter column titles",
    		"autocorrect": false,
    		"autocapitalization": "words",
    		"keyboard": "default",
    		"wantsFocus": true
    	});
    	p.addPicker("cols", "Select number of columns", [defaultColumnNumbers], [parseInt(cfg.colsIndex)]);
    	p.addSwitch("capsFields","Field capitalization",cfg.fieldsAutoCaps);
    	p.addButton("Convert", "convert", true);
    	
    	// exit if user cancels
    	if (!p.show()) { return false; }
    	cfg.fieldsAutoCaps = p.fieldValues["capsFields"];
    	
    	// create table
    	let titles = p.fieldValues["titles"];
    
    	let columnTitles = titles.split(textDelimiter);
    
    	columnTitles = columnTitles.map(function(s) { return String.prototype.trim.apply(s); });
    	let cols = columnTitles.length;
    	let header = "";
    	
    	if (!titles.length)
    	{
    		cols = parseInt(defaultColumnNumbers[p.fieldValues["cols"]]);
    		cfg.colsIndex = p.fieldValues["cols"];
    		
    		header = cell + " " + (cell + " ").repeat(cols) + "|\n";
    	}
    	else
    	{
    		header = cell + " " + columnTitles.join(" | ") + " |\n";
    	}
    	writeConfig(cfg);
    	let divider = div.repeat(cols) + "|\n";
    	let tblCells = lnText.split(textDelimiter);
    	tblCells = tblCells.map(function(s) { return String.prototype.trim.apply(s); });
    	if (cfg.fieldsAutoCaps)
    	{
    		tblCells = tblCells.map(function(s) { return s[0].toUpperCase() + s.substring(1); });
    	}
    	
    	
    	let tblHead = header + divider;
    	let tblBody = getRows(tblCells,cols).toString().replace(/,/g,"");
    	let tbl = tblHead + tblBody;
    
    	// Rewrite selected text width Markdown table
    	editor.setSelectedRange(lnStart, lnStart+lnLen);
    	editor.setSelectedText(tbl);
    	editor.activate();
    	editor.setSelectedRange(lnStart+tbl.length+0);
    	
    	return true;
    }
    
    
    if (!f()) { context.cancel(); }
    
    function getRows(cells, cols)
    {
    	var tblRows = [];
    	
    		for (i=0; i<cells.length; i=i+cols)
    	{
    		tblRows.push((cell + " " + cells.slice(i, i+cols).join(" " + cell + " ") + " " + cell).toString().replace(/\n/g, " ") + "\n");
    	}
    	return tblRows;
    }
    
    function readConfig(d)
    {
    	var retValue = d;
    	var f = FileManager.createCloud();
    	var cfgContent = f.readString(configFile);
    	if (cfgContent.length != 0)
    	{
    		retValue = JSON.parse(cfgContent);
    	}
    	
    	return retValue;
    }
    
    function writeConfig(c)
    {
    	var f = FileManager.createCloud();
    	f.writeString(configFile, JSON.stringify(c));
    }
    
    

Options

  • After Success Default
    Notification Info
    Log Level None
Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.