简体   繁体   中英

How to apply Google Apps Script to multiple sheets

I want to apply the following code to multiple tabs in the same Google Sheet. Defining different Tabs in the first var, or applying multiple .gs files in the same project each with a different var did not succeed.

var naamWerkbladKasboek = "Kasboek"; 
var naamWerkbladOpties = "Opties";
var eersteKolomMetInput = 2; 
var tweedeKolomMetInput = 3; 
var derdeKolomMetInput = 4; 

var werkblad = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(naamWerkbladKasboek);
var werkbladOpties = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(naamWerkbladOpties);
var Opties = werkbladOpties.getRange(2, 1, werkbladOpties.getLastRow()-1, 3).getValues();


function onEdit(e){
  var activeCell = e.range;
  var val = activeCell.getValue();
  var row = activeCell.getRow();
  var column = activeCell.getColumn();
  var werkbladName = activeCell.getSheet().getName();
  
  if(werkbladName === naamWerkbladKasboek && column === eersteKolomMetInput && row > 1){
    applyFirstLevelValidation(val, row);
  } else if(werkbladName === naamWerkbladKasboek && column === tweedeKolomMetInput && row > 1){ 
    applySecondLevelValidation(val, row);
  }
} 

function applyFirstLevelValidation(val, row){
  
  if(val === ""){
      werkblad.getRange(row, tweedeKolomMetInput).clearContent();
      werkblad.getRange(row, tweedeKolomMetInput).clearDataValidations();
      werkblad.getRange(row, derdeKolomMetInput).clearContent();
      werkblad.getRange(row, derdeKolomMetInput).clearDataValidations();
    } else {
      werkblad.getRange(row, tweedeKolomMetInput).clearContent();
      werkblad.getRange(row, tweedeKolomMetInput).clearDataValidations();
      werkblad.getRange(row, derdeKolomMetInput).clearContent();
      werkblad.getRange(row, derdeKolomMetInput).clearDataValidations();
      var gefilterdeOpties = Opties.filter(function(o){ return o[0] === val });
      var listToApply = gefilterdeOpties.map(function(o){ return o[1] });
      var cell = werkblad.getRange(row, tweedeKolomMetInput);
      applyValidationToCell(listToApply,cell);
    }
}


function applySecondLevelValidation(val, row){
  
  if(val === ""){
      werkblad.getRange(row, derdeKolomMetInput).clearContent();
      werkblad.getRange(row, derdeKolomMetInput).clearDataValidations();
    } else {
      werkblad.getRange(row, derdeKolomMetInput).clearContent();
      var waardeEersteKolomMetInput = werkblad.getRange(row, eersteKolomMetInput).getValue();
      var gefilterdeOpties = Opties.filter(function(o){ return o[0] === waardeEersteKolomMetInput && o[1] === val });
      var listToApply = gefilterdeOpties.map(function(o){ return o[2] });
      var cell = werkblad.getRange(row, derdeKolomMetInput);
      applyValidationToCell(listToApply,cell);
    }
}

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

}

I have tried working with the only and exclude tags as indicated here: How to run a script on multiple sheets, Google Sheets and also tried working with an array as indicated here: https://webapps.stackexchange.com/questions/115076/how-to-run-script-on-multiple-google-sheet-tabs but both to no avail. Can someone please point me in the right direction?

As an example I can show how I tried to work with the only tag

//**EDIT**
var only = [
"Kasboek1",
"Kasboek2",
"Kasboek3"
];
var naamWerkbladOpties = "Opties";
var eersteKolomMetInput = 2; 
var tweedeKolomMetInput = 3; 
var derdeKolomMetInput = 4; 

var werkblad = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(only);
var werkbladOpties = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(naamWerkbladOpties);
var Opties = werkbladOpties.getRange(2, 1, werkbladOpties.getLastRow()-1, 3).getValues();


function onEdit(e){
  var activeCell = e.range;
  var val = activeCell.getValue();
  var row = activeCell.getRow();
  var column = activeCell.getColumn();
  var werkbladName = activeCell.getSheet().getName();
  
  if(werkbladName === only && column === eersteKolomMetInput && row > 1){
    applyFirstLevelValidation(val, row);
  } else if(werkbladName === only && column === tweedeKolomMetInput && row > 1){ 
    applySecondLevelValidation(val, row);
  }
} 

function applyFirstLevelValidation(val, row){
  
  if(val === ""){
      werkblad.getRange(row, tweedeKolomMetInput).clearContent();
      werkblad.getRange(row, tweedeKolomMetInput).clearDataValidations();
      werkblad.getRange(row, derdeKolomMetInput).clearContent();
      werkblad.getRange(row, derdeKolomMetInput).clearDataValidations();
    } else {
      werkblad.getRange(row, tweedeKolomMetInput).clearContent();
      werkblad.getRange(row, tweedeKolomMetInput).clearDataValidations();
      werkblad.getRange(row, derdeKolomMetInput).clearContent();
      werkblad.getRange(row, derdeKolomMetInput).clearDataValidations();
      var gefilterdeOpties = Opties.filter(function(o){ return o[0] === val });
      var listToApply = gefilterdeOpties.map(function(o){ return o[1] });
      var cell = werkblad.getRange(row, tweedeKolomMetInput);
      applyValidationToCell(listToApply,cell);
    }
}


function applySecondLevelValidation(val, row){
  
  if(val === ""){
      werkblad.getRange(row, derdeKolomMetInput).clearContent();
      werkblad.getRange(row, derdeKolomMetInput).clearDataValidations();
    } else {
      werkblad.getRange(row, derdeKolomMetInput).clearContent();
      var waardeEersteKolomMetInput = werkblad.getRange(row, eersteKolomMetInput).getValue();
      var gefilterdeOpties = Opties.filter(function(o){ return o[0] === waardeEersteKolomMetInput && o[1] === val });
      var listToApply = gefilterdeOpties.map(function(o){ return o[2] });
      var cell = werkblad.getRange(row, derdeKolomMetInput);
      applyValidationToCell(listToApply,cell);
    }
}

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

}

in the onEdit you check if the name of the current sheet contains "Kasboek"

function onEdit(e){
  var activeCell = e.range;
  var activeSheet = activeCell.getSheet();
  
  if(activeSheet.getName().includes("Kasboek")){
    KasboekModification(activeCell);
  } else {
    //code here if changes need to be made on other sheets
  }
}

After that you have your function KasboekModification(cell){} function where you can do the magic of your current sheet.

Answer:

A simple solution would be to return out of the onEdit(e) if the sheet that has been edited is not one of the pre-defined ones to edit.

Code Sample:

To do with only :

var only = ["Sheet1Name", "Sheet2Name", "Sheet3Name"]; // add sheet names as desired

function onEdit(e) {
  if (!(only.includes(e.range.getSheet().getName())) {
    return;
  }
  // put the rest of your onEdit function here
}

or with exclude :

var exclude = ["Sheet4Name", "Sheet5Name", "Sheet6Name"]; // add sheet names as desired

function onEdit(e) {
  if (exclude.includes(e.range.getSheet().getName())) {
    return;
  }
  // put the rest of your onEdit function here
}

Example for your use case:

var only = ["Kasboek", "Kasboek2", "Kasboek3"];

function onEdit(e) {
  if (!(only.includes(e.range.getSheet().getName())) {
    return;
  }

  var activeCell = e.range;
  var val = activeCell.getValue();
  var row = activeCell.getRow();

  if (row <= 1) return;

  var column = activeCell.getColumn();
  var eersteKolomMetInput = 2; 
  var tweedeKolomMetInput = 3; 
  var derdeKolomMetInput = 4;
  
  if (column === eersteKolomMetInput) {
    applyFirstLevelValidation(val, row);
  } 
  else if (column === tweedeKolomMetInput) {
    applySecondLevelValidation(val, row);
  }
} 

function applyFirstLevelValidation(val, row) {
  // ...
}

function applySecondLevelValidation(val, row) {
  // ...
}

I hope this is helpful to you!

TypeError: Cannot read property 'range' of undefined (line 7, file "multipledatavalidation")

line 7; if (exclude.includes(e.range.getSheet().getName())) { return;

It should be noted I have another script running on my sheet as well. One that allows me to duplicate the sheet with its lock in place. I want to be able to run this code on all copies that I make of the original sheet. here is a link to a shared, editable, version of my sheet .

     var optionsWsName = "Backend-Prices";
      var wsOptions = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(optionsWsName);
      var exclude = ["BackendImportedData", "donotedit", "BackendDataSort", "09/20_DS7"]; // add sheet names as desired
    
// function to update each time the file is Edited 

function onEdit(e){
      if (exclude.includes(e.range.getSheet().getName())) {
        return;
      }
      
      var Options = wsOptions.getRange(2, 1, wsOptions.getLastRow()-1, 5).getValues();
      var activeCell = e.range;
      var val = activeCell.getValue();
      var r = activeCell.getRow();
    
      if (r <= 8) return;
    
      var c = activeCell.getColumn();
      var FirstLevelColumn = 1;
      var SecondLevelColumn = 2; 
      var ThirdLevelColumn = 3; 
      var FourthLevelColumn = 4;
      
      
      if(c === FirstLevelColumn){
        applyFirstLevelValidation(val, r);
      } else if(c === SecondLevelColumn){ 
        applySecondLevelValidation(val, r);
      } else if(c === ThirdLevelColumn){ 
        applyThirdLevelValidation(val, r);
      }
    
}//end onEdit
    
    
    
    // function for second level of data validation to work correctly
    
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();
          ws.getRange(r, FourthLevelColumn).clearContent();
          ws.getRange(r, FourthLevelColumn).clearDataValidations();
        } else {
          ws.getRange(r, SecondLevelColumn).clearContent();
          ws.getRange(r, SecondLevelColumn).clearDataValidations();
          ws.getRange(r, ThirdLevelColumn).clearContent();
          ws.getRange(r, ThirdLevelColumn).clearDataValidations();
          ws.getRange(r, FourthLevelColumn).clearContent();
          ws.getRange(r, FourthLevelColumn).clearDataValidations();
          var filteredOptions = Options.filter(function(o){ return o[0] === val });
          var listToApply = filteredOptions.map(function(o){ return o[1] });
          var cell = ws.getRange(r, SecondLevelColumn);
          applyValidationToCell(listToApply,cell);
        }

}
    
    // function for third level of data validation to work correctly
    
function applySecondLevelValidation(val, r){
      
      if(val === ""){
          ws.getRange(r, ThirdLevelColumn).clearContent();
          ws.getRange(r, ThirdLevelColumn).clearDataValidations();
          ws.getRange(r, FourthLevelColumn).clearContent();
          ws.getRange(r, FourthLevelColumn).clearDataValidations();
        } else {
          ws.getRange(r, ThirdLevelColumn).clearContent();
          ws.getRange(r, ThirdLevelColumn).clearDataValidations();
          ws.getRange(r, FourthLevelColumn).clearContent();
          ws.getRange(r, FourthLevelColumn).clearDataValidations();
          var firstlevelColValue = ws.getRange(r, FirstLevelColumn).getValue();
          var filteredOptions = Options.filter(function(o){ return o[0] === firstlevelColValue && o[1] === val });
          var listToApply = filteredOptions.map(function(o){ return o[2] });
          var cell = ws.getRange(r, ThirdLevelColumn);
          applyValidationToCell(listToApply,cell);
        }
        
}
    
    
    // function for fourth level of data validation to work correctly
    
    function applyThirdLevelValidation(val, r){
      
      if(val === ""){
          ws.getRange(r, FourthLevelColumn).clearContent();
          ws.getRange(r, FourthLevelColumn).clearDataValidations();
        } else {
          ws.getRange(r, FourthLevelColumn).clearContent();
          var firstlevelColValue = ws.getRange(r, FirstLevelColumn).getValue();
          var secondlevelColValue = ws.getRange(r, SecondLevelColumn).getValue();
          var filteredOptions = Options.filter(function(o){ return o[0] === firstlevelColValue && o[1] === secondlevelColValue && o[2] === val });
          var listToApply = filteredOptions.map(function(o){ return o[3] });
          var cell = ws.getRange(r, FourthLevelColumn);
          applyValidationToCell(listToApply,cell);
        }

}

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

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-2025 STACKOOM.COM