首页 > 解决方案 > 模板文本文档

问题描述

我正在尝试创建用于创建合同的脚本。这将位于“模板文件”中,该模板文件将成为 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 - 合同模板”的文件实例的函数

标签: google-apps-scriptgoogle-docs

解决方案


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:


推荐阅读