文档与 Google 应用程序制作工具合并

Document Merge with Google App Maker

我想将文档合并集成到我在 Google App Maker 中创建的工作流程中。每当以表单形式提交输入时,如果模板包含与字段名称匹配的占位符,则应将答案合并到 Google Docs 模板中。例如,如果字段名称是 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 App Maker 中是否有类似的东西?

将以下内容放入模型的 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();

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