google-apps-scriptgmail-addons

Pass all parameters from CardService Card builder to a function


I've prepared my CardService Card:

return CardService
  .newCardBuilder()
  .setHeader(
    CardService.newCardHeader()
    .setTitle('Elenco operazioni rapide')
    .setSubtitle('Inserisci rapidamente gli eventi con questi tasti')) 
  .addSection(
    CardService.newCardSection()
    .addWidget(widget)
    .addWidget(datatimepickerstart)
    .addWidget(datatimepickerend)
    .addWidget(button))

   .build();

When I press button:

var action = CardService.newAction()
      .setFunctionName('function1')

      var button = CardService.newTextButton()
      .setText('Calendario')
      .setOnClickAction(action)
      .setTextButtonStyle(CardService.TextButtonStyle.FILLED);

I would like to pass all parameters from the builder (checkbox choice, datatimepickerstart, datatimepickerend, etc...) to function1. How can I do it?


Solution

  • Here's an example of something I did several years ago:

    function buildGetNotesTypesSelectionInput(){
      var card=CardService.newCardBuilder();
      card.setHeader(CardService.newCardHeader().setTitle('Don\'t Forget - Notes'));
      var section=CardService.newCardSection().setHeader('Notes');
      var typeSelect=CardService.newSelectionInput()
      .setType(CardService.SelectionInputType.DROPDOWN)
      .setTitle('Select Note Type')
      .setFieldName('Note_Type');//field name
      typeSelect.addItem('', '', true);
      var optsA=getNoteTypes();
      Logger.log('\nselect1 optsA:\n');
      for(var i=0;i<optsA.length;i++){
        typeSelect.addItem(optsA[i], optsA[i],false);
        Logger.log('\noptsA[%s]=%s',i,optsA[i]);
      }
      //typeSelect.setOnChangeAction(CardService.newAction().setFunctionName('select1Change'));
      section.addWidget(typeSelect);
      var commentText=CardService.newTextInput()
      .setFieldName('Comments')//field name
      .setMultiline(true)
      .setTitle('Comments');
      section.addWidget(commentText);
      var action=CardService.newAction().setFunctionName('saveNote');
      var saveButton=CardService.newTextButton()
      .setText('Save')
      .setOnClickAction(action);
      section.addWidget(saveButton);
      card.addSection(section);
      return card.build();
    }
    

    The following function is getting parameters from cardservice via the field object

    function saveNote(e){
      var row=[];
      var ts=Utilities.formatDate(new Date(), "America/Denver", "E MMM dd,yyyy HH:mm:ss");
      row.push(ts,e.formInput.Note_Type,e.formInput.Comments,Session.getActiveUser().getEmail());//see the fieldnames in this object which is outputting data to a spreadsheet from the addon.
      var ss=SpreadsheetApp.openById(gDefault.DontForgetId);
      var sh=ss.getSheetByName('Notes');
      sh.appendRow(row);
      var action=CardService.newAction().setFunctionName('buildGetNotesTypesSelectionInput');  
      return CardService.newCardBuilder()
      .setHeader(CardService.newCardHeader().setTitle('Note Saved'))
      .addSection(CardService.newCardSection().addWidget(CardService.newTextParagraph().setText(Utilities.formatString('TimeStamp: %s\nNote Type: %s\nNote: %s\nEmail: %s\n', ts,e.formInput.Note_Type,e.formInput.Comments,Session.getActiveUser().getEmail()))))
      .addSection(CardService.newCardSection().addWidget(CardService.newTextButton().setText('Add Another Note').setOnClickAction(action)))
      .build();
    }