【问题标题】:How to document merge a parent record and all of its child records如何记录合并父记录及其所有子记录
【发布时间】:2019-08-22 22:29:23
【问题描述】:

我正在创建从 Google 应用制作工具到 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 文档模板以适应任意数量的子记录?

【问题讨论】:

    标签: javascript sql google-apps-script merge google-app-maker


    【解决方案1】:

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

    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 字段,您可能需要修改设置正确的名称引用,以便在尝试填写表中的项目。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 2012-03-08
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多