向Google文档添加格式化文本 [英] Adding formatted text to a Google Doc
本文介绍了向Google文档添加格式化文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有表单响应的Google工作表。我创建了一些代码,以便每次完成表单时,结果都会填充到Google Doc中。该功能工作正常。
不过,我还想将文本添加到我的Google Doc中,这是通过以下方式完成的:
function myFunction(e) {
var doc = DocumentApp.create('File');
var text = 'insert text here';
body.appendParagraph(text);
doc.saveAndClose();
}
此文本仅作为纯文本添加,但我希望设置此文本的格式。具体地说,我希望添加文本,使其在文档正文中使用粗体、下划线和居中对齐。
如何执行此操作?
经过一些互联网搜索和搜索,我尝试添加html(例如,<b> </b>
),然后尝试text.setBold(true)
。这些方法不起作用。
我承认,我对Google脚本编辑器中的编码知之甚少,所以我完全不确定如何进行这项工作。我很幸运,我得到了所有的表单响应来填充一个命名的Google Doc文件!
推荐答案
以下是我最近创建的文档片段:
var nameStyle={};
nameStyle[DocumentApp.Attribute.FONT_SIZE]=8;
nameStyle[DocumentApp.Attribute.BOLD]=true;
nameStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#000000';
var valStyle={};
valStyle[DocumentApp.Attribute.FONT_SIZE]=12;
valStyle[DocumentApp.Attribute.BOLD]=false;
valStyle[DocumentApp.Attribute.FOREGROUND_COLOR]='#cc0000';
body.appendParagraph('Basic Project Data').setAttributes(hdg1Style);
var p1=body.appendParagraph(Utilities.formatString('%s: ','PID')).setAttributes(nameStyle);
var p2=body.appendParagraph(Utilities.formatString('%s',selObj.pid)).setAttributes(valStyle);
p2.merge();
for(var i=0;i<basicDataA.length;i++){
var par1=body.appendParagraph(Utilities.formatString('%s: ',basicDataA[i][0])).setAttributes(nameStyle);
var par2=body.appendParagraph(Utilities.formatString('%s',basicDataA[i][1])).setAttributes(valStyle);
par2.merge();
}
请注意,appendParagraph先是setAttributes。
这篇关于向Google文档添加格式化文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文