google-apps-script - 设置富文本值不适用于具有不同格式的值
问题描述
我正在尝试组合两个富文本字符串,同时保持它们的格式。
function formatCombi() {
// get the sheet and the page
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// get the plaintext value of both cells, and their combined value
var text1 = sheet.getRange(1,1).getValue();
var text2 = sheet.getRange(1,2).getValue();
var text3 = text1 + text2;
var range = sheet.getRange(1,3);
// get the rich text value of both cells
var rtf1 = sheet.getRange(1,1).getRichTextValue();
var rtf2 = sheet.getRange(1,2).getRichTextValue();
// get the length of both strings and the combined length
var length1 = text1.length;
var length2 = text2.length;
var length3 = length1 + length2;
//get the formats of both strings
var bold1 = rtf1.getTextStyle().isBold();
var bold2 = rtf2.getTextStyle().isBold();
var italic1 = rtf1.getTextStyle().isItalic();
var italic2 = rtf2.getTextStyle().isItalic();
var under1 = rtf1.getTextStyle().isUnderline();
var under2 = rtf2.getTextStyle().isUnderline();
var strike1 = rtf1.getTextStyle().isStrikethrough();
var strike2 = rtf2.getTextStyle().isStrikethrough();
var colour1 = rtf1.getTextStyle().getForegroundColor();
var colour2 = rtf2.getTextStyle().getForegroundColor();
//create style for each string
const style1 = SpreadsheetApp.newTextStyle()
.setUnderline(under1)
.setBold(bold1)
.setItalic(italic1)
.setForegroundColor(colour1)
.build();
const style2 = SpreadsheetApp.newTextStyle()
.setUnderline(under2)
.setBold(bold2)
.setItalic(italic2)
.setForegroundColor(colour2)
.build();
//set the style for string based on the lengths
var rtf3 = SpreadsheetApp.newRichTextValue()
.setText(text3)
.setTextStyle(0,length1,style1)
.setTextStyle(length1,length3,style2)
.build();
range.setRichTextValues(rtf3);
}
这是我到目前为止的代码,但由于某种原因,它总是吐出相同的错误代码:
Exception: The parameters (String) don't match the method signature for SpreadsheetApp.Range.setRichTextValues.
formatCombi @ Code.gs:70
有人知道为什么吗?如果我不能将 RichTextBuilder 设置为一个值,我看不到它能够具有多种格式的意义,并且文档中的示例工作正常。
解决方案
推荐阅读
- symfony - 是否可以使用 League csv 逐列读取 csv 文件?
- php - 如何使用soap api从magento 1.9获取关联产品ID?
- python - 为什么 grequests.get() 返回无?
- python - Python 脚本在 Jupyter 但不是命令行中工作,listdir 错误
- scala - 使用 Akka parquet 读取 fastparquet 文件
- r - 如何将单词列表(chr)与数据帧中多列中的值进行比较,并在 R 中匹配时输出二进制响应
- configuration - Hazelcast 中是否有与 Coherence tangosol.coherence.distributed.localstorage 配置等效的配置?
- python - 我想安装 maven 以使用软件调用 GROBID
- python - DataLoader num_workers 与 torch.set_num_threads
- python - Django 查询集:order_by ForeignKey 布尔属性