Tanmay Bairagi
Tanmay Bairagi

Reputation: 624

How to get attributes (Bold, italic etc.) of each word from cell text value in SpreadsheetApp

I need the attributes of each word of a sentence present inside a spreadsheet cell in Google Apps Script.

For example, in the below image, the word "neural network." is in bold. I want to get attributes of each word present in a sentence to identify which word is having what style.

image1

Following is the code I am using to get the cell value (text).

function getSheetsSelection(e) {
  var text = '';
  var ranges = SpreadsheetApp.getActive().getSelection().getActiveRangeList().getRanges();
  for (var i = 0; i < ranges.length; i++) {
    const range = ranges[i];
    const numRows = range.getNumRows();
    const numCols = range.getNumColumns();
    for (let i = 1; i <= numCols; i++) {
      for (let j = 1; j <= numRows; j++) {
        const cell = range.getCell(j, i);
        if (cell.getValue()) {
          var cellTextStyle = cell.getValues()
        }
      }
    }
  }
}

Note: In this code, in the cell variable, we are getting a method named getTextStyle() but it returns the style of whole-cell, not an individual word. Whereas in a DocumentApp, we get the text e.g. as Paragraph or as Text where we can iterate through all the words and get the attributes of them. Is there anything like that paragraph we can perform here in SpreadsheetApp?

Upvotes: 0

Views: 781

Answers (1)

Yuri Khristich
Yuri Khristich

Reputation: 14537

Class RichTextValue

Class TextStyle

Verbose code:

var sheet     = SpreadsheetApp.getActiveSheet();
var range     = sheet.getRange("B1");
var rich_text = range.getRichTextValue();
var runs      = rich_text.getRuns();

var words_bold = [];

for (var r of runs) {
  var style = r.getTextStyle();
  if (style.isBold()) words_bold.push(r.getText());
}

Logger.log(words_bold);

One line function:

const bold_words_from = (cell) => 
  SpreadsheetApp.getActiveSheet().getRange(cell).getRichTextValue().getRuns()
  .filter(r=>r.getTextStyle().isBold()).map(r=>r.getText());

Logger.log(bold_words_from("B1"));

Input:

A B
1 Neuron(Node) It is the basis unit of a neural network.
2 Connections It connects one neuron in one layer to another neuron in other layer
3 Bias(Offset) It is an extra input to neurons and int as always 1
4 Input Shape It is the shape of the input matrix we pass to the input layer.

Output:

[ basis , neural network ]

In the same way you can get italics (isItalic()), etc. But they aren't words. They are 'runs':

A run is the longest unbroken substring having the same text style. For example, the sentence "This kid has two apples." has five words and four runs: ["This ", "kid ", "has two ", "apples."].

I don't know what exactly you're trying to gain. So it's up to you how you will use these runs, if these runs should be aligned with words somehow, etc.

Upvotes: 1

Related Questions