I have three drop down fields, one is pulling list of accounts and another drop down field is pulling the list of properties associated with that account. Once the property is selected, the third drop down is populated.
The second drop down field will get populated when the account value is selected. The third drop down value will get selected when second drop down value is selected.
Issue:
I select an account, the second box gets populated with a value. Let’s say the value is ABC. My desired selection for second box is ABC, now in order to populate the third drop down, we need select2 function to trigger. So I open the dropdown and select another value and then select ABC again to trigger the function
Explained Visually.
//Account Drop Down
//Sample Values - getAccounts()
[ { createTime: 'xxxxx',
regionCode: 'SA',
displayName: 'ABC',
name: 'accounts/XXXX',
updateTime: 'xxxx' } ]
<div class="form-group">
<label for="accountData">Select Account:</label>
<select id="accountData" style="width: 100%" onchange="select1(this)">
<? var data = getAccounts () ?>
<? for (i=0; i<data.length; i++) { ?>
<option>Select Account</option>
<option value="<?= data[i].name.split('/')[1] ?>" ><?= data[i].displayName ?></option>
<? } ?>
</select>
</div>
//Property Field Drop Down
<div class="form-group">
<label for="accountData2">Select Property:</label>
<select id="accountData2" style="width: 100%" onchange="select2(this)">
<option>Select Properties</option>
</select>
</div>
//Third Dropdown
<div class="form-group">
<label for="accountData3">Select Data Streams:</label>
<select id="accountData3" style="width: 100%" onchange="">
<option>Select Streams</option>
</select>
</div>
function select1(e) {
google.script.run.withSuccessHandler(ar => {
const select = document.getElementById("accountData2");
select.innerHTML = "";
if (ar.length == 0) return;
ar.properties.forEach(item => {
const option = document.createElement("option");
option.text = item.displayName;
option.value = item.name.split('/')[1];
select.appendChild(option);
});
}).withFailureHandler(er => {
alert('This is error:' + er);
}).listProperties(e.value);
}
function select2(e) {
console.log(`${e.value} was selected.`);
google.script.run.withSuccessHandler(ar => {
console.log(ar)
const select = document.getElementById("accountData3");
select.innerHTML = "";
if (ar.length == 0) return;
ar.forEach(item => {
const option = document.createElement("option");
option.text = item.displayName;
option.value = item.name.split('/')[3];
select.appendChild(option);
});
}).withFailureHandler(er => {
alert('This is error:' + er);
}).listDataStreams(e.value);
}
2
Answers
onEdit is a server side trigger not a client side. You have an enormous amount of client side events in the browser. Please check the references below to learn more about them. Also you will probably like to learn how to use Chrome Dev Tools to assist you in debugging your clientside code.
The only method available server side on Spreadsheets are the drop downs that at provided via Class DataValidation
From your provided whole script, how about the following modification?
Google Apps Script side:
HTML & Javascript side:
In this modification, when
showSidebar()
is run, a sidebar is opened on Google Spreadsheet. The flow is as follows.In this case, even when the response value is only one, the 1st values of the 3 dropdown lists are "Selected Account", "Select Properties", and "Select Streams", respectively. By this, you can select the response value from the dropdown list and the onChange event can be fired.