如何在卡片中添加敬酒通知(或类似内容)?



我正在尝试修改猫的快速入门教程,在底部添加通知,以便在遇到任何错误时通知用户(例如toast通知或类似的东西)。

我:

function createCatCard(text, isHomepage) {
if (!isHomepage) {
isHomepage = false;
}
// Use the "Cat as a service" API to get the cat image. Add a "time" URL
// parameter to act as a cache buster.
var now = new Date();
// Replace formward slashes in the text, as they break the CataaS API.
var caption = text.replace(///g, ' ');
var imageUrl = Utilities.formatString('https://cataas.com/cat/says/%s?time=%s', encodeURIComponent(caption), now.getTime());
var image = CardService.newImage().setImageUrl(imageUrl).setAltText('Meow')
// Create a button that changes the cat image when pressed.
// Note: Action parameter keys and values must be strings.
var action = CardService.newAction().setFunctionName('onChangeCat').setParameters({text: text, isHomepage: isHomepage.toString()});
var button = CardService.newTextButton().setText('Change cat').setOnClickAction(action).setTextButtonStyle(CardService.TextButtonStyle.FILLED);
var buttonSet = CardService.newButtonSet().addButton(button);
// Create a footer to be shown at the bottom.
var footer = CardService.newFixedFooter().setPrimaryButton(CardService.newTextButton().setText('Powered by cataas.com').setOpenLink(CardService.newOpenLink().setUrl('https://cataas.com')));
// Assemble the widgets and return the card.
var section = CardService.newCardSection().addWidget(image).addWidget(buttonSet);
var card = CardService.newCardBuilder().addSection(section).setFixedFooter(footer);
if (!isHomepage) {
// Create the header shown when the card is minimized,
// but only when this card is a contextual card. Peek headers
// are never used by non-contexual cards like homepages.
var peekHeader = CardService.newCardHeader().setTitle('Contextual Cat').setImageUrl('https://www.gstatic.com/images/icons/material/system/1x/pets_black_48dp.png').setSubtitle(text);
card.setPeekCardHeader(peekHeader)
}
var act = CardService.newActionResponseBuilder().setNotification(CardService.newNotification().setText('bob here').setType(CardService.NotificationType.INFO));
card.addCardAction(act);

return card.build();
}

(我添加的唯一部分是倒数第二行)。然而,当我运行代码时,我得到:

Cannot find method addCardAction(CardService.ActionResponseBuilder). [line: 62, function: createCatCard, file: Common]

编辑:

我也试过card.setNotification(CardService.newNotification().setText('bob here').setType(CardService.NotificationType.INFO));,但我得到TypeError: Cannot find function setNotification in object CardBuilder. [line: 62, function: createCatCard, file: Common]

EDIT2:我也试过

card.newNotification().setText('bob here').setType(CardService.NotificationType.INFO);

但我得到TypeError: Cannot find function newNotification in object CardBuilder. [line: 63, function: createCatCard, file: Common]

card对象的类型是CardBuilder,并且根据文档,它没有addCardAction,setNotificationnewNotification方法,这就是为什么您试图做的事情不起作用。有一个Notification类,当用户与UI元素交互时使用。下面是cat快速入门代码的示例:

function onChangeCat(e) {
... // <-- omitted for brevity
var navigation = CardService.newNavigation()
.updateCard(card);
var actionResponse = CardService.newActionResponseBuilder()
.setNavigation(navigation);
const failed = someOperationThatMightFail();
if (failed) {
actionResponse
.setNotification(CardService.newNotification()
.setText("PANIC"));
}
return actionResponse.build();
}

编辑:如果您想要使用现有的卡片来指示失败,我认为必须使用实际的card对象本身来完成。Notification类只能用作与UI元素交互的响应。使用Card对象指示故障的一个优点是,在问题解决之前,用户可以获得该信息,这与Notification不同,后者最终将从UI中消失。下面是如何使用现有卡提示失败的示例:

function createCatCard(text, isHomepage) {
...
// Create a footer to be shown at the bottom.
var footer = CardService.newFixedFooter().setPrimaryButton(CardService.newTextButton().setText('Powered by cataas.com').setOpenLink(CardService.newOpenLink().setUrl('https://cataas.com')));
// Assemble the widgets and return the card.
var section = CardService.newCardSection();
const failed = someOperationThatMightFail();
if (failed) {
var textParagraph = CardService.newTextParagraph();
textParagraph.setText('Something went wrong, please try again later');
section
.addWidget(buttontextParagraphSet);
} else {
section
.addWidget(image)
.addWidget(buttonSet);
}
var card = CardService.newCardBuilder().addSection(section).setFixedFooter(footer);
if (!isHomepage) {
// Create the header shown when the card is minimized,
// but only when this card is a contextual card. Peek headers
// are never used by non-contexual cards like homepages.
var peekHeader = CardService.newCardHeader().setTitle('Contextual Cat').setImageUrl('https://www.gstatic.com/images/icons/material/system/1x/pets_black_48dp.png').setSubtitle(text);
card.setPeekCardHeader(peekHeader)
}
...
}

相关内容

  • 没有找到相关文章

最新更新