【问题标题】:Text formatting for strings in Google Documents from Google Apps Script来自 Google Apps 脚本的 Google 文档中字符串的文本格式
【发布时间】:2013-08-22 19:32:15
【问题描述】:

我有一些 Google Apps 脚本使用 DocumentApp .replaceText 方法在 Google 文档中查找文本字符串并将其替换为不同的文本字符串。

var file = DocsList.getFileById('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
var newTextString = 'foo\nbar';
file.replaceText('Old text string',newTextString);

\n 可以换行,但是有没有办法用粗体/斜体/颜色/等来格式化文本?

我尝试将 HTML 标签和内联 CSS 样式与 span 标签一起使用,但这不起作用,它只是输出包含标签的文本。我记得 Google Docs 曾经有一个 HTML 按钮,你可以看到构成 Google doc 内容的标记,但他们把它去掉了。

【问题讨论】:

    标签: javascript google-apps-script google-docs


    【解决方案1】:

    使用 API 站点 here 中描述的函数

    您可以根据需要操作设置所有样式的文本。 例如:

    var file = DocsList.getFileById('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    file.replaceText('Old text string',newTextString);
    var newTextString = 'foo\nbar';
    
    var doc = DocumentApp.openById('DOCUMENT_ID_GOES_HERE');
    var text = doc.editAsText();
    text.setItalic(startOffset, endOffsetInclusive, italic)
    

    【讨论】:

    • 我认为您的意思是 DocumentApp.openById()。另外,replaceText 不在 Document 对象上,而是在 Paragraph、Body、HeaderSection 等。
    • 使用 DocumentApp 更改了通过 id 打开文档的解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    相关资源
    最近更新 更多