Google Sheets Apps Script – Dynamic Dependent Dropdown Data Validation Lists

Learn how to assign data validation using Apps Scripts and create dynamic dependent dropdown lists for the whole column for Google Sheets.

function onEdit(){
  var tabLists = "lists";
  var tabValidation = "Main";
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var datass = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabLists);
  
  var activeCell = ss.getActiveCell();
  
  if(activeCell.getColumn() == 1 && activeCell.getRow() > 1 && ss.getSheetName() == tabValidation){
    
    activeCell.offset(0, 1).clearContent().clearDataValidations();
    
    var makes = datass.getRange(1, 1, 1, datass.getLastColumn()).getValues();
    
    var makeIndex = makes[0].indexOf(activeCell.getValue()) + 1;
    
    if(makeIndex != 0){
    
        var validationRange = datass.getRange(3, makeIndex, datass.getLastRow());
        var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
        activeCell.offset(0, 1).setDataValidation(validationRule);
  
     }  
      
  }
  
}

Understand How This Script Works

 

Google Sheets Apps Script – Combine Multiple Tabs to a Master Tab When Column Positions Don’t Match

Results of Apps Script tutorial to combine multiple worksheets to a master sheet. It will also work when column positions don’t match.

function combineData() {
  
  var masterSheet = "Master";
  
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(masterSheet);
  var lc = ss.getLastColumn();
  var lr = ss.getLastRow() > 1 ? ss.getLastRow() : 2;
  
  ss.getRange(2, 1, lr-1, lc).clearContent();
  
  var labels = ss.getRange(1, 1, 1, lc).getValues()[0];
  
  
  labels.forEach(function(label,i){
    var colValues = getCombinedColumnValues(label,masterSheet);
    ss.getRange(2, i+1, colValues.length, 1).setValues(colValues);
    
  })
  
}


function getCombinedColumnValues(label,masterSheetName) {
  
  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  
  var colValues = [];
  
  for([i,sheet] in sheets){
    var sheetName = sheet.getSheetName();
    if(sheetName !== masterSheetName) {
      var tempValues = getColumnValues(label,sheetName);
      colValues = colValues.concat(tempValues);
    }
  }
  
  return colValues;
}


function getColumnValues(label,sheetName) {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  var colIndex = getColumnIndex(label,sheetName);
  var numRows = ss.getLastRow() - 1;
  var colValues = ss.getRange(2, colIndex, numRows, 1).getValues();
  return colValues;
}


function getColumnIndex(label,sheetName) {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  
  var lc = ss.getLastColumn();
  var lookupRangeValues = ss.getRange(1, 1, 1, lc).getValues()[0];
  
  var index = lookupRangeValues.indexOf(label) + 1;
  
  return index;
  
}

Understand How This Script Works

Google Sheets Get Distance & Time – GOOGLEMAPS Function

Add the following code to your Apps Script Editor first and then use the function.

/**
* Get Distance between 2 different addresses.
* @param start_address Address as string Ex. "300 N LaSalles St, Chicago, IL"
* @param end_address Address as string Ex. "900 N LaSalles St, Chicago, IL"
* @param return_type Return type as string Ex. "miles" or "kilometers" or "minutes" or "hours"
* @customfunction
*/

function GOOGLEMAPS(start_address,end_address,return_type) {

  // https://www.chicagocomputerclasses.com/
  // Nov 2017
  // improvements needed
  
  var mapObj = Maps.newDirectionFinder();
  mapObj.setOrigin(start_address);
  mapObj.setDestination(end_address);
  var directions = mapObj.getDirections();
  
  var getTheLeg = directions["routes"][0]["legs"][0];
  
  var meters = getTheLeg["distance"]["value"];
  
  switch(return_type){
    case "miles":
      return meters * 0.000621371;
      break;
    case "minutes":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to minutes and return
        return duration / 60;
      break;
    case "hours":
        // get duration in seconds
        var duration = getTheLeg["duration"]["value"];
        //convert to hours and return
        return duration / 60 / 60;
      break;      
    case "kilometers":
      return meters / 1000;
      break;
    default:
      return "Error: Wrong Unit Type";
   }
  
}

How to Use GOOGLEMAPS Function?

Understand How This Script Works

Google Sheets Import JSON – IMPORTJSON Function

Add the following code to your Apps Script Editor first and then use the function.

/**
* Imports JSON data to your spreadsheet Ex: IMPORTJSON("http://myapisite.com","city/population")
* @param url URL of your JSON data as string
* @param xpath simplified xpath as string
* @customfunction
*/
function IMPORTJSON(url,xpath){
  
  try{
    // /rates/EUR
    var res = UrlFetchApp.fetch(url);
    var content = res.getContentText();
    var json = JSON.parse(content);
    
    var patharray = xpath.split("/");
    //Logger.log(patharray);
    
    for(var i=0;i<patharray.length;i++){
      json = json[patharray[i]];
    }
    
    //Logger.log(typeof(json));
    
    if(typeof(json) === "undefined"){
      return "Node Not Available";
    } else if(typeof(json) === "object"){
      var tempArr = [];
      
      for(var obj in json){
        tempArr.push([obj,json[obj]]);
      }
      return tempArr;
    } else if(typeof(json) !== "object") {
      return json;
    }
  }
  catch(err){
      return "Error getting data";  
  }
  
}

How to Use IMPORTJSON Function?

Understand How This Script Works

Ultimate Google Sheets VLOOKUP Tutorial Series

The ultimate series covering everything you need to know about Google Sheets VLOOKUP function. Most of it applies to Microsoft Excel as well. Make sure you watch the whole series if you want in-depth understanding of VLOOKUP function and everything related. Other functions included: INDEX, MATCH, IFERROR, UNIQUE, QUERY & more.