Google Apps脚本中Google文档中字符串的文字格式 [英] Text formatting for strings in Google Documents from Google Apps Script

查看:129
本文介绍了Google Apps脚本中Google文档中字符串的文字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some Google Apps scripts that use the DocumentApp .replaceText method to find a text string in a Google Document and replace it with a different text string.

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

\ n用于换行,但有没有办法用粗体格式化文本/ italics / colors / etc?

The \n works to make line breaks, but is there a way to format the text with bold/italics/colors/etc?

我尝试使用带有span标签的HTML标签和内联CSS样式,但这不起作用,它只输出包括标签。我记得Google Docs过去常常有一个HTML按钮,你可以看到组成Google文档内容的标记,但是他们把它拿出来了。

I tried to use HTML tags and inline CSS styles with the span tag, but that didn't work, it just outputs the text including the tags. I remember how Google Docs used to have an HTML button and you could see the markup that made up the content of the Google doc, but they took that out.

推荐答案

使用API​​网站上描述的功能 here

Use the functions describe at the API site here

您可以操作文本设置各种风格,你想要的。
例如:

You can manipulate the text setting all styles as much as you want. For example:

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)

这篇关于Google Apps脚本中Google文档中字符串的文字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆