Skip to content
This repository was archived by the owner on Mar 27, 2018. It is now read-only.

Added option to include form values in the respondent notification email #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions About.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<div>
<p>
This version of <i>Form Notifications</i> is a fork of <i>Form Notifications</i> (https://github.com/googlesamples/apps-script-form-notifications-addon).
It added the possibility to add the form content to the respondent notification email so that the end-user has a copy of what he filled out.
</p>
<p>
<i>Form Notifications</i> was created as an sample add-on, and is meant
for demonstration purposes only. It should not be used for complex or
Expand Down
21 changes: 18 additions & 3 deletions Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ function adjustFormSubmitTrigger() {
var settings = PropertiesService.getDocumentProperties();
var triggerNeeded =
settings.getProperty('creatorNotify') == 'true' ||
settings.getProperty('respondentNotify') == 'true';
settings.getProperty('respondentNotify') == 'true' ||
settings.getProperty('sendContentsToRespondent') == 'true' ;

// Create a new trigger if required; delete existing trigger
// if it is not needed.
Expand Down Expand Up @@ -271,19 +272,33 @@ function sendRespondentNotification(response) {
var settings = PropertiesService.getDocumentProperties();
var emailId = settings.getProperty('respondentEmailItemId');
var emailItem = form.getItemById(parseInt(emailId));
var contentString = "";
if(settings.getProperty('sendContentsToRespondent') == 'true') {
contentString += "Your response was: \n";
var itemResponses = response.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
// Logger.log('Response #%s to the question "%s" was "%s"', (i + 1).toString(), itemResponse.getItem().getTitle(), itemResponse.getResponse());
contentString += " " + String(itemResponse.getItem().getTitle()) + ": " + String(itemResponse.getResponse()) + "\n";
};
contentString += "\n";
} else {
contentString += "Your response is not included.\n";
}
var respondentEmail = response.getResponseForItem(emailItem)
.getResponse();
if (respondentEmail) {
var template =
HtmlService.createTemplateFromFile('RespondentNotification');
template.paragraphs = settings.getProperty('responseText').split('\n');
template.responseData = contentString.split('\n');
template.notice = NOTICE;
var message = template.evaluate();
MailApp.sendEmail(respondentEmail,
settings.getProperty('responseSubject'),
message.getContent(), {
(message.getContent()), {
name: form.getTitle(),
htmlBody: message.getContent()
htmlBody: (message.getContent())
});
}
}
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ Form Notifications Add-on for Google Forms

A sample Google Apps Script add-on for Google Forms.

This version is a fork of <i>Form Notifications</i>
(https://github.com/googlesamples/apps-script-form-notifications-addon).
It added the possibility to add the form content to the respondent
notification email so that the end-user has a copy of what he filled out.

Introduction
------------

Expand Down Expand Up @@ -81,4 +86,4 @@ software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
under the License.
7 changes: 7 additions & 0 deletions RespondentNotification.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
<p><?= paragraphs[i] ?></p>
<? } ?>


<hr>

<? for (var i = 0; i < responseData.length; i++) { ?>
<p style="font-size:90%"; line-height: 90%;><?= responseData[i] ?></p>
<? } ?>

<hr>

<p style="font-size:80%">This automatic message was sent to you via the <i>Form
Expand Down
37 changes: 17 additions & 20 deletions Sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@
bottom: 54px;
top: 0;
}

.branding-text {
left: 7px;
position: relative;
top: 3px;
}

.logo {
vertical-align: middle;
}

.width-100 {
width: 100%;
box-sizing: border-box;
-webkit-box-sizing : border-box;‌
-moz-box-sizing : border-box;
}

label {
font-weight: bold;
}

#creator-options,
#respondent-options {
background-color: #eee;
Expand All @@ -36,18 +31,15 @@
border-style: solid;
display: none;
}

#creator-email,
#respondent-email,
#button-bar,
#submit-subject {
margin-bottom: 10px;
}

#response-step {
display: inline;
}

</style>

<div class="sidebar branding-below">
Expand Down Expand Up @@ -82,6 +74,8 @@
<label for="submit-notice">Notification email body:</label>
<textarea rows="8" cols="40" id="submit-notice"
class="width-100"></textarea>
<input type="checkbox" id="send-contents-to-respondent">
<label for="send-contents-to-respondent">Send contents to respondent</label>
</div>

<div class="block" id="button-bar">
Expand All @@ -107,14 +101,14 @@
$('#save-settings').click(saveSettingsToServer);
$('#creator-notify').click(toggleCreatorNotify);
$('#respondent-notify').click(toggleRespondentNotify);
// $('#send-contents-to-respondent').click(toggleSendContentsToRespondent);
$('#response-step').change(validateNumber);
google.script.run
.withSuccessHandler(loadSettings)
.withFailureHandler(showStatus)
.withUserObject($('#button-bar').get())
.getSettings();
});

/**
* Callback function that populates the notification options using
* previously saved values.
Expand All @@ -131,16 +125,17 @@
$('#submit-notice').val(!settings.responseText ?
'Thank you for responding to our form!' :
settings.responseText);

if (settings.creatorNotify === 'true') {
$('#creator-notify').prop('checked', true);
$('#creator-options').show();
}

if (settings.respondentNotify === 'true') {
$('#respondent-notify').prop('checked', true);
$('#respondent-options').show();
}
if (settings.sendContentsToRespondent === 'true') {
$('#send-contents-to-respondent').prop('checked', true);
}

// Fill the respondent email select box with the
// titles given to the form's text Items. Also include
Expand All @@ -154,6 +149,13 @@
$('#respondent-email').val(settings.respondentEmailItemId);
}

/*
function toggleSendContentsToRespondent() {
if ($('#send-contents-to-respondent').is(':checked')) {
}
}
*/

/**
* Toggles the visibility of the form creator notification options.
*/
Expand All @@ -165,7 +167,6 @@
$('#creator-options').hide();
}
}

/**
* Toggles the visibility of the form sumbitter notification options.
*/
Expand All @@ -177,7 +178,6 @@
$('#respondent-options').hide();
}
}

/**
* Ensures that the entered step is a number between 1
* and 99999, inclusive.
Expand All @@ -192,7 +192,6 @@
$('#response-step').val(99999);
}
}

/**
* Collects the options specified in the add-on sidebar and sends them to
* be saved as Properties on the server.
Expand All @@ -202,31 +201,30 @@
$('#status').remove();
var creatorNotify = $('#creator-notify').is(':checked');
var respondentNotify = $('#respondent-notify').is(':checked');
var sendContentsToRespondent = $('#send-contents-to-respondent').is(':checked');
var settings = {
'creatorNotify': creatorNotify,
'respondentNotify': respondentNotify
'respondentNotify': respondentNotify,
'sendContentsToRespondent': sendContentsToRespondent
};

// Only save creator options if notify is turned on
if (creatorNotify) {
settings.responseStep = $('#response-step').val();
settings.creatorEmail = $('#creator-email').val().trim();

// Abort save if entered email is blank
if (!settings.creatorEmail) {
showStatus('Enter an owner email', $('#button-bar'));
this.disabled = false;
return;
}
}

// Only save respondent options if notify is turned on
if (respondentNotify) {
settings.respondentEmailItemId = $('#respondent-email').val();
settings.responseSubject = $('#submit-subject').val();
settings.responseText = $('#submit-notice').val();
settings.sendContentsToRespondent = $('#send-contents-to-respondent').is(':checked');
}

// Save the settings on the server
google.script.run
.withSuccessHandler(
Expand All @@ -242,7 +240,6 @@
.withUserObject(this)
.saveSettings(settings);
}

/**
* Inserts a div that contains an status message after a given element.
*
Expand Down