与Google App Maker进行文档合并 [英] Document Merge with Google App Maker
问题描述
我想将文档合并功能集成到我在Google App Maker中创建的工作流程中.只要以表单的形式提交输入,则答案应合并到Google文档模板中(如果模板包含与字段名称匹配的占位符).例如,如果字段名称为last_name,则占位符为{{last_name}}.
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}}.
文档合并应该遍历每个项目的每个字段,因此我不必将字段名称编程到脚本中.在Google Spreadsheet中,可以使用类似
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 ...
}
Google App Maker中是否可能有类似的东西?
Is there something similar possible in Google App Maker?
推荐答案
在模型的onAfterCreate事件中添加以下内容:
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.
这篇关于与Google App Maker进行文档合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!