Labels

Learn the powerful enterprise adaptable database:

Getting Started With ADABAS & Natural

Friday, March 10, 2017

Creating Custom Survey with Google Apps 5


.
Creating Custom Survey with Google Apps 5

OBJECTIVE

1. Create a _SurveyMasterForm.
2. Add Trigger to _SurveyMasterForm.
3. Test the survey form.

1. Run script to create _SurveyMasterForm

Script: _createSurveyMasterForm
//get required object
   var SCID =ScriptApp.getScriptId(); //SCID
   var SSDOC = DriveApp.getFileById(SCID);
   var Folders = SSDOC.getParents();
   var Folder = Folders.next();
   var FOLDERID = Folder.getId(); //FOLDERID
function showId(){
  Logger.log("SCID:"+SCID);
  Logger.log("FOLDERID:"+FOLDERID);
}
function createForm(){

  var filename="SurveyMasterForm";

  var files = Folder.getFiles();
 while (files.hasNext()) {
   var file = files.next();
   if (filename==file.getName()){
     Logger.log(file.getName());
     Folder.removeFile(file)
   }
 }
  //create the form
  var form = FormApp.create(filename);
  var formFile = DriveApp.getFileById( form.getId() );
  DriveApp.getFolderById(FOLDERID).addFile( formFile );
  /*when we create a document, it will be placed both in the parent folder and root folder*/
  /*so we need to remove them from the root folder*/
  DriveApp.getRootFolder().removeFile(formFile);
  // Update form properties via chaining.
  form.setTitle(filename)
     .setDescription("Description")
     .setConfirmationMessage('Thanks for responding!')
     .setAllowResponseEdits(true)
     .setAcceptingResponses(true);  

   // Add text fields
  form.addTextItem().setTitle('Name').setRequired(true);
  form.addTextItem().setTitle('Email').setRequired(true);
  form.addTextItem().setTitle('RefNo (optional)');  

  //return response
  return ContentService
  .createTextOutput(JSON.stringify({
    "result": "success",
    "values": filename
  }))
  .setMimeType(ContentService.MimeType.JSON);  
}  
Run the script.

2. Add Trigger to _SurveyMasterForm

Open the form _SurveyMasterForm
Open Script Editor.
Add codes.
function sendConfirmationEmail(e) {
  // e is a Form Event object - see https://developers.google.com/apps-script/guides/triggers/events#google_forms_events
  // Edit this to set the subject line for the sent email
  var subject = "Submission Successful";
  // This will show up as the sender's name
  var sendername = "Survey Master";
     
  // This is the body of the registration confirmation message
  var message = "Thank you for registering.<br>We will be in touch.<br><br>";
  message += "Your form responses were:<br><br>";
  // response is a FormResponse - see https://developers.google.com/apps-script/reference/forms/form-response
  var response = e.response;
  var textbody, sendTo, bcc;
  // Get the script owner's email address, in order to bcc: them
  bcc = Session.getActiveUser().getEmail();
  // Now loop around, getting the item responses and writing them into the email message
  var itemResponses = response.getItemResponses();
  for (var i = 0; i < itemResponses.length; i++) {
    var itemResponse = itemResponses[i];
    message += itemResponse.getItem().getTitle() +": " + itemResponse.getResponse() + "<br>";
    // If this field is the email address, then use it to fill in the sendTo variable
    // Check that your form item is named "Email" or edit to match
    if (itemResponse.getItem().getTitle() == "Email") {
      sendTo = itemResponse.getResponse();
    }
  }
  message += "<br>If you wish to edit your response, please click on <a href=\"" + response.getEditResponseUrl() + "\">this link</a>.";
  message += "<br><br>";
  textbody = message.replace("<br>", "\n");
     
  GmailApp.sendEmail(sendTo, subject, textbody,
                       {bcc: bcc, name: sendername, htmlBody: message});
}
Add Trigger.

3. Test the survey form.

Check the respondent email.

REFERENCES


.

No comments:

Post a Comment