Google app script,gmail addon get TextInput value

风流意气都作罢 提交于 2020-07-19 04:20:08

问题


I have create simple gmail addon using google script,in that i have struggle here,

how to get textinput value when we perform some action,i have checked the document, i couldn't find any methods

TextInput

The below code i have tried,

var card = CardService.newCardBuilder();
  card.setHeader(CardService.newCardHeader().setTitle("Login Here"));
  var section = CardService.newCardSection()
  var cleint_id_Input = CardService.newTextInput()
     .setFieldName("client_id")
     .setTitle("Please enter clinet id")
  var username_Input = CardService.newTextInput()
     .setFieldName("username")
     .setTitle("Please enter username")
  var password_Input = CardService.newTextInput()
     .setFieldName("password")
     .setTitle("Please enter password")
  section.addWidget(cleint_id_Input)
  section.addWidget(username_Input)
  section.addWidget(password_Input)
  Logger.log("Input Value%s",cleint_id_Input)
  //Login action button
  var action = CardService.newAction().setFunctionName('loginCallback');
  section.addWidget(CardService.newTextButton().setText('Login').setOnClickAction(action))  
  card.addSection(section)

i need inputText value in "loginCallback" function

Thanks in advance


回答1:


You can retrieve the inputted values as JSON object. The modified script retrieves the inputted values and displays them. So please modify this for your environment.

Modified script :

function buildAddOn() {
  var card = CardService.newCardBuilder();
  card.setHeader(CardService.newCardHeader().setTitle("Login Here"));
  var section = CardService.newCardSection()
  var cleint_id_Input = CardService.newTextInput()
    .setFieldName("client_id")
    .setTitle("Please enter clinet id")
  var username_Input = CardService.newTextInput()
    .setFieldName("username")
    .setTitle("Please enter username")
  var password_Input = CardService.newTextInput()
    .setFieldName("password")
    .setTitle("Please enter password")
  section.addWidget(cleint_id_Input)
  section.addWidget(username_Input)
  section.addWidget(password_Input)
  Logger.log("Input Value%s",cleint_id_Input)
  //Login action button
  var action = CardService.newAction().setFunctionName('loginCallback');
  section.addWidget(CardService.newTextButton().setText('Login').setOnClickAction(action))  
  card.addSection(section)
  return card.build() // Added
}

// Added
function loginCallback(e) {
  return CardService.newCardBuilder()
  .setHeader(CardService.newCardHeader().setTitle('sample'))
  .addSection(CardService.newCardSection().addWidget(
    CardService.newKeyValue().setContent(JSON.stringify(e.formInput, null, "  ")))
  )
  .build();
}

Inputted values :

In your case, e of loginCallback(e) is as follows.

{
    "formInput": {
        "password": "###",
        "client_id": "###",
        "username": "###"
    },
    "clientPlatform": "web",
    "messageMetadata": {
        "messageId": "###",
        "accessToken": "###"
    },
    "formInputs": {
        "password": [
            "###"
        ],
        "client_id": [
            "###"
        ],
        "username": [
            "###"
        ]
    },
    "parameters": {}
}

If I misunderstand your question, I'm sorry.



来源:https://stackoverflow.com/questions/48680165/google-app-script-gmail-addon-get-textinput-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!