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

Changing text styles within parentheses (partial cell values)

$
0
0

I'm trying to write a Google Apps Script to italicize any text in parentheses in a specified range in Google Sheets (partial cell formatting). Ideally it would also remove any existing text-only formatting (bold, strikethrough, text size, color, etc) but not cell formatting or text alignment.

  • I worked out the regex search: /\((.*?)\)/g
  • I understand the RichTextValue class is how to achieve this.
  • As a non-coder, I'm having trouble correctly applying these. I tried modeling after this answer in a similar post.

This is the code I've come up with:

function italicizeTextInParentheses() {  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();  var range = sheet.getRange('F:F'); // where to search  var values = range.getValues();  var italic = SpreadsheetApp.newTextStyle().setItalic(true).build();  for (var i = 0; i < values.length; i++) {    var cellValue = values[i][0];    if (typeof cellValue === 'string') {      var textToItalicize = cellValue.match(/\((.*?)\)/g); // what to search      range.getCell(i + 1, 1).SpreadsheetApp.newRichTextValue()      .setText(textToItalicize)      .setTextStyle(italic);    }  }}

How do I fix this to work as intended?


Viewing all articles
Browse latest Browse all 9782

Trending Articles



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