google-apps-script - 将格式化文本添加到 Google Doc
问题描述
我有一个带有表单回复的 Google 表格。我创建了一些代码,以便每次完成表单时,结果都会填充 Google Doc。这工作正常。
但是,我也想将文本添加到我的 Google Doc 中,我使用以下方法完成:
function myFunction(e) {
var doc = DocumentApp.create('File');
var text = 'insert text here';
body.appendParagraph(text);
doc.saveAndClose();
}
但是,此文本仅作为纯文本添加,我想格式化此文本。具体来说,我想添加文本,使其在文档正文中以粗体、下划线和居中对齐。
我该怎么做呢?
经过一些互联网搜索和 SO 搜索后,我尝试添加 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。
推荐阅读
- ip - 以编程方式确定阿里云 (ALIYUN) 中 ECS 服务器的公网 IP
- html - 侧边栏和内容的引导对齐问题
- javascript - Create-react-app javascripts调用彼此的功能不起作用
- applescript - AppleScript 正在生成 json 而不是字符串
- ruby-on-rails - 如何获取 Active Storage 图片的 url
- ios - 执行时“else”语句未在“didSelectRowAt”函数中运行
- php - 值未使用 php mysql 和 ajax 提交到数据库表
- c# - 在 C# 中存储大字符串(html/xml/js 等)
- java - java.lang.OutOfMemoryError 开发带有 4 张图片的应用程序
- java - 如何在没有 root 的情况下在 Android 上可靠地模拟触摸事件(如 Automate 和 Tasker)?