Javascript Google应用程序脚本中Google文档中字符串的文本格式

Javascript Google应用程序脚本中Google文档中字符串的文本格式,javascript,google-apps-script,google-docs,Javascript,Google Apps Script,Google Docs,我有一些Google应用程序脚本,它们使用DocumentApp.replaceText方法在Google文档中查找文本字符串,并将其替换为其他文本字符串 var file = DocsList.getFileById('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); var newTextString = 'foo\nbar'; file.replaceText('Old text string',newTextString); \n可用于换行,但是否有方

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

var file = DocsList.getFileById('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
var newTextString = 'foo\nbar';
file.replaceText('Old text string',newTextString);
\n可用于换行,但是否有方法使用粗体/斜体/颜色等设置文本格式


我尝试将HTML标记和内联CSS样式与span标记一起使用,但没有成功,它只输出包含标记的文本。我记得谷歌文档曾经有一个HTML按钮,你可以看到组成谷歌文档内容的标记,但是他们去掉了

使用API站点上描述的函数

可以根据需要对所有样式的文本设置进行任意操作。 例如:

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不在文档对象上,而是段落、正文、标题节等。使用DocumentApp按id更改了打开文档的解决方案