Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Returns definition, example and synonym
- * 0 = nothing | 1 = Bold | 2 = Italic | 3 = Underline | 4 = Strikethrough
- * Example: =RICHTEXT(,"RemcoE33", 1, " made this", 2)
- *
- * @param {"RemcoE33", 1, " made this", 2} args
- * @return {string} Ritchtext string.
- * @customfunction
- */
- function RICHTEXT(...args){
- if(args.length % 2 != 0){
- throw new Error('You probably forgot an formatting option')
- }
- return args.join("|");
- }
- function setRichText_(args, e) {
- const string = args.reduce((acc, curr, i) => {
- if (i % 2 == 0){
- acc += curr
- }
- return acc
- },"");
- console.log(string)
- let richText = SpreadsheetApp.newRichTextValue()
- .setText(string)
- for(let i = 0; i < args.length; i += 2){
- const str = args[i]
- const type = args[i + 1]
- const start = string.indexOf(str)
- const end = start + str.length
- console.log(str, type, start, end)
- switch (type) {
- case '1':
- const bold = SpreadsheetApp.newTextStyle().setBold(true).build()
- richText.setTextStyle(start, end, bold)
- break;
- case '2':
- const italic = SpreadsheetApp.newTextStyle().setItalic(true).build()
- richText.setTextStyle(start, end, italic)
- break;
- case '3':
- const underline = SpreadsheetApp.newTextStyle().setUnderline(true).build()
- richText.setTextStyle(start, end, underline)
- break;
- case '4':
- const strike = SpreadsheetApp.newTextStyle().setStrikethrough(true).build()
- richText.setTextStyle(start, end, strike)
- break;
- }
- };
- e.range.setRichTextValue(richText.build())
- }
- function onEdit(e){
- const range = e.range;
- const formula = range.getFormula()
- const value = range.getValue()
- if(formula.match(/=richtext/gmi) && value != '#ERROR!'){
- const values = value.split("|")
- console.log(values)
- setRichText_(values, e)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement