【问题标题】:Issues DocumentMerge in Google AppMakerGoogle App Maker 中的文档合并问题
【发布时间】:2018-04-21 04:26:25
【问题描述】:

因为我想通过将列表中的条目合并到 Google 文档模板中来创建文档。因此,我将之前 question 中的 DocumentMerge 方法集成到列表小部件中的 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();
}

【问题讨论】:

  • 据我推断,您正在将小部件对象传递给服务器脚本,这就是您获得该循环引用的原因。理想情况下,您应该有一个客户端脚本来获取项目密钥,然后使用 google.script.run 调用服务器脚本,只传递项目密钥而不是小部件对象。
  • 不幸的是,我不熟悉将项目密钥移交给服务器脚本。那会是什么样子?

标签: google-app-maker


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多