简体   繁体   中英

Google Sheet Apps Script Exceeding maximum time

I have a dynamic dropdown apps script running on only 1 cell and there are some other formulas running in my sheet. Until 2 days before i could run the script onEdit but i cant seem to do so now. When checked inside apps script execution page it shows "Exceeded maximum execution time". What can i do to resolve this?


var mainWsName = "Print Sheet";
var mainDataName = "Orders";
var FirstLevelColumn = 1;
var SecondLevelColumn = 2;
var ThirdLevelColumn = 3;
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(mainWsName);
var wsJO = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(mainDataName);
var JO = wsJO.getRange(2,1,wsJO.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 === mainWsName && c === FirstLevelColumn && r > 1){
     applyFirstLevelValidation(val,r); 
  }else if(wsName === mainWsName && c=== SecondLevelColumn && r > 1){
    applysecondLevelValidation(val,r);
  }
} //end onEdit

function applyFirstLevelValidation(val,r){ 

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


function applyValidationToCell(list,cell){
  
             
  var rule=SpreadsheetApp.newDataValidation().requireValueInList(list).setAllowInvalid(false).build();        
  
   cell.setDataValidation(rule);

}

Tried executing app script to get dynamic dropdown based on my selection.

The code you quote is suboptimal, but chances are that the bad performance you mention is caused by the spreadsheet rather than the script. To improve spreadsheet performance, see these optimization tips .

You may also want to take a look at the dependentDropDownLists_ script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM