有没有办法使单元格的一部分变为粗体?

时间:2020-11-04 21:24:41

标签: javascript google-apps-script google-sheets-api

我在Google工作表中有几行,这些行从多个标签中提取句子,然后在另一个标签中返回它们。请注意,这些单元格包含公式。我写了一个脚本,它能很好地工作,但是我不知道添加什么来使某些单词 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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

// 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);