透過「文字輸入」小工具,外掛程式可以讀取及回應使用者提供的文字。您可以設定這些小工具,為使用者提供輸入文字的自動建議。
建議可來自您提供的靜態字串清單。 或者,您也可以根據內容 (例如使用者已在小工具中輸入的文字) 建構建議。
設定建議
如要為文字輸入設定建議,只需執行下列操作:
- 建立建議清單:
- 建立靜態清單,和/或
- 定義 action,並使用回呼函式從內容動態建構該清單。
- 將建議清單和/或動作附加至文字輸入小工具。
如果您同時提供靜態建議清單和動作,應用程式 UI 會使用靜態清單,直到使用者開始輸入字元為止,此時系統會使用回呼函式,並忽略靜態清單。
靜態建議
如要提供靜態建議清單,您只需要執行下列操作:
- 建立
Suggestions物件。 - 使用
addSuggestion()或addSuggestions(),將每個靜態建議新增至其中。 - 使用
TextInput.setSuggestions()將Suggestions物件附加至小工具。
使用者介面會按照新增順序顯示靜態建議。此外,當使用者在小工具中輸入字元時,UI 也會自動執行不區分大小寫的前置字元比對,並篩選建議清單。
建議動作
如果未使用靜態建議清單,就必須定義動作,動態建構建議。只要按照以下步驟操作即可:
- 建立
Action物件,並將其與您定義的回呼函式建立關聯。 - 呼叫小工具的
TextInput.setSuggestionsAction()函式,並提供Action物件。 - 實作回呼函式,建構建議清單並傳回建構的
SuggestionsResponse物件。
每當使用者在文字輸入欄位中輸入字元時,UI 就會呼叫回呼函式,但前提是使用者必須暫停輸入片刻。回呼函式會收到事件物件,其中包含開啟的資訊卡小工具相關資訊。詳情請參閱動作事件物件。
回呼函式必須傳回有效的 SuggestionsResponse 物件,其中包含要顯示的建議清單。使用者介面會按照新增順序顯示建議。與靜態清單不同,UI 不會根據使用者輸入的內容,自動篩選回呼建議。如要進行這類篩選,您必須從事件物件讀取文字輸入值,並在建構清單時篩選建議。
範例
下列 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() 處理常式函式,在小工具失去焦點時執行。如果為同一文字輸入內容同時啟用這個處理常式和建議,系統會根據下列規則定義文字輸入互動行為:
- 選取建議後,系統會執行
setOnChangeAction()處理常式。 - 如果使用者按下 Enter 鍵 (或以其他方式讓文字輸入失去焦點),但未修改所選建議,
setOnChangeAction()就不會再次觸發。 - 如果使用者選取建議後進行編輯,導致編輯後的內容與清單中的任何建議都不相符,系統就會再次觸發
setOnChangeAction()。 - 如果使用者未選取建議,當文字輸入失去焦點時,系統會觸發
setOnChangeAction()。