首页 > 解决方案 > 如何让 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删除线、foxACCENT3 颜色、jayImpact-font 和kit36 点。

但是,我得到的一切都是正确的,cat除了上述样式之外。cowbat

我哪里出错了?粗体的文档看起来很不言自明,我很确定我正在关注它。

(是的,我说英语的一种少数民族方言,它可以u表达其他方言没有的词语。)

明天

感谢Oleg Valter指出我的错字。所以,非常尴尬的是,问题不是出在谷歌身上,而是出在我身上。

标签: jsongoogle-apps-scriptgoogle-docs-api

解决方案


推荐阅读