简体   繁体   中英

Google Sheets, Two Font Sizes One Cell

I have a spreadsheet with hundreds of lines of data, coming from data validation lists.

Some of the data is highly relevant, therefore I would like it larger, some is useful but superfluous to most.

Different font size is fine. If anyone is an expert with though, different colours and format would be amazing

I would need the script to be dynamic in the sense that data to the left of the | is formatted one way and to the right of the |in another way.

在此处输入图像描述

I believe your goal as follows.

  • You want to modify the text style of the part of texts in a cell using Google Apps Script (From your tag of google-sheets-macros ).
  • You want to modify CODE of NAME NAME | CODE NAME NAME | CODE .
    • You don't want to modify the text style of NAME NAME + + | + .
    • You want to modify the text style of only CODE .
    • From Different font size is fine. If anyone is an expert with though, different colours and format would be amazing Different font size is fine. If anyone is an expert with though, different colours and format would be amazing , you want to modify the font size and foreground color of the text. In this case, format you expect is the bold and italic?
  • You want to keep the text style of NAME NAME of NAME NAME | CODE NAME NAME | CODE .

I think that above goal can be achieved using RichTextValue. In this answer, I would like to propose the sample script.

Sample situation:

This sample script supposes the sample situation that the value of NAME NAME | CODENAME NAME | CODE are put in the column "A". So in order to test it, please prepare this situation.

Sample script:

Please copy and paste the following script to the container-bound script of Spreadsheet, and set the sheet name.

function myFunction() {
  const sheetName = "Sheet1";  // Please set the sheet name.
  
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName);
  const range = sheet.getRange("A1:A" + sheet.getLastRow());
  const richTextValues = range.getRichTextValues().map(([a]) => {
    const text = a.getText();
    const pos = text.indexOf("|");
    if (pos > -1) {
      const temp = a.getTextStyle(0, pos - 1);
      const textStyle = SpreadsheetApp.newTextStyle()
        .setFontSize(5)
        .setForegroundColor("red")
        .setItalic(true)
        .setBold(true)
        .build();
      return [
        SpreadsheetApp.newRichTextValue()
        .setText(text)
        .setTextStyle(0, pos - 1, temp)
        .setTextStyle(pos + 2, text.length, textStyle)
        .build()
      ];
    }
    return [SpreadsheetApp.newRichTextValue().setText(text).setTextStyle(a.getTextStyle()).build()];
  });
  range.setRichTextValues(richTextValues);
}

Result:

When you run the above script, the text styles of column "A" are reformatted and the text style of CODE is modified, while the text style of NAME NAME of NAME NAME | CODENAME NAME | CODE is not modified. And also, the text style of the cell without |is not modified. You can see the sample result at the following image.

在此处输入图像描述

Note:

  • This sample script is a simple sample. So please modify it for your actual situation.

References:

Added:

When you want to convert the selected range, how about the following sample script? When you use this script, at first, please select the range and run this script. By this, the selected range is converted.

function myFunction() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const select = ss.getSelection();
  const sheet = select.getActiveSheet();
  const range = select.getActiveRange();
  const richTextValues = range.getRichTextValues().map(([a]) => {
    const text = a.getText();
    const pos = text.indexOf("|");
    if (pos > -1) {
      const temp = a.getTextStyle(0, pos - 1);
      const textStyle = SpreadsheetApp.newTextStyle()
        .setFontSize(5)
        .setForegroundColor("red")
        .setItalic(true)
        .setBold(true)
        .build();
      return [
        SpreadsheetApp.newRichTextValue()
        .setText(text)
        .setTextStyle(0, pos - 1, temp)
        .setTextStyle(pos + 2, text.length, textStyle)
        .build()
      ];
    }
    return [SpreadsheetApp.newRichTextValue().setText(text).setTextStyle(a.getTextStyle()).build()];
  });
  range.setRichTextValues(richTextValues);
}
  • If you want to put the converted values to one right side of the column, please modify above script as follows.

    • From

       range.setRichTextValues(richTextValues);
    • To

       range.offset(0, 1).setRichTextValues(richTextValues);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM