简体   繁体   中英

Run Google Sheets script on several certain tabs

I have a working script, which creates depending drop-down lists on one tab named "One". Now I need it to work for another similar tab named "middle" and third tab named "mix". The structure of these tabs is similar to each other. How can I manage it with the following script?

var mainWsName = "One";                             
var optionsWsName = "STOK";                             
var firstLevelColumn = 5;                               
var secondLevelColumn = 6;                              
var thirdLevelColumn = 7;                               

var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(mainWsName);                              
var wsOptions = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(optionsWsName);                                

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 > 4){                               
applyFirstLevelValidation(val,r);                               
} else if(wsName === mainWsName && c === secondLevelColumn && r > 4){                               
applySecondLevelValidation(val,r);                              
}                               
} //end onEdit                              

function applyFirstLevelValidation(val,r){                              
if(val === ""){                             
ws.getRange(r, secondLevelColumn).clearContent();                               
ws.getRange(r, secondLevelColumn).clearDataValidations();                               
ws.getRange(r, thirdLevelColumn).clearContent();                                
ws.getRange(r, thirdLevelColumn).clearDataValidations();                                
} else {                                
ws.getRange(r, secondLevelColumn).clearContent();                               
ws.getRange(r, secondLevelColumn).clearDataValidations();                               
ws.getRange(r, thirdLevelColumn).clearContent();                                
ws.getRange(r, thirdLevelColumn).clearDataValidations();                                
var filteredOptions = wsOptions.getDataRange().getValues().filter(function(o){ return o[0] === 
val });  // <--- Modified                               
var listToApply = filteredOptions.map(function(o){ return o[1] });                              
var cell = ws.getRange(r, secondLevelColumn);                               
applyValidationToCell(listToApply,cell);                                
}                               
}                               

function applySecondLevelValidation(val,r){                             
if(val === ""){                             
ws.getRange(r, thirdLevelColumn).clearContent();                                
ws.getRange(r, thirdLevelColumn).clearDataValidations();                                
} else {                                
ws.getRange(r, thirdLevelColumn).clearContent();                                
var firstLevelColValue = ws.getRange(r, firstLevelColumn).getValue();                               
var filteredOptions = wsOptions.getDataRange().getValues().filter(function(o){ return o[0] === 
firstLevelColValue && o[1] === val });  // <--- Modified                                
var listToApply = filteredOptions.map(function(o){ return o[2] });                              
var cell = ws.getRange(r, thirdLevelColumn);                                
applyValidationToCell(listToApply,cell);                                
}                               
}                               

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

cell.setDataValidation(rule)                                
}   

Your line

if(wsName === mainWsName && c === firstLevelColumn && r > 4){   

includes an if statement that runs the funciton applyFirstLevelValidation(val,r) only under the condition that the edited sheet name is equal to mainWsName , whereby

var mainWsName = "One";

Now, if you want to run the function for more then one sheet:

  1. You need to create additional variables for all sheet names of interest and compare the edited sheet name, against all of those variables with an || operator.

  2. You need to pass the edited sheet name wsName to the function applyFirstLevelValidation to make sure that the code is applied to the correct sheet.

Sample:


var mainWsName1 = "One";   
var mainWsName2 = "Two";                          
var optionsWsName = "STOK";                             
var firstLevelColumn = 5;                               
var secondLevelColumn = 6;                              
var thirdLevelColumn = 7; 

var wsOptions = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(optionsWsName);                                

function onEdit(e){                             
  var activeCell = e.range;                               
  var val = activeCell.getValue();                                
  var r = activeCell.getRow();                                
  var c = activeCell.getColumn();                             
  var wsName = activeCell.getSheet().getName();
  // compare the edited sheet name against multiple allowed sheet anmes with ||                               
  if(wsName == mainWsName1 || wsName == mainWsName2 && r > 4){
    if(c == firstLevelColumn){                               
      applyFirstLevelValidation(val,r, wsName);                               
    } else if(c == secondLevelColumn){                               
      applySecondLevelValidation(val,r, wsName);                              
    }  
  }                             
} 

function applyFirstLevelValidation(val,r,wsName){
  //  obtain the sheet name dynamically from the onEdit() function  
  var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wsName);                            
  if(val == ""){                             
    ws.getRange(r, secondLevelColumn).clearContent();                               
    ws.getRange(r, secondLevelColumn).clearDataValidations();                               
    ws.getRange(r, thirdLevelColumn).clearContent();                                
    ws.getRange(r, thirdLevelColumn).clearDataValidations();                                
  } else {                                
    ws.getRange(r, secondLevelColumn).clearContent();                               
    ws.getRange(r, secondLevelColumn).clearDataValidations();                               
    ws.getRange(r, thirdLevelColumn).clearContent();                                
    ws.getRange(r, thirdLevelColumn).clearDataValidations();                                
    var filteredOptions = wsOptions.getDataRange().getValues().filter(function(o){ return o[0] == 
      val });  // <--- Modified                               
    var listToApply = filteredOptions.map(function(o){ return o[1] });                              
    var cell = ws.getRange(r, secondLevelColumn);                               
    applyValidationToCell(listToApply,cell);                                
  }                               
}                               

function applySecondLevelValidation(val,r, wsName){   

  //change in the same way as function applyFirstLevelValidation
}

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