针对文本输入的自动补全建议

文本输入 widget 让您的插件能够读取用户提供的文本并做出响应。您可以 配置这些微件 输入文本。

所提供的建议可以来自您提供的静态字符串列表。 或者,您也可以根据上下文生成建议, 。

配置建议

您只需执行以下操作即可为文本输入配置建议: 以下:

  • 通过以下方式创建建议列表:
    • 创建静态列表,和/或
    • 定义一项操作 一个根据上下文动态构建该列表的回调函数。
  • 将建议列表和/或操作附加到文本输入 widget。

如果您同时提供静态建议列表和操作, 应用界面使用静态列表,直到用户开始输入字符, 然后使用回调函数并忽略静态列表。

静态建议

如需提供静态的建议列表,您只需执行以下操作:

  1. 创建 Suggestions 对象。
  2. 使用 addSuggestion() 将每条静态建议添加到其中 或 addSuggestions()
  3. Suggestions 使用 TextInput.setSuggestions() 将对象分配给 widget。

界面会按照添加顺序显示静态建议。界面 还会自动执行不区分大小写的前缀匹配, 。

建议操作

如果您使用的不是静态建议列表,则必须定义一项操作 动态生成建议。您可按以下步骤实现这一目的:

  1. 创建 Action 对象 并将其与某个回调函数相关联 定义。
  2. 调用 widget 的 TextInput.setSuggestionsAction() 函数,为其提供 Action 对象。
  3. 实现回调函数以构建建议列表并返回 构建的 SuggestionsResponse 对象。

每当用户在 并在用户停止输入一段时间后触发。通过 回调函数将收到一个事件对象,其中包含有关 打开卡片的小部件请参阅 操作事件对象 了解详情。

回调函数必须返回有效的 SuggestionsResponse 对象,该对象包含要显示的建议列表。界面会显示 按添加顺序排列建议与静态列表不同, 不针对用户信息自动过滤回调建议 输入。如果您想进行此类过滤,则必须读取文本输入值 事件对象,并在构建列表时过滤建议。

示例

以下 Google Workspace 插件代码段 显示了如何配置建议 两个不同的文本输入微件上,第一个微件具有静态列表, 再使用回调函数:

// Create an input with a static suggestion list.
var textInput1 = CardService.newTextInput()
    .setFieldName('colorInput')
    .setTitle('Color choice')
    .setSuggestions(CardService.newSuggestions()
        .addSuggestion('Red')
        .addSuggestion('Yellow')
        .addSuggestions(['Blue', 'Black', 'Green']));

// Create an input with a dynamic suggestion list.
var action = CardService.newAction()
    .setFunctionName('refreshSuggestions');
var textInput2 = CardService.newTextInput()
    .setFieldName('emailInput')
    .setTitle('Email')
    .setSuggestionsAction(action);

// ...

/**
 *  Build and return a suggestion response. In this case, the suggestions
 *  are a list of emails taken from the To: and CC: lists of the open
 *  message in Gmail, filtered by the text that the user has already
 *  entered. This method assumes the Google Workspace
 *  add-on extends Gmail; the add-on only calls this method for cards
 *  displayed when the user has entered a message context.
 *
 *  @param {Object} e the event object containing data associated with
 *      this text input widget.
 *  @return {SuggestionsResponse}
 */
 function refreshSuggestions(e) {
   // Activate temporary Gmail scopes, in this case so that the
   // open message metadata can be read.
   var accessToken = e.gmail.accessToken;
   GmailApp.setCurrentMessageAccessToken(accessToken);

   var userInput = e && e.formInput['emailInput'].toLowerCase();
   var messageId = e.gmail.messageId;
   var message = GmailApp.getMessageById(messageId);

   // Combine the comma-separated returned by these methods.
   var addresses = message.getTo() + ',' + message.getCc();

   // Filter the address list to those containing the text the user
   // has already entered.
   var suggestionList = [];
   addresses.split(',').forEach(function(email) {
     if (email.toLowerCase().indexOf(userInput) !== -1) {
       suggestionList.push(email);
     }
   });
   suggestionList.sort();

   return CardService.newSuggestionsResponseBuilder()
       .setSuggestions(CardService.newSuggestions()
           .addSuggestions(suggestionList))
       .build();  // Don't forget to build the response!
 }

建议和OnChangeAction()

文本输入微件可以 setOnChangeAction() 定义的处理程序函数,会在 widget 失去焦点时执行。 如果为同一文本输入同时启用了此处理程序和建议, 以下规则定义了文本输入交互行为:

  1. setOnChangeAction() 处理程序会在选择建议后执行。
  2. 如果用户按 Enter 键(或以其他方式使文本输入失去焦点) 在不修改所选建议的情况下,setOnChangeAction()不会 触发。
  3. 如果用户在选择某个事件后,setOnChangeAction() 会再次触发, 请编辑该建议,使其不再与任何建议相匹配 。
  4. 如果用户未选择建议,则会触发 setOnChangeAction() 当文本输入失去焦点时。