有什么方法可以更有效地编写这些if语句?

时间:2019-05-13 01:17:34

标签: if-statement google-apps-script google-sheets

我有一个电子表格,其中有大量工作表,详细说明了填写工作表的人可以拥有的每个“工作”的信息。为了解决这个问题,我编写了一个脚本来隐藏或显示工作表,具体取决于他们在工作表首页上选择的作业-他们最多可以选择3个。

脚本...有效,但是我收到错误消息,说它试图一次做太多而失败。我并不是一个优秀的程序员,所以到目前为止,如何清理它还远远超出了我。我并不是在寻找最有效的方法,而是在寻找一种容易使用的方法。

我用谷歌搜索了几次,但是我看到的许多解决方案似乎都不完全符合我的工作,并且涉及数组和字典之类的东西?

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet1 = ss.getSheetByName("Character Sheet");
  var sheet2 = ss.getSheetByName("Marauder Abilities");
  var sheet3 = ss.getSheetByName("Warrior Abilities");
  var sheet4 = ss.getSheetByName("Dark Knight");
  var sheet5 = ss.getSheetByName("Gladiator");
  var sheet6 = ss.getSheetByName("Paladin");
  var sheet7 = ss.getSheetByName("Conjurer");
  var sheet8 = ss.getSheetByName("White Mage");
  var sheet9 = ss.getSheetByName("Arcanist");
  var sheet10 = ss.getSheetByName("Scholar");
  var sheet11 = ss.getSheetByName("Astrologian");
  var sheet12 = ss.getSheetByName("Pugilist");
  var sheet13 = ss.getSheetByName("Monk");
  var sheet14 = ss.getSheetByName("Lancer");
  var sheet15 = ss.getSheetByName("Dragoon");
  var sheet16 = ss.getSheetByName("Rogue");
  var sheet17 = ss.getSheetByName("Ninja");
  var sheet18 = ss.getSheetByName("Samurai");
  var sheet19 = ss.getSheetByName("Archer");
  var sheet20 = ss.getSheetByName("Bard");
  var sheet21 = ss.getSheetByName("Machinist");
  var sheet22 = ss.getSheetByName("Summoner");
  var sheet23 = ss.getSheetByName("Thaumaturge");
  var sheet24 = ss.getSheetByName("Black Mage");
  var sheet25 = ss.getSheetByName("Red Mage");
  var sheet26 = ss.getSheetByName("Garlean Pureblood");

  var cell1 = sheet1.getRange('B5');
  var cell2 = sheet1.getRange('C5');
  var cell3 = sheet1.getRange('D5');

  if (cell1.getValue() != "Marauder"||cell2.getValue() != "Marauder"||cell3.getValue() != "Marauder") {
    sheet2.hideSheet();
  }

  if (cell1.getValue() == "Marauder"||cell2.getValue() == "Marauder"||cell3.getValue() == "Marauder") {
    sheet2.showSheet();
  }

  if (cell1.getValue() != "Warrior"||cell2.getValue() != "Warrior"||cell3.getValue() != "Warrior") {
    sheet3.hideSheet();
  }

  if (cell1.getValue() == "Warrior"||cell2.getValue() == "Warrior"||cell3.getValue() == "Warrior") {
    sheet3.showSheet();
  }

  if (cell1.getValue() != "Dark Knight"||cell2.getValue() != "Dark Knight"||cell3.getValue() != "Dark Knight") {
    sheet4.hideSheet();
  }

  if (cell1.getValue() == "Dark Knight"||cell2.getValue() == "Dark Knight"||cell3.getValue() == "Dark Knight") {
    sheet4.showSheet();
  }

从那里开始所有26张纸。

有没有更简单的方法可以写出这么大的东西?

我可以做

if (cell1.getValue() == "Marauder"||cell2.getValue() == "Marauder"||cell3.getValue() == "Marauder") 
{
    sheet2.showSheet();
}
else
{
    sheet2.hideSheet();
}

1 个答案:

答案 0 :(得分:0)

我认为是这样做的:

function myFunction() {
  var sA=["Marauder Abilities","Warrior Abilities","Dark Knight","Gladiator","Paladin","Conjurer","White Mage","Arcanist","Scholar","Astrologian","Pugilist","Monk","Lancer","Dragoon","Rogue","Ninja","Samurai","Archer","Bard","Machinist","Summoner","Thaumaturge","Black Mage","Red Mage","Garlean Pureblood"];
  var cA=["Marauder","Warrior","Dark Knight","Gladiator","Paladin","Conjurer","White Mage","Arcanist","Scholar","Astrologian","Pugilist","Monk","Lancer","Dragoon","Rogue","Ninja","Samurai","Archer","Bard","Machinist","Summoner","Thaumaturge","Black Mage","Red Mage","Garlean Pureblood"];
  var ss=SpreadsheetApp.getActive();
  var sheet1=ss.getSheetByName("Character Sheet");
  var cells=sheet1.getRange('B5:D5').getValues()[0];
  var shts=ss.getSheets();
  for(var i=0;i<shts.length;i++) {
    var index=sA.indexOf(shts[i].getName());
    if(index>-1) {
      if(cells[0]!=cA[index] || cells[1]!=cA[index] || cells[2]!=cA[index]) {
        shts[i].hideSheet();
      }
      if(cells[0]==cA[index] || cells[1]==cA[index] || cells[2]==cA[index]) {
        shts[i].showSheet()
      }
    }
  }
}