2

I'm working on a card service script and am having some difficulties with the action handler.

I have two drop downs on a CardService, one is product_category and the other is sub_category.

// created as (and the following sub_category)
var productCategory = CardService.newSelectionInput()
  .setType(CardService.SelectionInputType.DROPDOWN)
  .setTitle("Product Category")
  .setFieldName("product_category")

I then created a button below, so when an action is taken, it will submit the data to a spreadsheet (that functionality i have). What i'm missing here and can't seem to dig up in the docs is how i can grab the product_category and sub_category and pass them in as parameters.

// my action caller on the button

  var newButton = CardService.newTextButton()
  .setText('Submit')
  .setOnClickAction(CardService.newAction()
                   .setFunctionName("sendToSpreadsheet"));

// adding button to section

section.addWidget(CardService.newButtonSet().addButton(newButton));

//i then have my function which takes product and sub product
function sendToSpreadsheet(product_category, sub_category){
  // process events here
}

My question is how do i pass the product_category and sub_category to the sendToSpreadsheet function? Been basing off the docs here: https://developers.google.com/apps-script/reference/card-service/card-action

Chris
  • 2,057
  • 1
  • 15
  • 25
Rem
  • 359
  • 1
  • 5
  • 15
  • Possible duplicate of [How to get the selected value of a selectbox and pass it to an action?](https://stackoverflow.com/questions/50444238/how-to-get-the-selected-value-of-a-selectbox-and-pass-it-to-an-action) – hhsb Jul 23 '18 at 09:54

2 Answers2

2

Update:

Figured this one out, there is a formInputs object in the callback's e object that you can use to access in the function.

I switched my sendToSpreadsheet to the following

 function sendToSpreadsheet(e){
     var res = e['formInputs'];
     var productCat = res['product_category'];
     var productSubCat = res['product_sub_category'];
     // rest of code...
}
Aaron Dunigan AtLee
  • 1,860
  • 7
  • 18
Rem
  • 359
  • 1
  • 5
  • 15
1

If the above is not working you can use

e.formInputs.product_category;

e.formInputs returns a JSON Object and you can fetch the data by the Field name provided.