1

I want to known is there any way to update the card when I close the dialog

//this function is called when click a button in the card
function showDialog() {
    const html = HtmlService.createHtmlOutputFromFile('dialog.html')
        .setWidth(600)
        .setHeight(425)
        .setSandboxMode(HtmlService.SandboxMode.IFRAME);
    SpreadsheetApp.getUi().showModalDialog(html, 'Select the spreadsheets');
}

//this function is called by client code
function callback(text) {
    var card = CardService.newCardBuilder().build();
    var navigation = CardService.newNavigation().updateCard(card);
  return CardService.newActionResponseBuilder()
    .setNavigation(navigation)
    .setNotification('Import Succeed')
    .build();
}

dialog.html

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
  <script>
    function onApiLoad() {
      gapi.load('picker', {'callback': function() {
        ......
      }});
     }

    function createPicker(token) {
      if (pickerApiLoaded && token) {
        var picker = new google.picker.PickerBuilder()
            .addView(new google.picker.DocsView(google.picker.ViewId.SPREADSHEETS)
              .setMode(google.picker.DocsViewMode.LIST)
            .setCallback(pickerCallback)
            .........
            .build();
        picker.setVisible(true);
      } else {
        showError('Unable to load the file picker.');
      }
    }

    function pickerCallback(data) {
      var action = data[google.picker.Response.ACTION];
      if (action == google.picker.Action.PICKED) {       
        google.script.run.callback('');
        google.script.host.close();
      } else if (action == google.picker.Action.CANCEL) {
        google.script.host.close();
      }
    }
  </script>
</head>
<body>
  <script src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
</body>
</html>

I can write the text in the current spreadsheet developer metadata, and check the developer metadata in a dead loop in the card like the below code, but if user didn't close the dialog in time, google will throw timeout error in the card

//this function is called when click a button in the card
function showDialog() {
    const html = HtmlService.createHtmlOutputFromFile('dialog.html')
        .setWidth(600)
        .setHeight(425)
        .setSandboxMode(HtmlService.SandboxMode.IFRAME);
    SpreadsheetApp.getUi().showModalDialog(html, 'Select the spreadsheets');
    //But I must return a response within a certain time since google has time limit for response
    var metadataList = SpreadsheetApp.getActive().getDeveloperMetadata();
    //find the text in the metadataList, and return the response 
}

//this function is called by client code
function callback(text) {
    //updat the card with text
    SpreadsheetApp.getActive().addDeveloperMetadata('text', text);
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
Hamu
  • 11
  • 2
  • 1
    Welcome to [so]. What do you mean by "update the card"? P.S. Please add a [mcve] (if a function is relevant, and that function is called from client-side code, include the client side code that calls that function) – Rubén Dec 06 '22 at 03:19
  • thank you for your advise, I already modify my question based on your advise, please check again – Hamu Dec 06 '22 at 09:28

1 Answers1

1

Tl;Dr There is no way to update a card when a dialog is closed.


While an Editor add-on and Workspace add-on might share the same Google Apps Script project / Google Cloud project they are still two different add-ons. At this time there is no way to directly communicate two add-ons.

Workspace add-ons don't have triggers for file edit / changes, only for user clicking a card button.

For your current approach, I think that the only thing that you can do is to provide instructions to the user to reopen the card.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166