Search code examples
google-apps-scriptgmail-addons

Google app script,gmail addon get TextInput value


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


Solution

  • 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.