如何通过数据验证隐藏基于单元格值的Google隐藏工作表列?

时间:2019-05-05 21:27:30

标签: javascript switch-statement hide show

我是Google脚本的新手,但我想编写一些脚本来改善我的Google表格体验。脚本运行得很好-尽管相对较慢-直到电子表格中充满了公式和制表符,此后onEdit函数通常在35秒后停止执行,因此不执行所有取消隐藏和隐藏列。我相信还有一种更好的方式来编写代码,它也更快。我想要实现的是,基于下拉菜单单元格的更改值,某些列的集合将被隐藏而其他可见。哪个取决于下拉单元格的选择。

任何改进代码并使其更快的提示将不胜感激。

提前谢谢

var VCO = [1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1];
  var VW1=[0,1,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0,1,0,1,1];
  var VW2=[0,0,1,0,0,0,0,0,0,0,1,1,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,1,1,0,1,1];
  var VW3=[0,0,0,1,0,0,0,0,0,0,1,1,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,1,1,0,1,1];
  var VW4=[0,0,0,0,1,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,1,1,0,1,1];
  var VP1=[0,0,0,0,0,1,0,0,0,0,1,1,0,1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,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,1,1,0,1,1];
  var VW9=[0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,1,1,0,1,1];
  var V10=[0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,1,1,0,1,1];
  var V11=[0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,1,1,0,1,1];
  var V12=[0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0,1,1,1,0,0,0,0,1,1,0,1,1];
  var VP3=[0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,1,1,1,0,0,0,0,1,1,0,1,1];
  var VA3=[0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1];


function onEdit(e) {

  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();

  if (e.range.getA1Notation() === "BX2") {

    var vl=sh.getRange("BX2").getValue();
    var j=11;

    switch(vl)
    {
    case "COMM":
      for(var i=0;i<67;i++){
        if(VCO[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;    
    case "W1":
      for(var i=0;i<67;i++){
        if(VW1[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "W2":
      for(var i=0;i<67;i++){
        if(VW2[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "W3":
      for(var i=0;i<67;i++){
        if(VW3[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "W4":
      for(var i=0;i<67;i++){
        if(VW4[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;    
    case "PRE JAN":
      for(var i=0;i<67;i++){
        if(VP1[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break; 
    case "ACT JAN":
      for(var i=0;i<67;i++){
        if(VA1[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;  
    case "W5":
      for(var i=0;i<67;i++){
        if(VW5[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;  
    case "W6":
      for(var i=0;i<67;i++){
        if(VW6[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "W7":
      for(var i=0;i<67;i++){
        if(VW7[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "W8":
      for(var i=0;i<67;i++){
        if(VW8[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "PRE FEB":
      for(var i=0;i<67;i++){
        if(VP2[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "ACT FEB":
      for(var i=0;i<67;i++){
        if(VA2[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "W9":
      for(var i=0;i<67;i++){
        if(VW9[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "W10":
      for(var i=0;i<67;i++){
        if(V10[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "W11":
      for(var i=0;i<67;i++){
        if(V11[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "W12":
      for(var i=0;i<67;i++){
        if(V12[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "PRE MAR":
      for(var i=0;i<67;i++){
        if(VP3[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    case "ACT MAR":
      for(var i=0;i<67;i++){
        if(VA3[i]==1){
          sh.showColumns(i+j);
        }
        else{
        sh.hideColumns(i+j);
        }
      }
      break;
    }
  }

0 个答案:

没有答案