Created
May 22, 2017 12:08
-
-
Save 5argon/3e13bf4964775f1a2d53c510cc975f78 to your computer and use it in GitHub Desktop.
Highlight a character name (5argon : Hello world!) or any lines that begin with # sign. Each character get different colors via the function at the bottom.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @OnlyCurrentDoc | |
* | |
* The above comment directs Apps Script to limit the scope of file | |
* access for this add-on. It specifies that this add-on will only | |
* attempt to read or modify the files in which the add-on is used, | |
* and not all of the user's files. The authorization request message | |
* presented to users will reflect this limited scope. | |
*/ | |
/** | |
* Creates a menu entry in the Google Docs UI when the document is opened. | |
* This method is only used by the regular add-on, and is never called by | |
* the mobile add-on version. | |
* | |
* @param {object} e The event parameter for a simple onOpen trigger. To | |
* determine which authorization mode (ScriptApp.AuthMode) the trigger is | |
* running in, inspect e.authMode. | |
*/ | |
function onOpen(e) { | |
DocumentApp.getUi().createAddonMenu() | |
.addItem('Start', 'formatInkVN') | |
.addToUi(); | |
} | |
/** | |
* Runs when the add-on is installed. | |
* This method is only used by the regular add-on, and is never called by | |
* the mobile add-on version. | |
* | |
* @param {object} e The event parameter for a simple onInstall trigger. To | |
* determine which authorization mode (ScriptApp.AuthMode) the trigger is | |
* running in, inspect e.authMode. (In practice, onInstall triggers always | |
* run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or | |
* AuthMode.NONE.) | |
*/ | |
function onInstall(e) { | |
onOpen(e); | |
} | |
function formatInkVN() { | |
var body = DocumentApp.getActiveDocument().getBody(); | |
//all text! | |
var allText = body.editAsText(); | |
regexAction(allText, "^.*[ ]:[ ]", formatCharacterNames); | |
regexAction(allText, "^#.*", formatHashtags); | |
} | |
function regexAction(allText, regexString, actionOnText) | |
{ | |
var rangeElement = allText.findText(regexString); | |
while( rangeElement != null) | |
{ | |
var matchedText = rangeElement.getElement().asText(); | |
var begin = rangeElement.getStartOffset(); | |
var end = rangeElement.getEndOffsetInclusive(); | |
rangeElement = allText.findText(regexString, rangeElement); //continue from previous point | |
actionOnText(matchedText,begin,end); | |
} | |
} | |
function formatCharacterNames(matchedText,begin,end){ | |
matchedText.setBold(false); //matchedText is the entire line, I want to reset the line first | |
matchedText.setForegroundColor("#000000"); | |
matchedText.setBold(begin, end, true); //Then I bold only the part that RegEx matches. | |
var characterName = parseCharacterName(matchedText, begin, end); | |
matchedText.setForegroundColor(begin,end, characterNameToColor(characterName)); | |
} | |
function formatHashtags(matchedText,begin,end){ | |
matchedText.setBold(begin, end, false); | |
matchedText.setForegroundColor(begin,end,"#BBBBBB"); | |
} | |
function parseCharacterName(text,begin,end) | |
{ | |
var name = text.getText().substring(begin,end-2); //remove 3 characters from the end " : " | |
//Assuming in the parentheses is the character's real name e.g. SunnyDay (Shun) -> Shun | |
var matchArray = name.match(/\((.+)\)/); | |
Logger.log(name); | |
Logger.log(matchArray); | |
if(matchArray != null) | |
{ | |
return matchArray[1]; //index 0 is the original string | |
} | |
else | |
{ | |
return name; | |
} | |
} | |
function characterNameToColor(characterName) | |
{ | |
var mapper = { | |
"Reef" : "#513d04" , | |
"Sui" : "#0e7b93" , | |
"Shun" : "#7d9e2a" , | |
"Ef" : "#202463", | |
"Ave:ra" : "#b5972b", | |
"Hydrangea" : "#665470", | |
"Lua" : "#6b1a17", | |
"Eru" : "#e2a548" | |
} | |
var color = mapper[characterName]; | |
if(color == null) | |
{ | |
return "#000000"; | |
} | |
else | |
{ | |
return color; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment