Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9831

How can I speed up this script in google sheets?

$
0
0

I am wondering if there is a more efficient way to speeding up this script?

var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Trends");
var wsOptions = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Options");
var options = wsOptions.getRange(2, 1, wsOptions.getLastRow()-1, 2).getValues();

function onEdit(e){
 var activeCell = e.range;
 var val = activeCell.getValue();
 var r = activeCell.getRow();
 var c = activeCell.getColumn();
 var wsName = activeCell.getSheet().getName();
 if(wsName == "Trends"&& c === 5 && r > 2){

   if(val === ""){
     ws.getRange(r, 6).clearContent();
     ws.getRange(r, 6).clearDataValidations();
   } else {
     ws.getRange(r, 6).clearContent();
     var filteredOptions= options.filter(function(o){ return o[0] === val });
     var listToApply = filteredOptions.map(function(o){ return o[1] });
     var cell = ws.getRange(r, 6);
     applyValidationToCell(listToApply,cell);
   }   
  }
 }

function applyValidationToCell(list,cell){

 var rule = SpreadsheetApp
 .newDataValidation()
 .requireValueInList(list)
 .setAllowInvalid(false)
 .build();

 cell.setDataValidation(rule);
}

Viewing all articles
Browse latest Browse all 9831

Latest Images

Trending Articles



Latest Images