Skip to content

Instantly share code, notes, and snippets.

@mhawksey
Created November 21, 2011 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mhawksey/1383136 to your computer and use it in GitHub Desktop.
Save mhawksey/1383136 to your computer and use it in GitHub Desktop.
Google Apps Script to turn line breaked text and wiki markup links for UI Services
// http://goodoldhacking.blogspot.com/2011/01/workaround-for-html-in-apps-script.html
/**
* Returns a widget formatted with the text.
* Modified my mhawksey for inline links
*/
function formatTextPanel(a, text) {
var app = a ;
var panel = app.createFlowPanel();
var lines = text.split("\n");
for (var i=0; i<lines.length; i++) {
var blocks = lines[i].split(/[\[](http.*)[!\]]/g);
var cleaned = removeLeadingWhiteSpace(lines[i]);
for (var j=0; j<blocks.length; j++) {
var p = blocks[j].replace(/[\[\]]/g, '').split(/ /);
var link = p.shift();
if (link.substr(0,4)=="http"){
var label = app.createAnchor((p.length ? p.join(' ') : link),link);
} else {
if (blocks[j]!=lines[i]){
var label = app.createInlineLabel(blocks[j]);
} else {
var label = app.createLabel(lines[i]);
}
}
panel.add(label);
}
if (cleaned[1].length == 0) {
var label = app.createLabel(cleaned[1]);
label.setText("-");
label.setStyleAttribute("visibility", "hidden");
}
panel.add(label);
}
return panel;
}
/**
* Remove whitespaces from start and report how many.
*/
function removeLeadingWhiteSpace(text) {
var i = 0;
var res = [];
while (i < text.length && text[i] == ' ') {
i = i+1;
}
res[0] = i;
res[1] = text.substr(i);
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment