1

I'm building an Addon used with Google Sheets which has a root homepage and 3 sub-Cards.

Card 1 generates folders and files and documents for orders based on sheet data which have an associated order number (list of whole integers).

Card 2 includes a Dropdown menu which is populated on Card creation from values (order numbers) from a column on a tab on the spreadsheet which is itself updated via function calls when any docs/orders are generated (using Card 1).

What I'm trying to do is update the values containing the order numbers on the Dropdown menu on Card 2 when new documents are created using Card 1. I'm successfully updating the spreadsheet tab with the new order numbers when docs are generated but I cannot get the Addon dropdown to update (properly - see below) with the new order numbers.

As I understand it, Cards cannot be refreshed, only replaced entirely, which should be fine but I cannot get it to work. I've seen this question but it doesn't seem to completely replace/refresh Card 2 with the new dropdown data.

I build the Addon with this:

function startAddon(e) {

  return [createGenerateDocsCard(), createManageOrdersCard(), createResetSheetCard()];

}

The relevant Card which includes the Dropdown menu:

function createManageOrdersCard() {
//Get list of Order Numbers from sheet
  let orderList = ss.getRange("C2!A1:A1000").getDisplayValues().filter(String);

//Generate dropdown using order numbers from sheet
  let orderDropdown = generateDropdown("Order", "CHOOSE EXISTING ORDER TO LOAD:", previousSelected, orderList);

  let manageOrdersCard = CardService
    .newCardBuilder()
    .setHeader(
      CardService.newCardHeader()
        .setTitle(' MANAGE ORDERS')
        .setSubtitle('Switch between existing Order sheets')
    )
    .addSection(
      CardService.newCardSection()
        .addWidget(
//Insert generated dropdown
          orderDropdown
            .setOnChangeAction(CardService.newAction().setFunctionName("handleOrderDropdown"))
        )
        .addWidget(CardService.newTextButton()
          .setText("SYNC ORDER NUMBERS")
          .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
          .setOnClickAction(CardService.newAction().setFunctionName('syncOrderNumbersButton'))
        )
    )
  return manageOrdersCard.build();
}

The function which generates the Dropdown:

function generateDropdown(fieldName, fieldTitle, previousSelected, items) {
  var selectionInput = CardService.newSelectionInput().setTitle(fieldTitle)
    .setFieldName(fieldName)
    .setType(CardService.SelectionInputType.DROPDOWN);

  items.forEach((item, index) => {
    selectionInput.addItem(item[0], item[0], item[0] == previousSelected);
    Logger.log(previousSelected);
  })
  return selectionInput;
}

And the code I'm using to attempt to fully replace the Card with a new one including updated Dropdown entries:

function syncOrderNumbersButton(e) {
  syncOrderNumbers();

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

}

At the moment, I'm testing using a button to initiate inserting an updated list of orders into the spreadsheet using syncOrderNumbers() (which works) and then (attempting to) re-create Card 2 which should then have the new Dropdown entries. Ultimately this process will happen automatically without a button click when new order files are created.

What's happening though is that Card 2 appears to update and the new order number does show in the Dropdown, but when I navigate (using back button at top of Addon) to the root card (Addon homepage) and then navigate again to Card 2, the Dropdown no longer has the order number in it which previously showed.

When createManageOrdersCard() is run, the generateDropdown() function is within it, so I don't understand why a refreshed list for the Dropdown isn't being created/displayed and why it seems to revert to using the list from when the Addon first loaded?

Is it possible to do a full refresh of Card 2 so that even if navigated away and back again, the Dropdown it contains will represent a fully updated list of values taken from the spreadsheet?

Would really appreciate any help figuring this out.

Diagonali
  • 109
  • 1
  • 10
  • There are missing functions on your code such as createGenerateDocsCard() & syncOrderNumbers(). Please share your complete script with sample sheet file that contains few sample data (data that are similar to your actual sheet) for us to replicate your code and investigate. – SputnikDrunk2 Jul 15 '21 at 19:12
  • @IrvinJayG. Thanks for your reply. Those functions aren't related to my issue since createGenerateDocsCard() produces a card not related to my question and syncOrderNumbers() simply writes out a column of integers ("47234", "18382", "948382") etc to the sheet which the Addon then reads with generateDropdown(). The problem I'm having is fully refreshing the Addon Card "manageOrdersCard" as described. – Diagonali Jul 16 '21 at 08:25
  • 1
    I've now realised that the Card which includes the dropdown does in fact "update" but when I press the "back" button in the Addon, it takes me to the original homepage with the original cards. This is odd, since I'd have thought the .updateCard() method would replace the card in-place as the docs suggest? I'm now trying to re-create the homepage cards (works ok) and then navigate back to the new card which includes the updated dropdown. There has to be a better way than this though? I'm not quite managing to do this at the moment... – Diagonali Jul 16 '21 at 11:45
  • On further investigation, I'm now clearer that what I think I need to do is _refresh_ the Homepage card (which is auto generated from the manifest function) and _then_ navigate to the updated card I was on (ideally). – Diagonali Jul 16 '21 at 15:58

0 Answers0