Document Merge with Google App Maker

孤者浪人 提交于 2019-11-28 02:19:10

问题


I would like to integrate a document merge into a workflow which I created in Google App Maker. Whenever an input is submitted in a form, the answers should be merged into a Google Docs template, if the template contains a placeholder that matches the name of the field. For example, if the field name is last_name, the placeholder would be {{last_name}}.

The document merge should go through each field of every item, so that I don't have to program the field names into the script. In a Google Spreadsheet this would be resolved by using a loop like

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 ...
  }

Is there something similar possible in Google App Maker?


回答1:


Put the following in your onAfterCreate event of your model:

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();

That should do it for you. See the pictures as to the template and created document.



来源:https://stackoverflow.com/questions/49146043/document-merge-with-google-app-maker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!