To add to Brianβs answer, I wrote this to convert individual characters to return an array of words or phrases that were highlighted.
function findHighlighted() { var results = []; var body = DocumentApp.getActiveDocument().getBody(), bodyTextElement = body.editAsText(), bodyString = bodyTextElement.getText(), char, len; for (char = 0, len = bodyString.length; char < len; char++) { if (bodyTextElement.getBackgroundColor(char) !== null) results.push([char, bodyString.charAt(char)]); } return results; } function getWords() { var arr = findHighlighted(); var wordList = []; var holding = []; var nextNum, sum; for (var i = 0; i < arr.length; i++) { if (arr[i + 1] === undefined) { nextNum = 0; } else { nextNum = arr[i + 1][0]; } sum = (Number(arr[i][0]) + 1); if (nextNum === sum) { holding.push(arr[i][1]); } else { holding.push(arr[i][1]); wordList.push(holding.join("")); holding = []; } } Logger.log(wordList); }
source share