google-apps-script - 模板文本文档
问题描述
我正在尝试创建用于创建合同的脚本。这将位于“模板文件”中,该模板文件将成为 gDrive 上的文本文件”示例:
Client's VAT: $VAT
Client's Name: $Clients_name
如何获取此文件内容(getBody(),它仅适用于文档类型,但您无法访问 Document 实例,因为我发现的所有可能性都将我引导到 File 实例..)
我的目标是获取“模板文件”的精确副本并通过他并更改以美元开头的单词,并通过 HTTP 请求中的值更改它们。(这部分已经完成。)
编辑:
你可能对我的尝试感兴趣(或者它可以帮助某人)
function createContract(obj)
{
const templateName = "RC - Contract Template";
const templateFile = getTemplate(templateName);
const templateText = templateFile.getAs("plain/text");
const file = DocumentApp.create(obj.filename);
const body = file.getBody();
const url = file.getUrl();
body.insertParagraph(0, templateText);
HtmlService.createHtmlOutput(url);
}
注意: getTemplate() 是遍历所有文件并返回名称为“RC - 合同模板”的文件实例的函数
解决方案
After some searching I've found out, that google has a big difference between "File", "Document", etc etc.
If someone gets in same situation, this is my way to get Content of file, when you know only name:
function getTemplateId(filename) {
const files = DriveApp.getFilesByType(MimeType.GOOGLE_DOCS);
while (files.hasNext()) {
var file = files.next();
if (file.getName() == filename) return file.getId();
}
return HtmlService.createHtmlOutput("Required template: " + filename + " does not exists!");
}
Note:Moved Solution posted by OP from question:
推荐阅读
- android - 是否有一些 Kotlin 文件的反编译器可以反编译成 kotlin *.kt 文件
- javascript - 在 Puppeteer 中连接浏览器
- mysql - 如何在需要重复的地方加入?
- ios - 关键观察者不会停止更新新值?
- video-capture - 将网络视频流注册为虚拟摄像机
- android - RecyclerView隐藏在android中的动态视图下
- javascript - 未触发可观察到的 next() 回调
- javascript - 传播 Redux 操作在 mapDispatchToProps 中不起作用
- sql - 避免可空关系中的重复记录
- angular5 - 如何将角度组件导出为 PDF?