Drafts Action Directory

Text to Table

Posted by @dudasimre, Last update 5 days 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.

Install

Steps

  • script

    // configurable default variables
    let defaultColumnNumbers = ["1","2","3","4","5"];
    let defaultIndex = 2;
    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 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], [defaultIndex]);
    	p.addButton("Convert", "convert", true);
    	
    	// exit if user cancels
    	if (!p.show()) { return false; }
    	
    	// 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"]]);
    		header = cell + " " + (cell + " ").repeat(cols) + "|\n";
    	}
    	else
    	{
    		header = cell + " " + columnTitles.join(" | ") + " |\n";
    	}
    	let divider = div.repeat(cols) + "|\n";
    	let tblCells = lnText.split(textDelimiter);
    	tblCells = tblCells.map(function(s) { return String.prototype.trim.apply(s); });
    	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;
    }
    
    
    

Options

  • After Success Default
    Notification Info
    Log Level None

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.