6

我正在使用 Apps 脚本做很多场景的噩梦,但没有任何效果!我有一个GET请求返回一组卡片的函数。现在,有时我需要再次刷新此卡以获取新内容。

function listTemplatesCards(){
  var getAllTemplates = getTemplates();
  var allTemplates = getAllTemplates.templates;
  var theUserSlug = getAllTemplates.user_slug;
  var templateCards = [];

  //There are templates
  if(allTemplates.length > 0){
    allTemplates.forEach(function(template){
      templateCards.push(templateCard(template, theUserSlug).build());
    });
    return templateCards;
  }
}

该函数在 上调用onTriggerFunction。现在,如果我移动到另一张卡并且我想再次回到根目录但以干净清晰的方式,我使用它但它不起作用:

  //Move the user to the root card again
  var refreshNav = CardService.newNavigation().popToRoot();
  return CardService.newActionResponseBuilder().setStateChanged(true).setNavigation(refreshNav).build();

简单地说,我想要的是一旦用户单击Refresh按钮,卡就会刷新/更新自身以再次拨打电话并获取新数据。

4

4 回答 4

5

我发现这样做的唯一方法是始终使用一张卡作为根。在 main 函数中(在 中命名appscript.json onTriggerFunction),只返回一张卡片,而不是卡片数组。然后你就可以使用popToRoot().updateCard(...)它了。

于 2018-05-12T21:03:02.930 回答
2

我为此苦苦挣扎了一天多,改进了 Glen Little 的答案,使其更加清晰。

我在一个名为的函数中定义了要刷新的根卡:onHomepage.

我更新appscript.json清单以设置homepageTriggeronTriggerFunction返回构建我的根卡的函数。

"gmail": {
      "homepageTrigger": {
          "enabled": true,
          "runFunction":"onHomepage"
        },
      "contextualTriggers":[
        {
          "unconditional":{},
          "onTriggerFunction": "onHomepage"
        }
      ]  
      }

然后就像构建一个gotoRoot始终刷新根页面的导航按钮功能一样简单。

  function gotoRootCard() {
    var nav = CardService.newNavigation()
    .popToRoot()
    .updateCard(onHomepage());
    
    return CardService.newActionResponseBuilder()
        .setNavigation(nav)
        .build();        
  }

于 2021-09-01T14:07:22.727 回答
2

就 gmail 插件而言,卡片不会刷新,而是会用新卡片进行更新。这很简单。

//lets assume you need a form to be updated
function updateProfile() {
    //ajax calls
    //...

    //recreate the card again.
    var card = CardService.newCardBuilder();
    //fill it with widgets
    //....

    //replace the current outdated card with the newly created card.
    return CardService.newNavigation().updateCard(card.build());
}

于 2018-06-06T06:38:54.767 回答
1

一个适用于我的 Gmail 插件的坏技巧:

return CardService.newActionResponseBuilder()
  .setStateChanged(true) // this doesn't seem to do much.  Wish it would reload the add-on
  .setNotification(CardService.newNotification()
                   .setText('Created calendar event')
  )
  // HACK!  Open a URL which closes itself in order to activate the RELOAD_ADD_ON side-effect
  .setOpenLink(CardService.newOpenLink() 
               .setUrl("https://some_site.com/close_yoself.html")
               .setOnClose(CardService.OnClose.RELOAD_ADD_ON))
  .build();

close_yoself.html 的内容只是:

<!DOCTYPE html>
<html><body onload="self.close()"></body></html>

因此,看起来 Google 已经为使用 OpenLink 的 ActionResponse 考虑并解决了这个问题,但对于使用 Navigation 或 Notification 的 ActionResponse 却没有。上面的 hack 绝对不是很好,因为它会短暂地打开和关闭浏览器窗口,但至少它会刷新加载项,而无需用户手动执行此操作。

于 2020-02-04T18:02:51.100 回答