Skip to content

Instantly share code, notes, and snippets.

@develmaycare
Last active May 24, 2018 13:36
Show Gist options
  • Save develmaycare/b0c5f691b9a55457938db02757126ecb to your computer and use it in GitHub Desktop.
Save develmaycare/b0c5f691b9a55457938db02757126ecb to your computer and use it in GitHub Desktop.
A dialog for Google Sheets for selecting multiple elements in a validation lookup.
/* To use this in a Google Sheet:
1. Go to Tools > Script Editor.
2. Paste this script and click on the bug symbol.
3. Create an HTML page and paste the contents of page.html into the editor.
4. Authorize the script.
5. Refresh the sheet.
*/
/* Set up multi-select validation. Sort of. There will be a validation error,
but this does allow you to select multiple items.
*/
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('Options')
.addItem('Multi-Select Dialog', 'showDialog')
.addToUi();
}
function showDialog() {
var html = HtmlService.createTemplateFromFile('Page').evaluate();
SpreadsheetApp.getUi().showSidebar(html);
}
var valid = function(){
try{
return SpreadsheetApp.getActiveRange().getDataValidation().getCriteriaValues()[0].getValues();
}
catch(e){
return null
}
}
function fillCell(e){
var s = [];
for(var i in e){
if(i.substr(0, 2) == 'ch') s.push(e[i]);
}
if(s.length) SpreadsheetApp.getActiveRange().setValue(s.join(', '));
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div>
<? var data = valid(); ?>
<form id="form" name="form">
<? if(Object.prototype.toString.call(data) === '[object Array]') { ?>
<? for (var i = 0; i < data.length; i++) { ?>
<? for (var j = 0; j < data[i].length; j++) { ?>
<input type="checkbox" id="ch<?= '' + i + j ?>" name="ch<?= '' + i + j ?>" value="<?= data[i][j] ?>"><?= data[i][j] ?><br>
<? } ?>
<? } ?>
<? } else { ?>
<p>May be current cell havn't the <a href="https://support.google.com/drive/answer/139705?hl=en">Data validation...</a></p>
<? } ?>
<input type="button" value="Fill Current Selections" onclick="google.script.run.fillCell(this.parentNode)" />
<!--
<input type="button" value="Validation From Existing Selections" onclick="google.script.run.showDialog()" />
-->
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment