3

I create a Gmail add-on with a form. In this form, I have input but in my script, I can not find how to retrieve the value of input in variables. The following code opens a sidebar in the Gmail inbox when you click on an e-mail and fill in the form's input with the mail's metadata and contact information. If the contact does not exist, the user can create it by entering the name and first name of the sender (the e-mail address is retrieved from the e-mail). But I do not know how to recover the information entered by the user.

Here is my script:

function buildAddOn(e) {
  // Activer les extensions complémentaires de Gmail
  var accessToken = e.messageMetadata.accessToken;
  GmailApp.setCurrentMessageAccessToken(accessToken);
  
  var messageId = e.messageMetadata.messageId;
  var message = GmailApp.getMessageById(messageId);
  var emailAddress = recupEmail(message.getFrom(), '<', '>'); // Dans le message, l'adresse mail de l'expéditeur se trouve entre les balises '<' et '>'
  var objet = message.getSubject();

  var contact = ContactsApp.getContact(emailAddress);
  var contactNom;
  var contactPrenom;
  
  if(contact != null){
    contactPrenom = contact.getFamilyName();
    contactNom = contact.getFullName().replace(contactPrenom, '');
  }else{
    contactNom = '';
    contactPrenom = '';
  }// if-else
  
  var head = 'Expediteur';
  var labelNom = 'Nom';
  var labelPrenom = 'Prenom';
  var labelEmail = 'Adresse e-mail';
  var labelObjet = 'Objet';
  var labelMessage = 'Message';
  
  var action = CardService.newAction().setFunctionName('creatContact');
  var section = CardService.newCardSection();
  section.setHeader(head);
  section.addWidget(CardService.newTextInput().setFieldName('nom').setTitle(labelNom).setValue(contactPrenom));
  section.addWidget(CardService.newTextInput().setFieldName('prenom').setTitle(labelPrenom).setValue(contactNom));
  if(contact == null){
    section.addWidget(CardService.newTextButton().setText('Ajouter au contacts').setOnClickAction(action));
  }
  section.addWidget(CardService.newTextInput().setFieldName('mail').setTitle(labelEmail).setValue(emailAddress));
  section.addWidget(CardService.newTextInput().setFieldName('objet').setTitle(labelObjet).setValue(objet));

  var card = CardService.newCardBuilder()
  .addSection(section)
  .build();
  
  return [card];
}

function recupEmail(from, debut, fin){
  var result = from.split(debut)[1].split(fin)[0];
  return result;
}

function creatContact(){
  /*
  var givenName = ??;
  var familyName = ??;
  var email = ??;
  */
  ContactsApp.createContact(givenName, familyName, email)
}

Thank you in advance. Sorry for my bad english.

Flow
  • 33
  • 7
  • Refer this https://stackoverflow.com/questions/50444238/how-to-get-the-selected-value-of-a-selectbox-and-pass-it-to-an-action/50444741#50444741 as well. – hhsb Jan 29 '19 at 13:41
  • TY Hari. That's what I found. – Flow Jan 29 '19 at 14:36

2 Answers2

1

I've not used Gmail Addons extensively but here's a portion of one that I use to save notes in a spreadsheet that is used by another WebApp as sort of a personal assistant. It allows the user to pick a note type, of which there are many, and enter comments and load it into a spreadsheet.

function buildAddOn(e) {
  var accessToken = e.messageMetadata.accessToken;
  GmailApp.setCurrentMessageAccessToken(accessToken);
  var messageId = e.messageMetadata.messageId;
  var cards = [];
  cards.push(buildGetNotesTypesSelectionInput());
  return cards;
}

function buildGetNotesTypesSelectionInput(){
  var card=CardService.newCardBuilder();
  card.setHeader(CardService.newCardHeader().setTitle('Don\'t Forget - Notes'));
  var section=CardService.newCardSection().setHeader('Notes');
  var typeSelect=CardService.newSelectionInput()
  .setType(CardService.SelectionInputType.DROPDOWN)
  .setTitle('Select Note Type')
  .setFieldName('Note_Type');
  typeSelect.addItem('', '', true);
  var optsA=getNoteTypes();
  Logger.log('\nselect1 optsA:\n');
  for(var i=0;i<optsA.length;i++){
    typeSelect.addItem(optsA[i], optsA[i],false);
    Logger.log('\noptsA[%s]=%s',i,optsA[i]);
  }
  //typeSelect.setOnChangeAction(CardService.newAction().setFunctionName('select1Change'));
  section.addWidget(typeSelect);
  var commentText=CardService.newTextInput()
  .setFieldName('Comments')
  .setMultiline(true)
  .setTitle('Comments');
  section.addWidget(commentText);
  var action=CardService.newAction().setFunctionName('saveNote');
  var saveButton=CardService.newTextButton()
  .setText('Save')
  .setOnClickAction(action);
  section.addWidget(saveButton);
  card.addSection(section);
  return card.build();
}

function saveNote(e){
  var row=[];
  var ts=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "E MMM dd,yyyy HH:mm:ss");
  row.push(ts,e.formInput.Note_Type,e.formInput.Comments,Session.getActiveUser().getEmail());
  var ss=SpreadsheetApp.openById(gDefault.DontForgetId);
  var sh=ss.getSheetByName('Notes');
  sh.appendRow(row);
  return CardService.newCardBuilder()
  .setHeader(CardService.newCardHeader().setTitle('Note Saved'))
  .addSection(CardService.newCardSection().addWidget(CardService.newTextParagraph().setText(Utilities.formatString('TimeStamp: %s\nNote Type: %s\nNote: %s\nEmail: %s\n', ts,e.formInput.Note_Type,e.formInput.Comments,Session.getActiveUser().getEmail()))))
  .build();
}


function getNoteTypes(){
  var ss=SpreadsheetApp.openById(gDefault.DontForgetId);
  var sh=ss.getSheetByName('Options');
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var oA=[];
  for(var i=1;i<vA.length;i++){
    oA.push(vA[i][0]);
  }
  return oA;
}

I realize this isn't the exact answer you were requesting but hopefully it will help you to gain some insight into your problem.

Cooper
  • 59,616
  • 6
  • 23
  • 54
1

change your contact function signature to

createContact(e) { 
.. 
}

the e parameter will be populated with all the goodies you need. Message Meta data, form inputs etc...

JBone
  • 3,163
  • 11
  • 36
  • 47
  • Event object structure reference: https://developers.google.com/gsuite/add-ons/concepts/event-objects – Optimae Aug 08 '20 at 15:49