I’m currently making a Gmail Google Add-on but I’m confused about using card navigation in Apps Script. What I intend is to have Card A, Card B, and Card C in sequence, and when a button is pressed on Card C, Card C is poped and previous Card B should be updated to a Card B’
[Card A, Card B, Card C] -> [Card A, Card B`]
Below is the example code which works same as mine
function buildCardA() {
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("Card A"))
.addSection(
CardService.newCardSection()
.addWidget(
CardService.newTextButton()
.setText('Go to Card B')
.setOnClickAction(CardService.newAction()
.setFunctionName('goToCardB'))
)
)
.build();
return card;
}
function buildCardB() {
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("Card B"))
.addSection(
CardService.newCardSection()
.addWidget(
CardService.newTextButton()
.setText('Go to Card C')
.setOnClickAction(CardService.newAction()
.setFunctionName('goToCardC'))
)
)
.build();
return card;
}
function buildCardC() {
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("Card C"))
.addSection(
CardService.newCardSection()
.addWidget(
CardService.newTextButton()
.setText('Pop and Update Card B')
.setOnClickAction(CardService.newAction()
.setFunctionName('handleCardCButtonClick'))
)
)
.build();
return card;
}
function goToCardB() {
var cardB = buildCardB();
return CardService.newNavigation().pushCard(cardB);
}
function goToCardC() {
var cardC = buildCardC();
return CardService.newNavigation().pushCard(cardC);
}
function handleCardCButtonClick(e) {
var updatedCardB = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("Updated Card B"))
.addSection(
CardService.newCardSection()
.addWidget(
CardService.newTextParagraph().setText("This is the updated content of Card B.")
)
)
.build();
return CardService.newNavigation() //Modified Part
.popCard()
.updateCard(updatedCardB)
}
function onHomepage(e) {
var cardA = buildCardA();
return CardService.newActionResponseBuilder()
.setNavigation(CardService.newNavigation().pushCard(cardA))
.build();
}
However, navigation doesn’t work for what I intended. For changing the modified part of the above code,
return CardService.newNavigation()
.popCard()
.updateCard(updatedCardB)
and
return CardService.newNavigation()
.popToRoot()
.updateCard(updatedCardB)
results [Card A, Card B, Card B`],
return CardService.newNavigation()
.popCard()
.popCard()
.pushCard(updatedCardB)
and
return CardService.newNavigation()
.popToRoot()
.pushCard(updatedCardB)
results [Card A, Card B, Card C, Card B`]
I’ve tried other attempts, but they didn’t work as what I intended. How can I make the code work resulting [Card A, Card B`]?
Sa2Da4728 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.