gpt4 book ai didi

javascript - 如何记录合并父记录及其所有子记录

转载 作者:行者123 更新时间:2023-11-30 13:54:18 24 4
gpt4 key购买 nike

我正在创建从 Google App Maker 到 Google 文档模板的文档合并(邮件合并)。我在合并一条记录时可以成功,但是如何将多条记录合并到一个文档中呢?

我有一个 purchase_orders 父记录,它有几个 purchase_order_line_items 子记录,但我似乎无法将所有这些记录合并到一个文档中。

Johan W 提出了一个类似的问题 (Document Merge with 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 文档模板以满足任意数量的子记录?

Screenshot of template

最佳答案

与其通过第二个参数传递子记录键,我建议只传递父键,然后按如下方式更改您的函数:

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

建表基于二维数组,文档在这里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 字段的库存项目,您可能需要修改设置正确的名称引用,以便在您尝试填写表中的项目。

关于javascript - 如何记录合并父记录及其所有子记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57609094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com