在 Google AppMaker 中发布 DocumentMerge

Issues DocumentMerge in Google AppMaker

因为我想通过将列表中的条目合并到 Google 文档模板来创建文档。因此,我将之前 中的 DocumentMerge 方法集成到列表小部件中的 printButton 中。

单击 printButton 应该会生成一个将当前行的内容合并到文档模板中的文档。但是当我点击 printButton 时,该方法由于循环引用而失败。我该如何解决?打印方法是这样的...

 function printReview(widget) { 
  var review = app.models.Review.getRecord(widget.datasource.item._key);

  var templateId = 'templateId';
  var filename = 'Review for ...' + new Date();

  var copyFile = DriveApp.getFileById(templateId).makeCopy(filename);

  var copyDoc = DocumentApp.openById(copyFile.getId());
  var copyBody = copyDoc.getBody();
  var fields = app.metadata.models.Review.fields;

  for (var i in fields) {
    var text = '$$' + fields[i].name + '$$';
    var data = review[fields[i].name];
    copyBody.replaceText(text, data);
  }

  copyDoc.saveAndClose();
}

正如 Morfinismo 注意到的那样,您收到错误是因为您试图将复杂对象从客户端传递到服务器,而序列化程序无法处理它。为了解决这个问题,您需要调整代码:

// onClick button's event handler (client script)
function onPrintClick(button) {
  var reviewKey = button.datasource.item._key;

  google.script.run
    .withSuccessHandler(function() { /* TODO */ })
    .withFailureHandler(function() { /* TODO */ })
    .printReview(reviewKey);
}

// server script
function printReview(reviewKey) {
  var review = app.models.Review.getRecord(reviewKey);
  ...
}