I successfully created a web application using Google Apps Script (GAS) that sends data on submission to Spreadsheet A. Furthermore, I have implemented a select option that dynamically fetches data from another spreadsheet B ("xxx") in column A.
Below is the code snippet:
In Code.gs:
function getAddressOptions() {
var sheet = SpreadsheetApp.openById("xxx").getSheetByName("Sheet3");
var lastRow = sheet.getLastRow();
var myRange = sheet.getRange("A1:A" + lastRow);
var data = myRange.getValues();
var optionsHTML = "";
for (var i = 0; i < data.length; i+=1) {
optionsHTML += '<option>' + data[i][0] + '</option>';
};
return optionsHTML;
}
In Index.html:
<select class="custom-select" name="test" id="test">
<?= getAddressOptions(); ?>
</select>
I intend to auto-fill two other fields in the form based on Columns B and C corresponding to the value in Col A selected in the dropdown.
For example, if "Italy" is chosen in the select option from spreadsheet B ("xxx"), there would be two readonly fields with the values "Tom" and "Red."
+-----------------------+---------+-------+
| Col A (Select Option) | Col B | Col C |
+-----------------------+---------+-------+
| Italy | Tom | Red |
| USA | Michael | Green |
| Africa | Anna | Blue |
+-----------------------+---------+-------+
(Output)
https://i.sstatic.net/dNL5p.jpg
What steps should I follow next?
Update 1 (Attempting solution provided by Tanaike)
Code.gs
function doGet(request) {
return HtmlService.createTemplateFromFile('Index')
.evaluate();
}
/* @Include JavaScript and CSS Files */
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
/* @Process Form */
function processForm(formObject) {
var url = "xxxx";
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("Data");
ws.appendRow([formObject.company,
formObject.test,
formObject.field1,
formObject.field2]);
}
function getAddressOptions() {
var sheet = SpreadsheetApp.openById("xxxx").getSheetByName("Sheet3");
var lastRow = sheet.getLastRow();
var myRange = sheet.getRange("A2:C" + lastRow);
var data = myRange.getValues();
var optionsHTML = "";
for (var i = 0; i < data.length; i+=1) {
optionsHTML += `<option data-values="${data[i][1]},${data[i][2]}">${data[i][0]}</option>`;
};
return optionsHTML;
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-8">
<form id="myForm" onsubmit="handleFormSubmit(this)">
<p class="h4 mb-4 text-center">Contact Details</p>
<div class="form-row">
<div class="form-group col-md-12">
<label for="company">Company</label>
<input type="text" class="form-control" id="company" name="company" required>
</div>
<div class="form-group col-md-2">
<label for="test">Test field 1</label>
<select class="custom-select" name="test" id="test">
<?!= getAddressOptions(); ?>
</select>
</div>
<div class="form-group col-md-5">
<label for="field1">Field 1</label>
<input type="text" class="form-control" id="field1" name="field1" disabled>
</div>
<div class="form-group col-md-5">
<label for="field2">Field 2</label>
<input type="text" class="form-control" id="field2" name="field2" disabled>
</div>
</div>
<button type="submit" class="btn btn-primary btn-block">Insert in Master Leads</button>
</form>
<div id="output"></div>
</div>
</div>
</div>
</body>
<?!= include('JavaScript'); ?>
</html>
Javascript.html
<script>
// Prevent forms from submitting.
function preventFormSubmit() {
var forms = document.querySelectorAll('form');
for (var i = 0; i < forms.length; i++) {
forms[i].addEventListener('submit', function(event) {
event.preventDefault();
});
}
}
window.addEventListener('load', preventFormSubmit);
function handleFormSubmit(formObject) {
google.script.run.processForm(formObject);
document.getElementById("myForm").reset();
}
function setValues(select) {
const [value1, value2] = select.options[select.selectedIndex].dataset.values.split(",");
document.getElementById("field1").value = value1;
document.getElementById("field2").value = value2;
}
const select = document.getElementById("test");
setValues(select);
select.addEventListener("change", () => setValues(select));
</script>