Search code examples
google-apps-scriptgmail-addons

How to get the selected value of a selectbox and pass it to an action?


I need to pass the value of the selectbox to it's widget's handler function

var payment = CardService.newSelectionInput()
  .setType(CardService.SelectionInputType.DROPDOWN)
  .setTitle("payment mode")
  .setFieldName("payment_mode")
  .addItem("Debit card", "contact", false)
  .addItem("Net banking", "lead", true)
  .setOnChangeAction(CardService.newAction().setFunctionName("onModeChange"));
section.addWidget(payment);

I expect something like setFunctionName("onModeChange").setParams(payment.value)


Solution

  • I think that your script is almost the correct. You can achieve what you want by modifying a little. In your case, you want to retrieve contact or lead at the function onModeChange. If my understanding is correct, how about this modification?

    In your script, you can retrieve the selected values as follows. When an user selects, the selected values are sent to onModeChange, immediately. Please add this function in your script.

    function onModeChange(e) {
        console.log(e.formInput.payment_mode)
    }
    

    If the user selects Debit card and Net banking, you can retrieve contact and lead at e.formInput.payment_mode, respectively.

    If you want to give the additional values when the user selects, please modify

    From
    .setOnChangeAction(CardService.newAction().setFunctionName("onModeChange"))
    
    To :
    .setOnChangeAction(CardService.newAction().setFunctionName("onModeChange").setParameters({key1: "value1"}))
    

    By this modification, at onModeChange(e), you can retrieve the added values by e.parameters.key1.

    Note :

    • This sample supposes that both your script works and you can see the select box at the gmail addon. If your script didn't work yet, please tell me.

    References :

    If I misunderstand your question, I'm sorry.