I have been struggling for a couple days to build a simple interface for my colleagues to record every customer interaction. They will input the following:
- Name of Customer (Autocomplete feature, from a superset of all names in a column in the spreadsheet)
- Date of Interaction
- Summary of the Interaction
- Prospects (Hot, Warm, Tepid, Cold)
My problem is in getting the Autocomplete working.
I have seen threads where @Tanaika has beautifully laid down the Server Side, HTML+JS, etc. but I am unable to get this to work. My files are attached. Thanks for your time!
HTML+JS
JavaScript
x
67
67
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<style>
5
label {
6
display: inline-block;
7
width: 150px;
8
}
9
</style>
10
11
<base target="_top">
12
<script>
13
function submitForm() {
14
google.script.run.appendRowFromFormSubmit(document.getElementById("feedbackForm"));
15
document.getElementById("form").style.display = "none";
16
document.getElementById("thanks").style.display = "block";
17
}
18
</script>
19
</head>
20
<body>
21
<datalist id="datalist">
22
<?!
23
var url = "https://docs.google.com/spreadsheets/d/13Ms0Cny3f-XaXS26s5AnrDT4H9c8p8OKRfwxPIQ9_CU/edit#gid=16760772";
24
var ss = SpreadsheetApp.openByUrl(url);
25
var ws = ss.getSheetByName("Pipeline");
26
var rng = ws.getRange('D2:D')
27
var rangeArray = rng.getValues();
28
var filArray = rangeArray.filter(function (el) {return el[0] != ""}).flat(); // Modified
29
console.info("hello read the data");
30
for (var i = 0; i < datalist.length; i++) { !?>
31
<option value="<?= datalist[i] ?>">
32
<?! } !?>
33
</datalist>
34
<div>
35
<div id="form">
36
<h1>Record Interaction</h1>
37
<form id="feedbackForm">
38
<label for="name">Parent Name</label>
39
<input type="text" id="name" name="name" list="datalist"><br><br>
40
41
<label for="doi">Date of Interaction</label>
42
<input id="today" type="date" name="doi"><br><br>
43
44
<label for="feedback">Interaction Summary</label>
45
<textarea rows=4 cols=35 id="feedback" name="feedback">Enter Interaction Summary Here
46
</textarea><br><br>
47
48
<div>
49
<label for="temperature">Likely Candidate?</label><br>
50
<input type="radio" id="Hot" name="temperature" value="Hot">
51
<label for="yes">Hot</label><br>
52
<input type="radio" id="Warm" name="temperature" value="Warm">
53
<label for="yes">Warm</label><br>
54
<input type="radio" id="Tepid" name="temperature" value="Tepid">
55
<label for="yes">Tepid</label><br>
56
<input type="radio" id="Cold" name="temperature" value="Cold">
57
<label for="no">Cold</label><br><br>
58
59
<input type="button" value="Submit Interaction" onclick="submitForm();">
60
</form>
61
</div>
62
</div>
63
<div id="thanks" style="display: none;">
64
<p>Thank you for speaking to our customers!</p>
65
</div>
66
</body>
67
</html>
CODE.GS
JavaScript
1
66
66
1
function onOpen() {
2
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
3
.createMenu('Customer Engagement')
4
.addItem('Record Interaction', 'showDialog')
5
.addToUi();
6
}
7
8
function showDialog() {
9
var html = HtmlService.createHtmlOutputFromFile('RecordInteraction.html')
10
.setWidth(400)
11
.setHeight(600);
12
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
13
.showModalDialog(html, 'Please Enter Details');
14
}
15
function readData() {
16
var url = "https://docs.google.com/spreadsheets/d/13Ms0Cny3f-XaXS26s5AnrDT4H9c8p8OKRfwxPIQ9_CU/edit#gid=16760772";
17
var ss = SpreadsheetApp.openByUrl(url);
18
var ws = ss.getSheetByName("Pipeline");
19
var rng = ws.getRange('D2:D')
20
var rangeArray = rng.getValues();
21
var filArray = rangeArray.filter(function (el) {return el[0] != ""}).flat(); // Modified
22
console.info("hello read the data")
23
return filArray;
24
}
25
26
function activateSheetById(sheetId) {
27
28
//Access all the sheets in the Google Sheets spreadsheet
29
var sheets = SpreadsheetApp.getActive().getSheets();
30
31
//Filter out sheets whose Ids do not match
32
var sheetsForId = sheets.filter(function(sheet) {
33
return sheet.getSheetId() === sheetId;
34
});
35
36
//If a sheet with the Id was found, activate it
37
if(sheetsForId.length > 0)
38
sheetsForId[0].activate();
39
}
40
41
function appendRowFromFormSubmit(form) {
42
var row = [form.name, form.doi, form.feedback, form.temperature];
43
console.info("Appending Row");
44
activateSheetById(2059810756);
45
SpreadsheetApp.getActiveSheet().appendRow(row);
46
}
47
48
function makeUL(array) {
49
// Create the list element:
50
var namelist = document.createElement('ul');
51
52
for (var i = 0; i < array.length; i++) {
53
// Create the list item:
54
var item = document.createElement('li');
55
56
// Set its contents:
57
item.appendChild(document.createTextNode(array[i]));
58
59
// Add it to the list:
60
list.appendChild(item);
61
}
62
63
// Finally, return the constructed list:
64
return namelist;
65
}
66
Advertisement
Answer
Modification points:
- In your HTML, the template is used. In this case, please use
createTemplateFromFile
instead ofcreateHtmlOutputFromFile
. - The scriptlet of
<?!= ... ?>
isForce-printing scriptlets
(like printing scriptlets except that they avoid contextual escaping.
). Ref
I thought that these are the reasons for your issue. When these points are reflected to your script, it becomes as follows.
Modified script:
Google Apps Script side:
From:
JavaScript
1
8
1
function showDialog() {
2
var html = HtmlService.createHtmlOutputFromFile('RecordInteraction.html')
3
.setWidth(400)
4
.setHeight(600);
5
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
6
.showModalDialog(html, 'Please Enter Details');
7
}
8
To:
JavaScript
1
6
1
function showDialog() {
2
var html = HtmlService.createTemplateFromFile('index.html');
3
html.data = readData();
4
SpreadsheetApp.getUi().showModalDialog(html.evaluate().setWidth(400).setHeight(600), 'Please Enter Details');
5
}
6
- Here, your function of
readData()
is used.
HTML and Javascript side:
From:
JavaScript
1
14
14
1
<datalist id="datalist">
2
<?!
3
var url = "https://docs.google.com/spreadsheets/d/###/edit#gid=16760772";
4
var ss = SpreadsheetApp.openByUrl(url);
5
var ws = ss.getSheetByName("Pipeline");
6
var rng = ws.getRange('D2:D')
7
var rangeArray = rng.getValues();
8
var filArray = rangeArray.filter(function (el) {return el[0] != ""}).flat(); // Modified
9
console.info("hello read the data");
10
for (var i = 0; i < datalist.length; i++) { !?>
11
<option value="<?= datalist[i] ?>">
12
<?! } !?>
13
</datalist>
14
To:
JavaScript
1
6
1
<datalist id="datalist">
2
<? data.forEach(e => { ?>
3
<option value="<?= e ?>">
4
<? }); ?>
5
</datalist>
6