【问题标题】:Cannot return a card markup from the callback function of a universal action无法从通用操作的回调函数返回卡片标记
【发布时间】:2019-11-13 23:52:02
【问题描述】:

我只是想开始使用 Google 在此处描述的用于扩展撰写 UI 的示例插件: https://developers.google.com/gsuite/add-ons/gmail/extending-compose-ui

但是当我运行它时,我得到了这个错误:

插件出错。运行时错误。无法返回卡片标记 来自通用动作的回调函数。

我没有在清单文件中设置任何通用操作:

{
  "timeZone": "America/New_York",
  "oauthScopes":[
    "https://www.googleapis.com/auth/script.send_mail",
    "https://www.googleapis.com/auth/gmail.readonly",
    "https://www.googleapis.com/auth/spreadsheets",
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/gmail.addons.current.message.metadata",
    "https://www.googleapis.com/auth/gmail.modify",
    "https://www.googleapis.com/auth/gmail.addons.current.action.compose"
    ],
  "gmail":{
    "name": "My Mail Merge",
    "logoUrl": "https://www.gstatic.com/images/icons/material/system/1x/label_googblue_24dp.png",
    "composeTrigger": {
      "draftAccess": "METADATA",
      "selectActions": [
        {
          "text": "show UI",
          "runFunction": "buildImageComposeCard"
        }
      ]
    },
    "openLinkUrlPrefixes": [
      "https://mail.google.com/"
      ],
    "primaryColor": "#42585F4",
    "secondaryColor": "#42585F4"
  },
  "exceptionLogging": "STACKDRIVER"
}

下面是 Code.gs:

function getInsertImageComposeUI(e) {
  return [buildImageComposeCard()];
}

function buildImageComposeCard() {
  // Get a list of image URLs to display in the UI.
  // This function is not shown in this example.
  var imageUrls  = [
    "https://mail.google.com/1",
    "https://mail.google.com/2",
    "https://mail.google.com/3"
    ];

  var card = CardService.newCardBuilder();
  var cardSection = CardService.newCardSection().setHeader('My Images');
  for (var i = 0; i < imageUrls.length; i++) {
    var imageUrl = imageUrls[i];
    cardSection.addWidget(
      CardService.newImage()
      .setImageUrl(imageUrl)
      .setOnClickAction(CardService.newAction()
                        .setFunctionName('applyInsertImageAction')
                        .setParameters({'url' : imageUrl})));
  }
  return card.addSection(cardSection).build();
}

function applyInsertImageAction(e) {
  var imageUrl = e.parameters.url;
  var imageHtmlContent = '<img style=\"display: block\" src=\"'
  + imageUrl + '\"/>';
  var response = CardService.newUpdateDraftActionResponseBuilder()
  .setUpdateDraftBodyAction(CardService.newUpdateDraftBodyAction()
                            .addUpdateContent(
                              imageHtmlContent,
                              CardService.ContentType.HTML)
                            .setUpdateType(
                              CardService.UpdateDraftBodyType.IN_PLACE_INSERT))
  .build();
  return response;
}

【问题讨论】:

  • 我也无法正常工作。如果我是你,我会转向别的事情。也许这里有人会有更好的答案,但这个例子让人想起十年前的日子,当时智商一般的人几乎不可能考虑有机会弄清楚一个例子实际上是如何工作的。
  • 哈哈。好吧,我会发现一个 Gmail 插件对我的雇主非常有用,所以我有动力找到一种工作方式。将直接与 Google 联系,看看他们是否可以提供帮助。 Stackoverflow 是他们对应用程序脚本内容的第一条支持,但不是最后一条。
  • 我对这里的其他志愿者了解不多,所以一切都还没有丢失。我写过 Gmail 插件,但没有写作曲家插件。
  • 我也能够让侧边栏的东西工作,只是不能让撰写的东西。
  • 谷歌有什么更新吗?还是开发插件?

标签: google-apps-script gmail-addons


【解决方案1】:

在清单文件中,runFunction 必须设置为 "getInsertImageComposeUI"

另外,在代码文件中,CardService.ContentType.HTML 必须是其中之一

CardService.ContentType.MUTABLE_HTML

CardService.ContentType.IMMUTABLE_HTML

【讨论】:

  • 我也有同样的问题。已经将 runFunction 和 ContentType 设置为您建议的,但它不起作用。还有其他建议吗?
【解决方案2】:

我有相同的错误消息,但无法让该示例正常工作。但是,我创建了一个更简单的 Hello-World 示例,它确实有效。我认为它对新手来说更有用。不要忘记将清单中的runFunction 设置为getComposeCard

function getComposeCard(e) {   return [buildComposeCard()]; }

function buildComposeCard() {
    var card = CardService.newCardBuilder();   
    var cardSection = CardService.newCardSection().setHeader('Test Header');   
    testVar = ['item1', 'item2'];   
    for (var i = 0; i < testVar.length; i++) {
        var item = testVar[i];
        cardSection.addWidget(
          CardService.newTextParagraph().setText(item));   
   }   
   return card.addSection(cardSection).build(); 

}

【讨论】:

    【解决方案3】:

    可能为时已晚,但只需将buildImageComposeCard 的结果包装为数组即可解决问题

    return [card.addSection(cardSection).build()]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-08
      • 2019-10-23
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 2016-01-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多