javascript - 有没有办法使单元格的一部分变为粗体?
问题描述
我在谷歌表格中有几行从几个选项卡中获取句子,然后在另一个选项卡中返回它们。请注意,这些单元格包含公式。我写了一个脚本,它运行良好,但我不知道要添加什么来使某些单词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);
}
}
}
解决方案
您可以创建一个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);
推荐阅读
- atom-editor - Atom 编辑器 - 快速重命名变量的多项选择无法识别范围
- python - 使用带有 queue.put() 的地图?
- c# - 运行多个 Http 调用时的 Sigabrt -6 xamarin.forms 线程池
- php - Phalcon - ::find() 与关系条件
- webpack - manifest.json 中缺少条目
- javascript - 在这种情况下,document.write 会给我带来麻烦吗?
- c# - 预订引擎中的体验商店日期时间
- http - IIS 网站只允许 https 连接,不允许 http
- node.js - Joi 正则表达式不被识别为正则表达式模式
- amazon-web-services - CloudFormation 因小写而无法找到 RDS 子网组