如何让GoogleApps脚本告诉GoogleDocs从JSON表示中使用粗体、斜体、删除线和下划线?
我已经编写了一个函数,如何让GoogleApps脚本告诉GoogleDocs从JSON表示中使用粗体、斜体、删除线和下划线?,json,google-apps-script,google-docs-api,Json,Google Apps Script,Google Docs Api,我已经编写了一个函数,setParagraphToRichText,它接受一个GoogleAppsScript.Document.Parague和一个字符串。字符串是一个字符串化的JSON blob,它定义给定段落的文本和属性 /** * * * @param {GoogleAppsScript.Document.Paragraph} paragraph * @param {string} jsonString */ function setParagraphToRichText(pa
setParagraphToRichText
,它接受一个GoogleAppsScript.Document.Parague和一个字符串。字符串是一个字符串化的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);
}
}
按理说,我应该得到dog cat cow bat fox jay kit
其中dog
有链接URL,cat
是粗体,cow
是斜体,bat
是删除线,fox
是重音3颜色,jay
是冲击字体,kit
是36点
然而,我得到的是一切正常,除了cat
、cow
和bat
,它们不属于上述样式
我哪里出错了?的文档似乎是不言自明的,我相当确定我在遵循它
(是的,我说的是一种少数民族的英语方言,它把u
放进了其他方言没有的单词中。)
第二天
谢谢你指出我的打字错误。所以,非常尴尬的是,问题不在谷歌,而在我身上。应该是
run.textStyle.isBold
,run.textStyle.isItalic
吗?顺便说一句,你能澄清一下为什么要字符串化然后立即解析JSON对象吗?这是为了对你打算在Web应用程序中使用的函数或与PropertiesService
一起使用的函数进行单元测试吗?谢谢@OlegValter指出这一点。唉。至于stringify和parse,这只是为了测试。发生:)如果您使用在线编辑器-切换到本地IDE,我认为这在良好的自动完成下是不可能的。Re:JSON-ok,我只是好奇为什么会有这么奇特的设置
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))
}