Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

javascript - 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)

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...