Skip to content

Instantly share code, notes, and snippets.

@brainysmurf
Created November 19, 2015 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brainysmurf/ba5772ec9f56650f74e9 to your computer and use it in GitHub Desktop.
Save brainysmurf/ba5772ec9f56650f74e9 to your computer and use it in GitHub Desktop.
var gl = false; // for debugging
function onOpen() {
var ui = DocumentApp.getUi();
ui.createMenu('Actions')
.addItem('Insert newline every N lines', 'actionEveryNLines')
.addItem('Make Table', 'actionAppendTable')
.addSeparator()
.addItem('Blankify', 'actionBlankify')
.addItem('Tokenize Sentences', 'actionTokenizeSentences')
.addItem('Create form', 'actionCreateForm')
.addToUi();
}
function actionCreateForm() {
var doc = DocumentApp.getActiveDocument();
var text = doc.getBody().getText();
var lines = text.split("\n");
var form = FormApp.create('Sample'),
extract, choice;
lines.forEach(function (item, index, _) {
extract = item.match(/{\w+}/g);
item = item.replace(/{\w+}/g, '___________');
var mcitem = form.addMultipleChoiceItem();
mcitem.setTitle(item);
if (extract) {
mcitem.setChoiceValues([extract.map(function (value, index, _) { return value.substring(1, value.length-1)}).join(", ")])
}
});
var ui = DocumentApp.getUi();
var response = ui.alert('Open the form to edit: ', form.getEditUrl(), ui.ButtonSet.OK_CANCEL),
n;
// Process the user's response.
if (response.getSelectedButton() == ui.Button.OK) {
n = int(response.getResponseText());
} else if (response.getSelectedButton() == ui.Button.CANCEL) {
n = undefined;
} else {
n = undefined;
}
}
/**
* Make a empty line every N number of lines for the current document
*
* @param {n} Number of lines until making a new line
* @return none
*/
function everyNLines(n) {
var doc = DocumentApp.getActiveDocument();
var text = doc.getBody().getText();
var lines = text.split("\n");
Logger.log(lines);
arr = [];
for (i = 0; i < n; i++) {
arr[i] = ".*\n";
}
var howmanynewlines = arr.join("");
var regExp = new RegExp('(' + howmanynewlines + ')', "g")
var regExpSplit = text.split(regExp).filter(function (value, index, _) { return value ? true : false });
doc.getBody().setText(regExpSplit.join('\n')); // only one newline because last row already has one
}
function test_everyNLines() {
everyNLines(3);
}
/**
* Very basic parser
*
* @param {text} text to parse
* @param {rowHeader} Reference to the header, or string
* @return The value of
*/
function tokenizeSentences(text) {
var result = text.match( /[^\.!\?]+[\.!\?]+/g ); // http://stackoverflow.com/questions/11761563/javascript-regexp-for-splitting-text-into-sentences-and-keeping-the-delimiter
Logger.log(result);
return result.map(function (value, index, _) { return value.trim(); }).filter(function (value, index, _) { return value.trim().replace(/\n/g, '') ? true : false });
}
function test_tokenizeSentences() {
tokenizeSentences('This is a test to see if you detect sentences. Do you, or not?');
}
function actionTokenizeSentences() {
var body = DocumentApp.getActiveDocument().getBody();
var text = body.getText();
var result = tokenizeSentences(text);
body.setText(result.join("\n"));
}
function actionEveryNLines() {
var ui = DocumentApp.getUi();
var response = ui.prompt('Add newline every N lines', 'Enter "n":', ui.ButtonSet.OK_CANCEL),
n;
// Process the user's response.
if (response.getSelectedButton() == ui.Button.OK) {
n = parseInt(response.getResponseText());
} else if (response.getSelectedButton() == ui.Button.CANCEL) {
n = undefined;
} else {
n = undefined;
}
if (n) {
everyNLines(n);
}
}
function actionAppendTable() {
var body = DocumentApp.getActiveDocument().getBody();
var text = body.getText();
var blankLines = new RegExp("\n{2}")
var blocks = text.split(blankLines);
var cells = [];
blocks.forEach(function (item, index, arr) {
cells.push(item.split("\n"));
});
body.appendTable(cells);
}
/*
tokenize
@param s: string
@return an array of strings
*/
function tokenize(s){
s = s.replace(/(^\s*)|(\s*$)/gi,""); //exclude start and end white-space
s = s.replace(/[ ]{2,}/gi," "); //2 or more space to 1
return s.split(' ');
}
/*
blankify
@param txt: string
@param every: integer. How many words to skip over
@return string
*/
function blankify(txt, every) {
(gl || log) && Logger.log(txt + every);
var tokens = tokenize(txt); // tokens gives us an array with each element representing a word
var tally = 0;
var output = []; // array that will be put back onto the document
for (var i = 0; i < tokens.length; i++) {
var word = tokens[i];
tally += 1;
//(gl || log) && Logger.log(tally);
// don't count if it's a small word
if (word.replace(/[^a-zA-Z]+/g, '').length <= 6) {
(gl || log) && Logger.log(' <=3: ' + word);
output.push(word);
continue;
}
if (tally >= every) {
(gl || log) && Logger.log('blankify: '+word);
var pre = "", post = "";
var after = word.replace(/^[^a-zA-Z]+/g, '');
if (after.length < word.length) {
// There are punctuation chars to handle at the start of the word
var index = word.length - after.length;
pre = word.substring(0, index);
word = word.substring(index);
}
after = word.replace(/[^a-zA-Z]+$/g, '');
if (after.length < word.length) {
// There are puntuation char to handle at the end of the word
var index = word.length - (word.length - after.length);
post = word.substring(index);
word = word.substring(0, index);
}
output.push(pre+ "{"+word+"}" + post);
tally = 0;
} else {
(gl || log) && Logger.log(' : ' + word);
output.push(word)
}
}
return output.join(" "); // this will convert the array into a string where a space separates each element
}
function test_blankify() {
var log = false;
var test_material = "I wonder what happens now.\nLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
var output = blankify(test_material, 10);
update_document(output);
}
/*
update_document
@param txt: array of words
*/
function update_document(txt) {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
body.clear();
body.appendParagraph(txt);
}
/*
clicked_on_blankify
Called when user clicks on the menu
*/
function actionBlankify() {
var log = true;
(gl || log) && Logger.log('clicked_on_blankify');
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var ui = DocumentApp.getUi();
var result = ui.prompt(
'Blank out every _ word',
'Please enter the number to skip (words <= 3 are not counted at all)',
ui.ButtonSet.OK_CANCEL);
// Process the user's response.
var button = result.getSelectedButton();
var text = result.getResponseText();
// When user clicks okay, go through the document text, adding to the a new array (output)
if (button == ui.Button.OK) {
var every = parseInt(text);
var output = blankify(body.getText(), every);
update_document(output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment