6

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

Robert
  • 3,373
  • 1
  • 18
  • 34

1 Answers1

14

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.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • i have small query, how open add on when open email,without click addon icon – Robert Feb 09 '18 at 06:07
  • @Robert I think that is not small query for me. I'm sorry. I don't have the solution about it yet. When I have it, I will let you know. – Tanaike Feb 09 '18 at 07:10
  • @Robert I'm really sorry. By the way, I commented to your other question. Can I get your reply for this? https://stackoverflow.com/questions/48561117/google-apps-script-handle-global-variable-for-gmail-add-on#comment84391098_48561117 – Tanaike Feb 09 '18 at 07:14
  • did you know answer for this https://stackoverflow.com/q/48705997/7261317 – Robert Feb 10 '18 at 05:09
  • @Robert I saw it. But I cannot understand what you want to do. I'm sorry. When users inputted a text using ``newTextInput()``, do you want to change the value? – Tanaike Feb 10 '18 at 08:11
  • Yes,we need to change "setOnChangeAction" of callback function for that "newTextInput()" – Robert Feb 10 '18 at 08:15
  • @Robert Do you want to change the callback functions by the inputted text? – Tanaike Feb 10 '18 at 08:17
  • @Robert For example, when an user inputted the text of "A", you want to run "functionA". When the user inputted the text of "B", you want to run "functionB". Is my understanding correct? – Tanaike Feb 10 '18 at 08:19
  • NO,consider this,we have newTextInput().setOnChangeAction('functionA'). then we need change input value in "functionA" – Robert Feb 10 '18 at 08:24
  • @Robert I'm sorry. I cannot image the vision what you want. I'm sorry for my poor English skill. – Tanaike Feb 10 '18 at 08:31
  • @Robert Do you want to change the inputted text which is displaying in the text box? – Tanaike Feb 10 '18 at 08:34
  • Yes We need to change – Robert Feb 10 '18 at 08:37
  • @Robert ok. I could understand it. Can I have the time to think of it? When I find the solution or workaround, I will tell you. – Tanaike Feb 10 '18 at 08:45
  • Please let me know,i waiting for inputs – Robert Feb 10 '18 at 09:16