Moh*_*nna 5 google-apps-script gmail-addons
我在使用 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;
}
}
Run Code Online (Sandbox Code Playgroud)
该函数在 上调用onTriggerFunction。现在,如果我移到另一张卡上,我想再次回到根目录,但以干净清晰的方式,我使用它,但它不起作用:
//Move the user to the root card again
var refreshNav = CardService.newNavigation().popToRoot();
return CardService.newActionResponseBuilder().setStateChanged(true).setNavigation(refreshNav).build();
Run Code Online (Sandbox Code Playgroud)
简单地说,我想要的是一旦用户点击Refresh按钮,卡片就会刷新/更新自己以再次调用并获取新数据。
我发现做到这一点的唯一方法是始终使用单张卡作为根。在主函数(在 中命名appscript.json onTriggerFunction)中,仅返回一张卡片,而不是卡片数组。然后你就可以使用popToRoot().updateCard(...)并且它可以工作。
我为此苦苦挣扎了一天多,改进了格伦·利特尔的答案,使其更加清晰。
我在名为:的函数中定义了要刷新的根卡onHomepage。
我更新appscript.json清单以设置homepageTrigger并onTriggerFunction返回构建我的根卡的函数。
"gmail": {
"homepageTrigger": {
"enabled": true,
"runFunction":"onHomepage"
},
"contextualTriggers":[
{
"unconditional":{},
"onTriggerFunction": "onHomepage"
}
]
}Run Code Online (Sandbox Code Playgroud)
然后就像构建一个gotoRoot始终刷新根页面的导航按钮功能一样简单。
function gotoRootCard() {
var nav = CardService.newNavigation()
.popToRoot()
.updateCard(onHomepage());
return CardService.newActionResponseBuilder()
.setNavigation(nav)
.build();
}Run Code Online (Sandbox Code Playgroud)