Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9782

Script to change specify text style within Google Sheet's cells, but I cant seem to style text manually [closed]

$
0
0

I have this script in Google Sheets which find keywords that highlight them for me in the cell. But I also realize, i can no longer do manual text styling within the cells, they get reset to the cell style because the script also reset cell to undo the automative styling when the keywords are removed.

Is it possible to achieve both options?

  1. Automative styling and unstyling of keywords when they are typed in (currently working)
  2. Allow manual text styling to other text that are not indicated as keywords

Here the google sheet i am working on: LinkCredits: Script originated from here

/** @OnlyCurrentDoc */'use strict';/*** Simple trigger that runs each time the user opens the spreadsheet.* Adds a menu item to highlight a phrase.* @param {Object} e The onOpen() event object.*/function onOpen(e) {  SpreadsheetApp.getUi()    .createMenu('Padang Team')    .addItem('Highlight crew involved in selected cells', 'formatPhrasesInText')    .addToUi();  if (!e) console.log(`New "Padang Team" menu was added in the spreadsheet's menu bar.`);}/*** Simple trigger that runs each time the user hand edits the spreadsheet.* @param {Object} e The onEdit() event object.*/function onEdit(e) {  if (!e) throw new Error('Please do not run the onEdit(e) function in the script editor window. '+'It runs automatically when you hand edit the spreadsheet.'  );  formatPhrasesInText(e.range);}/*** Applies rich text formats to currently selected cells.*/function formatPhrasesInText(range = SpreadsheetApp.getActiveRange()) {  const specs = [    { regex: /\bDO\b/gi, textColor: 'red', bold: true, underline: true },    { regex: /\bKB\b/gi, textColor: 'red', bold: true, underline: true },    { regex: /\bDC\b/gi, textColor: 'red', bold: true, underline: true },    { regex: /\bAL\b/gi, textColor: 'red', bold: true, underline: true },    { regex: /\bMG\b/gi, textColor: 'red', bold: true, underline: true },    { regex: /\bYY\b/gi, textColor: 'red', bold: true, underline: true },    { regex: /\bDS\b/gi, textColor: 'red', bold: true, underline: true },    { regex: /\bNS\b/gi, textColor: 'red', bold: true, underline: true },    { regex: /\bHL\b/gi, textColor: 'red', bold: true, underline: true }  ];  formatText(range, specs);}/*** Applies rich text formats to text in a range.* Formats substrings that match a regex, each substring separately.* Handles multiple occurrences of each phrase in each cell.* Skips formula cells. Mutates specs.** @param {Range} range The range to format.* @param {Object[]} specs An array of objects { {RegExp} regex, {String} textColor, {Boolean} bold, {Boolean} underline }* @return {Range} The same range, for chaining.*/function formatText(range, specs) {  const values = range.getDisplayValues();  const formulas = range.getFormulas();  let match;  const formattedText = values.map((row, rowIndex) => row.map((value, columnIndex) => {    if (formulas[rowIndex][columnIndex]) return value;    const richText = SpreadsheetApp.newRichTextValue().setText(value);    specs.forEach(spec => {      while (match = spec.regex.exec(value)) {        spec.format = spec.format || SpreadsheetApp.newTextStyle()          .setForegroundColor(spec.textColor)          .setBold(spec.bold)          .setUnderline(spec.underline)          .build();        richText.setTextStyle(match.index, match.index + match[0].length, spec.format);      }    });    return richText.build();  }));  range.setRichTextValues(formattedText);}

Viewing all articles
Browse latest Browse all 9782

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>