代替:
doc.appendparagraph();
如何在文档中添加一行文本而不返回下一行?
我正在尝试用 .setAttributes(bold);
我用....定义的粗体
var boldpl = {};
boldpl[DocumentApp.Attribute.BOLD] = true;
。
谢谢。
我已经想出了如何使用合并功能以迂回的方式执行此操作,但是如何添加新行而不是新段落?没有 doc.appendText();有什么功能吗?
还有一种简单的方法可以使我的文档成为单行距吗?
最佳答案
你在那里问了很多不同的问题!
Given an existing paragraph, how can some portion of it be rendered in bold while the rest remains normal?
一个 Document包含 Body ,其中可以包含 Paragraph s,每个都可以包含其他元素,包括Text。您可以将该文本作为字符串(您不能用它做很多事情)或作为 Text Object 来获取。 .这是使用 Text.setBold()
的示例将段落中的某些文本更改为粗体的方法:
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var firstParagraph = body.getParagraphs()[0];
textElement = firstParagraph.editAsText();
textElement.setBold(0,6,true); // Set the 0th to 6th characters bold
或者,您可以将 Text.setAttributes()
方法与您的自定义属性 boldpl
一起使用:
textElement.setAttributes(0,6, boldpl)
从该构建 block 开始,您可以执行以下操作:
- 从段落的开头到第一次出现冒号 (
:
) 应用粗体。 - 将粗体应用于前几个单词。
...甚至在前半部分加粗!
textElement.setBold(0, Math.floor((textElement.getText().length)/2), true);
There is no doc.appendText(); feature is there?
没有...但是有一个 Paragraph.appendText()
方法。
Also is there an easy way to make my document single spaced?
您可以通过设置各个段落的属性来控制段落中文本的行距。见 Paragraph.setLineSpacing()
.这是一个将文档中的每个段落设置为单行距的函数:
function singleSpace() {
var doc = DocumentApp.getActiveDocument();
var bodyElement = DocumentApp.getActiveDocument().getBody();
var paragraphs = bodyElement.getParagraphs();
// Set each paragraph to single-spaced
paragraphs.forEach( function( paragraph ) {
paragraph.setLineSpacing( 1.0 );
});
}
关于google-apps-script - 使用谷歌脚本创建文档时如何分割文本行(前半部分粗体,后半部分非粗体),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17255893/