首页 > 解决方案 > 有没有办法使单元格的一部分变为粗体?

问题描述

我在谷歌表格中有几行从几个选项卡中获取句子,然后在另一个选项卡中返回它们。请注意,这些单元格包含公式。我写了一个脚本,它运行良好,但我不知道要添加什么来使某些单词BOLD

     function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Hide')
      .addItem('Hide All Empty Rows', 'clear')
      .addToUi();
}

function clear() {
    const sheet = SpreadsheetApp.getActive().getSheetByName('NY Quote with Rebate');
    const sheetRANGE = sheet.getDataRange();
    const rangeVals = sheetRANGE.getValues();

    for (let i = rangeVals.length - 1; i >= 0; i--) {
        if (i > 8) {
            const $row = rangeVals[i];
            let data = 0;
            for (const [key, value] of Object.entries($row)) {
                if (value !== '') {
                    data = 1;
                }
            }
            if (data === 0) {
                sheet.hideRows(i + 1);
            }
        }
    }

    const maxRows = sheet.getMaxRows();
    const lastRow = sheet.getLastRow();
    if (maxRows !== lastRow) {
        if (lastRow > 9) {
            sheet.hideRows(lastRow + 1, maxRows - lastRow);
        } else {
            sheet.hideRows(9, maxRows - 9);
        }
    }
}

标签: javascriptgoogle-apps-scriptgoogle-sheets-api

解决方案


您可以创建一个RichTextValue。该方法中有一个相关示例setRichTextValue()

// Sets all cells in range B2:D4 to have the text "Hello world", with "Hello" bolded.
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("B2:D4");
var bold = SpreadsheetApp.newTextStyle()
    .setBold(true)
    .build();
var richText = SpreadsheetApp.newRichTextValue()
    .setText("Hello world")
    .setTextStyle(0, 5, bold)
    .build();
range.setRichTextValue(richText);

推荐阅读