![](/img/trans.png)
[英]Include child records count in the parent record attributes list without fetching all child records
[英]How to document merge a parent record and all of its child records
我正在创建一个从Google App Maker到Google Document模板的文档合并(邮件合并)。 合并单个记录时,我可以成功完成此操作,但是如何将多个记录合并到一个文档中?
我有一个purchase_orders
父记录,其中有几个purchase_order_line_items
子记录,但是我似乎无法将所有这些记录合并到一个文档中。
Johan W提出了类似的问题( 与Google App Maker合并文档 ),Markus Malessa和Pavel Shkleinik提出了全面的答案(谢谢!)。 但是,它仅适合合并单个记录的情况。
我试图通过使用第二个for
循环来获取所有关联子记录的数据来建立他们的答案。 该脚本可以运行,但似乎只合并了第一个子记录。 不是所有的人。
这是我尝试使用的服务器端代码的示例:
function Export(key, key2) {
// Get the parent record by its key, which was passed by the first parameter above
var record = app.models.Purchase_Orders.getRecord(key);
// Get the first child record by its key, which was passed by the second parameter above
var childRecord = app.models.Purchase_Order_Line_Items.getRecord(key2);
// Get the Google Document which will be used as a template for this merge
var templateId = '1Xbt8camqHJYrhBnx0a6G2-RvTvybqU0PclHifcdiLLA';
//Set the filename of the new merge document to be created
var filename = 'Document for Customer ' + new Date();
//Make a copy of the template to use as the merge document
var copyFile = DriveApp.getFileById(templateId).makeCopy(filename);
//Get the Google Docs ID of the newly created merge document
var copyDoc = DocumentApp.openById(copyFile.getId());
var copyBody = copyDoc.getBody();
// Replace the field names in the template with the field data from the parent record
var fields = app.metadata.models.Purchase_Orders.fields;
for (var i in fields) {
console.log(i);
var text = '<<' + fields[i].name + '>>';
var data = record[fields[i].name];
if (data !== null) {
copyBody.replaceText(text, data);
} else {
// do nothing
}
}
// Replace the field names in the template with the field data from the child records
childFields = app.metadata.models.Purchase_Order_Line_Items.fields;
for (i in childFields) {
console.log(i);
var childtext = '<<' + childFields[i].name + '>>';
var childdata = childRecord[childFields[i].name];
if (childdata !== null) {
copyBody.replaceText(childtext, childdata);
} else {
// do nothing
}
}
}
如何改进代码,以便将所有关联的子记录合并到一个文档中?
如何设置我的Google文档模板以容纳任意数量的子记录?
我建议不要只通过传递父键,然后按如下所示更改函数,而不是通过第二个参数传递子记录键:
function Export(key) {
// Get the parent record by its key, which was passed by the first parameter above
var record = app.models.Purchase_Orders.getRecord(key);
// Get the first child record by its key, which was passed by the second parameter above
var childRecords = record.Purchase_Order_Line_Items;
// Get the Google Document which will be used as a template for this merge
var templateId = '1Xbt8camqHJYrhBnx0a6G2-RvTvybqU0PclHifcdiLLA';
//Set the filename of the new merge document to be created
var filename = 'Document for Customer ' + new Date();
//Make a copy of the template to use as the merge document
var copyFile = DriveApp.getFileById(templateId).makeCopy(filename);
//Get the Google Docs ID of the newly created merge document
var copyDoc = DocumentApp.openById(copyFile.getId());
var copyBody = copyDoc.getBody();
// Replace the field names in the template with the field data from the parent record
var fields = app.metadata.models.Purchase_Orders.fields;
for (var i in fields) {
console.log(i);
var text = '<<' + fields[i].name + '>>';
var data = record[fields[i].name];
if (data !== null) {
copyBody.replaceText(text, data);
} else {
// do nothing
}
}
// Replace the field names in the template with the field data from the child records
var childFields = app.metadata.models.Purchase_Order_Line_Items.fields;
var table = [];
var tableheader = [];
for (i in childFields) {
console.log(i);
tableheader.push(childFields[i].displayName);
}
table.push(tableheader);
for (i in childRecords) {
var data = [];
for (var j in childFields) {
data.push(childRecords[i][childFields[j].name]);
}
table.push(data);
}
copyBody.appendTable(table);
表格的构建基于2D数组,文档位于https://developers.google.com/apps-script/reference/document/table 。 但是,您还需要删除预建表,而只附加一个表。 这样,您就不必像当前在文档模板中那样固定子记录的数量。 另外,childRecords的变量可能会也可能不会工作,因为我不确定预取是否可以与.getRecord(key)
结合使用,所以我尚未对其进行测试。 这可能需要一些其他测试,但希望它将提供足够的指导。
以为我会添加它作为替代。 假设您保留表格,但删除所有行(标题行除外),则仍然可以使用DocumentApp服务将行添加到表格中,如下所示:
var tableheaderfieldnames = ['Quantity_for_PO', 'Inventory_Item.id', 'Unit_Price']; //set a fixed table header with the field names, uncertain if the table header for the related inventory item will work or not
var table = copyBody.getTables()[0];
for (i in childRecords) {
var row = table.appendRow();
for (var j in tableheaderfieldnames) {
row.appendTableCell(childRecords[i][tableheaderfieldnames[j]]);
}
}
请记住,AM不允许您使用FK引用,因此对于似乎使用fk字段的库存物料,您可能需要在尝试填写自己的物料时修改适当的名称引用。表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.