1

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?

  • Have you read the [documentation](https://developers.google.com/apps-script/reference/card-service/action#setParameters(Object)) on `Action` class methods? – Oleg Valter is with Ukraine Jun 06 '20 at 21:40
  • You need to add setFieldName to each of your input elements and then you can refer to them as e.formInput.fieldname where fieldname is replaced by the actual field name. And then where you declare your function1(e) add a parameter like e to get populated with the data that you need. It's kind of like using a form object in html. – Cooper Jun 06 '20 at 23:48

1 Answers1

2

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();
}
Cooper
  • 59,616
  • 6
  • 23
  • 54