【问题标题】:Document Merge with Google App Maker使用 Google 应用制作工具合并文档
【发布时间】:2018-08-15 05:00:27
【问题描述】:

我想将文档合并集成到我在 Google 应用制作工具中创建的工作流程中。每当在表单中提交输入时,如果模板包含与字段名称匹配的占位符,则应将答案合并到 Google 文档模板中。例如,如果字段名称为 last_name,则占位符将为 {{last_name}}。

文档合并应该遍历每个项目的每个字段,这样我就不必将字段名称编程到脚本中。在 Google 电子表格中,这将通过使用类似的循环来解决

function documentMerge() {
  var ss = SpreadsheetApp.getActiveSpreadsheet.getActiveSheet;
  var lastClmn = ss.getDataRange.getLastColumn();
  var lastRow = ss.getDataRange.getLastRow();

  for (var i=0, lastClmn, i++) {
    for (var j=1, lastRow, j++) {  
      // if '{{' + (ss.getRange(0, i).getValue()) + '}}' is found in document
      // ... replace placeholder in document with contents from column i, row j ...
  }

在 Google 应用制作工具中是否有类似的可能?

【问题讨论】:

    标签: google-docs google-app-maker


    【解决方案1】:

    将以下内容放入模型的 onAfterCreate 事件中:

    var templateId = 'your template ID';
    var filename = 'Document for Customer ' + record.ClientName + new Date();
    
    var copyFile = DriveApp.getFileById(templateId).makeCopy(filename);
    
    var copyDoc = DocumentApp.openById(copyFile.getId());
    var copyBody = copyDoc.getBody();
    var fields = app.metadata.models.Clients.fields;
    
    for (var i in fields) {
      var text = '<<' + fields[i].name + '>>';
      var data = record[fields[i].name];
      copyBody.replaceText(text, data);
    }
    
    copyDoc.saveAndClose();
    

    这应该为你做。有关模板和创建的文档,请参见图片。

    【讨论】:

    • 不幸的是,当我尝试将字段合并到文档中时出现异常(“未找到具有此 ID 的文档或您无权访问该文档”)。我已经创建了一个访问链接,因此拥有该链接的任何人都可以访问该文档,但结果保持不变。我做错了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多