json - 如何让 Google Apps Script 告诉 Google Docs 以 JSON 表示的粗体、斜体、删除线和下划线?
问题描述
我写了一个函数,setParagraphToRichText
它接受一个 GoogleAppsScript.Document.Paragraph 和一个字符串。该字符串是一个字符串化的 JSON blob,它定义了给定段落的文本和属性。
/**
*
*
* @param {GoogleAppsScript.Document.Paragraph} paragraph
* @param {string} jsonString
*/
function setParagraphToRichText(paragraph, jsonString) {
const text = paragraph.editAsText();
const json = JSON.parse(jsonString);
const texts = json.runs.reduce((accumulator, current) => {
return accumulator + current.text;
}, "");
text.setText(texts);
for (let i = 0; i < json.runs.length; i++) {
const run = json.runs[i];
if (run.endIndex >= texts.length) run.endIndex--;
text.setLinkUrl(run.startIndex, run.endIndex, run.linkUrl)
.setBold(run.startIndex, run.endIndex, run.isBold)
.setItalic(run.startIndex, run.endIndex, run.isItalic)
.setStrikethrough(run.startIndex, run.endIndex, run.isStrikethrough)
.setUnderline(run.startIndex, run.endIndex, run.isUnderline)
.setFontFamily(run.startIndex, run.endIndex, run.textStyle.fontFamily)
.setFontSize(run.startIndex, run.endIndex, run.textStyle.fontSize)
.setForegroundColor(run.startIndex, run.endIndex, run.textStyle.foregroundColour);
}
}
这就是它在 Google Apps 脚本本身中的外观。原件是打字稿。
我使用以下代码测试该代码:
function myFunction() {
const body = DocumentApp.getActiveDocument().getBody();
const paragraphs = body.getParagraphs();
const obj = {
"runs": [{
"endIndex": 4,
"startIndex": 0,
"text": "dog ",
"textStyle": {
"fontFamily": "Arial",
"fontSize": 10,
"foregroundColour": "#1155cc",
"foregroundColor": "#1155cc",
"isBold": false,
"isItalic": false,
"isStrikethrough": false,
"isUnderline": true
},
"linkUrl": "https://en.wikipedia.org/wiki/Dog"
}, {
"endIndex": 8,
"startIndex": 4,
"text": "cat ",
"textStyle": {
"fontFamily": "Arial",
"fontSize": 10,
"foregroundColour": "#000000",
"foregroundColor": "#000000",
"isBold": true,
"isItalic": false,
"isStrikethrough": false,
"isUnderline": false
},
"linkUrl": null
}, {
"endIndex": 12,
"startIndex": 8,
"text": "cow ",
"textStyle": {
"fontFamily": "Arial",
"fontSize": 10,
"foregroundColour": "#000000",
"foregroundColor": "#000000",
"isBold": false,
"isItalic": true,
"isStrikethrough": false,
"isUnderline": false
},
"linkUrl": null
}, {
"endIndex": 16,
"startIndex": 12,
"text": "bat ",
"textStyle": {
"fontFamily": "Arial",
"fontSize": 10,
"foregroundColour": "#000000",
"foregroundColor": "#000000",
"isBold": false,
"isItalic": false,
"isStrikethrough": true,
"isUnderline": false
},
"linkUrl": null
}, {
"endIndex": 20,
"startIndex": 16,
"text": "fox ",
"textStyle": {
"fontFamily": "Arial",
"fontSize": 10,
"foregroundColour": "#fbbc04",
"foregroundColor": "#fbbc04",
"isBold": false,
"isItalic": false,
"isStrikethrough": false,
"isUnderline": false
},
"linkUrl": null
}, {
"endIndex": 24,
"startIndex": 20,
"text": "jay ",
"textStyle": {
"fontFamily": "Impact",
"fontSize": 10,
"foregroundColour": "#000000",
"foregroundColor": "#000000",
"isBold": false,
"isItalic": false,
"isStrikethrough": false,
"isUnderline": false
},
"linkUrl": null
}, {
"endIndex": 27,
"startIndex": 24,
"text": "kit",
"textStyle": {
"fontFamily": "Arial",
"fontSize": 36,
"foregroundColour": "#000000",
"foregroundColor": "#000000",
"isBold": false,
"isItalic": false,
"isStrikethrough": false,
"isUnderline": false
},
"linkUrl": null
}
]
};
setParagraphToRichText(paragraphs[0], JSON.stringify(obj))
}
按理说,我应该知道dog cat cow bat fox jay kit
哪里dog
有linkUrl、cat
粗体、cow
斜体、bat
删除线、fox
ACCENT3 颜色、jay
Impact-font 和kit
36 点。
但是,我得到的一切都是正确的,cat
除了上述样式之外。cow
bat
我哪里出错了?粗体的文档看起来很不言自明,我很确定我正在关注它。
(是的,我说英语的一种少数民族方言,它可以u
表达其他方言没有的词语。)
明天
感谢Oleg Valter指出我的错字。所以,非常尴尬的是,问题不是出在谷歌身上,而是出在我身上。
解决方案
推荐阅读
- regex - 插入符号和美元符号在 scrapy s 中不起作用
- python-3.x - 有效地创建具有空值的分类数据框
- css - 在屏幕的不同位置放置不同的 Materialise toast [版本:1.0.0]
- sql-server - HIbernate 工具 org.hibernate.exception.JDBCConnectionException:调用 Driver#connect 时出错
- javascript - 如何创建可以从 Python 调用的 JavaScript API?
- c# - MVVM 结构:在 MVVM 中将文件发送到服务器的位置在哪里?
- haskell - 双函子与箭头方法
- bash - 如果第一列匹配,则处理第二列
- javascript - 如何对 keydown 和 click 事件使用相同的功能?
- macos - 移植一个Window专用工具到macOS,这个工具可以修改其他应用程序的属性