C
C
cajoke59792020-03-28 18:08:14
Google Sheets
cajoke5979, 2020-03-28 18:08:14

How to split text by boldness in google sheets?

Hello! There is a small task that I don't know how to implement. I process the text using scripts in Google spreadsheets, I get the value of the text of the cell: and I need to do the following: if the first line of the cell is bold, add it to one variable, and fill the second with the remaining text, if not bold, then leave one of the variables empty. 5e7f67ddd9078989363961.pngI have no idea how to do this, because I have not found information anywhere on how to determine the boldness of a part of the text and not the cell as a whole. Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Grigory Boev, 2020-03-28
@ProgrammerForever

You need to use a script, look at getRichTextValue () in it

/**
 * Аналог функции split. Разбивает по наличию жирности у текста
 * [ ProgrammerForever (c) 2020 ]
 * @param {"A1"} rangeName Имя ячейки
 * @return Возвращает массив строк с чередующейся жирностью
 * @customfunction
 */
function splitByBold(rangeName) { 
  if (!rangeName) {
    throw "Параметр rangeName не задан. Должен быть адресом ячейки"
  };
  
  var rtv = SpreadsheetApp.getActiveSheet().getRange(rangeName).getRichTextValue();
  
  if (rtv) {
    rtv=rtv.getRuns()
  }else{
    return SpreadsheetApp.getActiveRange().getValue();
  };  
  
  var outData = [rtv[0].getText()];
  var isBold = rtv[0].getTextStyle().isBold();
  var k=0;
  for (var i = 1; i < rtv.length; i++){
    if (rtv[i].getTextStyle().isBold() === isBold){
      outData[k]+= rtv[i].getText();
    }else{
      k+=1;
      outData[k]= rtv[i].getText();
      isBold = rtv[i].getTextStyle().isBold();
    };
  };
  return outData;
}

Demo table

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question