在gmail插件中创建带有动态选项列表的选择

在gmail插件中创建带有动态选项列表的选择

本文介绍了在gmail插件中创建带有动态选项列表的选择(下拉)输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google App脚本SelectionInput文档显示带有静态选项列表的选择输入.尝试创建如下所示的动态选项列表:

The Google App Script SelectionInput docs show selection inputs with a static list of options. Trying to create a dynamic options list like below:

var items = [...];

var selectWidget = CardService.newSelectionInput()
  .setType(CardService.SelectionInputType.DROPDOWN)
  .setTitle("Select an item")
  .setFieldName("item");

items.forEach(function (item) {
  selectWidget.addItem(item, item, false);
});

导致运行时错误:

Object does not have property
    - /​Card/​sections[0]/​widgets[1]/​selection_control/​items. [line: 115, function: XYZ, file: Code]

我应该如何在gmail插件中创建带有动态选项的选择输入?

How should I go about creating a selection input with dynamic options in my gmail addon?

推荐答案

插件下拉列表

我已经有一段时间没有构建任何插件了,但是我知道我想在将来的某个时候做,所以我为自己做了一些例子.这是其中之一.

Addon Dropdown

I haven't built any addons in a while but I knew that I wanted to sometime in the future so I did a couple of examples for myself. This is part of one of them.

function buildMessageCard(dfltObj){
  var card=CardService.newCardBuilder();
  card.setHeader(CardService.newCardHeader().setTitle('DropDown Tester'));
  var section=CardService.newCardSection().setHeader('Making Choices');
  var makeAChoice=CardService.newSelectionInput().setType(CardService.SelectionInputType.DROPDOWN)
  .setTitle('Make a Choice')
  .setFieldName('userChoice')
  .setOnChangeAction(CardService.newAction().setFunctionName('saveChoice'))
  for(var i=1;i<dfltObj.selections;i++){
    makeAChoice.addItem('Choice ' + i, i, (i==dfltObj.finalChoice)?true:false);
  }
  section.addWidget(makeAChoice);
  var choiceText=CardService.newTextInput()
  .setTitle('Final Choice')
  .setFieldName('finalChoice')
  .setValue(dfltObj.finalChoice);
  section.addWidget(choiceText);
  card.addSection(section);
  return card.build();
}

function saveChoice(e){
  var cObj={finalChoice:e.formInput.userChoice};
  setDefaults(cObj);
  return buildMessageCard(getDefaults());
}

var Default_URL='dflturl';

function setDefaults(dfltObj){
  var ss=SpreadsheetApp.openByUrl(Default_URL);
  var sh=ss.getSheetByName('Defaults');
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  for(var i=0;i<vA.length;i++){
    if(typeof(dfltObj[vA[i][0]])!='undefined'){
      vA[i][1]=dfltObj[vA[i][0]];
    }
  }
  rg.setValues(vA);
}

function getDefaults(){
  var ss=SpreadsheetApp.openByUrl(Default_URL);
  var sh=ss.getSheetByName('Defaults');
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var dfltObj={};
  for(var i=0;i<vA.length;i++){
    dfltObj[vA[i][0]]=vA[i][1];
  }
  return dfltObj;
}

因此,您可以通过少量代码并稍加思考就可以动态更改选择的数量.

So you could change the number of selections dynamically with a little code and with a little more thought you could probably figure out how to change the options.

这是我的dfltObj的哈希表:

Here is the hash table for my dfltObj:

我希望这会有所帮助.

这篇关于在gmail插件中创建带有动态选项列表的选择(下拉)输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 14:32