Google应用脚本-为什么我的onEdit()函数花费这么长时间?

时间:2019-04-16 08:00:41

标签: google-apps-script drop-down-menu range

好,所以我需要选择一个大约350行和4列的范围,从中获取值,并在下拉菜单中的每一次更改中搜索并比较所选范围中的行和列,然后找到匹配以过滤掉下拉列表中的dataValidation。但是,每次下拉更改功能大约需要5-6秒才能完成。

我什至尝试只选择较小的范围,它仍然使用相同的时间。在代码示例中,我的范围大约为20行,但仍然需要5-6秒才能完成。

function onEdit() {

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("EAC Input");
  var dataSS = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Resource names");

  var activeCell = ss.getActiveCell();
  var column = activeCell.getColumn();
  var lastRow = dataSS.getLastRow();

  if(column == 1 || column == 2 || column == 3 || column == 4 && activeCell.getRow() > 5) {

    var makeValue = activeCell.getValue(); 

    if(makeValue == "" || makeValue == "WORKSTREAM:" || !isNaN(parseFloat(makeValue)) && isFinite(makeValue))
    {
      return;
    }

    var validationRange =  dataSS.getRange("A36:D55").getValues();

    var filteredRangeOfDepartments = [];
    var filteredRangeOfPositions = [];
    var filteredRangeOfEmployees = [];

    for(var i = 0; i < validationRange.length; i++)
    {      
      var rangeAtZero = validationRange[i][0];
      var rangeAtOne = validationRange[i][1];
      var rangeAtTwo = validationRange[i][2];

      if(rangeAtZero== makeValue)
      {
        filteredRangeOfDepartments.push(rangeAtOne);         
      }      
      if(rangeAtOne == makeValue && rangeAtZero == activeCell.offset(0, -1).getValue())
      {
         filteredRangeOfPositions.push(rangeAtTwo);        
      }
      if(rangeAtTwo == makeValue && rangeAtZero == activeCell.offset(0, -2).getValue() && rangeAtOne == activeCell.offset(0, -1).getValue())
      {
         filteredRangeOfEmployees.push(validationRange[i][3]);        
      }
    }

    if(column == 1)
    {
    var validationRule = SpreadsheetApp.newDataValidation().requireValueInList(filteredRangeOfDepartments, true).build();
        activeCell.offset(0, 1).setDataValidation(validationRule);
    }
    if(column == 2)
    {
    var validationRule = SpreadsheetApp.newDataValidation().requireValueInList(filteredRangeOfPositions, true).build();
    activeCell.offset(0, 1).setDataValidation(validationRule);
    }
    if(column == 3)
    {
      filteredRangeOfEmployees.push("TBC");
    var validationRule = SpreadsheetApp.newDataValidation().requireValueInList(filteredRangeOfEmployees, true).build();`enter code here`
    activeCell.offset(0, 1).setDataValidation(validationRule);
    }  
}
}

0 个答案:

没有答案