How can I add new sections to an existing card on Gmail Addon?

1.8k Views Asked by At

I'm actually trying to create a Chat interface in Gmail using Add-ons with App Script. All the interactions are working. But it keeps navigating to different cards rather than have it all in one card. Like in Google Tasks.

Is there a way in which I can just keep adding sections to the same card dynamically to create a chat like interface.

I assume if it is possible on Google Tasks Add-on it should be possible in this case too. Please help me out as it is a necessary requirement.

2

There are 2 best solutions below

8
On

Update : Try this

    function renderRootCard(){
        var card = CardService.newCardBuilder();
        var section = CardService.newCardSection();
        var btnSet = CardService.newButtonSet();
        section.addWidget(CardService.newTextParagraph().setText("old widget"));
        section.addWidget(btnSet.addButton(addTaskBtn));
        card.addSection(section);

        var addTask = CardService.newAction().setFunctionName("addTask");
        var addTaskBtn = CardService.newTextButton()
            .setText("Add Task")
            .setOnClickAction(addTask); 


        return card.build();
    }

    function addTask(e) {
        var card = CardService.newCardBuilder();
        var section = CardService.newCardSection();
        var btnSet = CardService.newButtonSet();
        section.addWidget(CardService.newTextParagraph().setText("old widget"));
        section.addWidget(btnSet.addButton(addTaskBtn));
        section.addWidget(CardService.newTextParagraph().setText("new widget"));
var addTask = CardService.newAction().setFunctionName("addTask");
        var addTaskBtn = CardService.newTextButton()
            .setText("Add Task")
            .setOnClickAction(addTask); 
        return CardService.newNavigation().updateCard(card.build());
    }

Original answer:

In short: Possible

I understand what you are trying to do. This involves replacing the current card by updating it with a new card that has the same widgets as that of current card and additional widgets that are required.

Let's say, you are building a to-do app. When you click add task(say), you just create the same card, add some widgets and replace the current card with a new one.

1
On

I made corrections to the code above and now this works. The addTask and addTaskBtndefinitions for the actions needed to go before the creation of the sections and the card itself.

You also need to build the card.addSection(section); in the addTask() function.

function renderRootCard(){
        var card = CardService.newCardBuilder();
        var section = CardService.newCardSection();
        var btnSet = CardService.newButtonSet();
        var addTask = CardService.newAction().setFunctionName("addTask");
        var addTaskBtn = CardService.newTextButton()
            .setText("Add Task")
            .setOnClickAction(addTask); 
        section.addWidget(CardService.newTextParagraph().setText("old widget"));
        section.addWidget(btnSet.addButton(addTaskBtn));
        card.addSection(section);

        return card.build();
    }

    function addTask(e) {
        var card = CardService.newCardBuilder();
        var section = CardService.newCardSection();
        var btnSet = CardService.newButtonSet();
        var addTask = CardService.newAction().setFunctionName("addTask");
        var addTaskBtn = CardService.newTextButton()
            .setText("Add Task")
            .setOnClickAction(addTask); 
        section.addWidget(CardService.newTextParagraph().setText("old widget"));
        section.addWidget(btnSet.addButton(addTaskBtn));
        section.addWidget(CardService.newTextParagraph().setText("new widget"));
        card.addSection(section);

        return CardService.newNavigation().updateCard(card.build());
    }