根据另一个变量在 Google Scripts 中设置变量

时间:2021-01-05 13:24:38

标签: javascript google-apps-script google-sheets

我是 Javascript 的新手,并且使用谷歌的 Apps Scripts 来练习,首先,如何根据 if 语句将全局变量的值分配给另一个变量。下面代码的每次测试都会给我最后一个值。

var oneweek = 8640, onefort = 4696, onemonth = 2628, threeweek = 7728, threefort = 4290, threemont = 2401;

var totalprice = 0;

switch (true) { 
    case (contract_len == "1 Year" && time_len == "Weekly"):
        if (day_options == "Full Day") {
          var totalprice = oneweek;
    }
        else {
          var totalprice = oneweek / 2;
        break;
    }
    
    case (contract_len == "1 Year" && time_len == "Fortnightly"):
        if (day_options == "Full Day") {
          var totalprice = onefort;
    }
        else {
          var totalprice = onefort / 2;
        break;
    }
  
    case (contract_len == "1 Year" && time_len == "Monthly"):
        if (day_options == "Full Day") {
          var totalprice = onemonth;
    }
        else {
          var totalprice = onemonth / 2;
        break;
    }
    case (contract_len == "3 Years" && time_len == "Weekly"):
        if (day_options == "Full Day") {
          var totalprice = threeweek;
    }
        else {
          var totalprice = threeweek / 2;
        break;
    }
 
    case (contract_len == "3 Years" && time_len == "Fortnightly"):
        if (day_options == "Full Day") {
          var totalprice = threefort;
    }
        else {
          var totalprice = threefort / 2;
        break;
    }
  
    case (contract_len == "3 Years" && time_len == "Monthly"):
    if (day_options == "Full Day") {
        var totalprice = threemont;
    }
    else {
        var totalprice = threemont / 2;
        break;
    }
  }

有没有办法简化这个?我正在使用提交表单触发器。

1 个答案:

答案 0 :(得分:3)

问题:

您代码中的问题是当第一个 break 条件成立时您没有 ifing。

例如:

 switch (true) { 
    case (contract_len == "1 Year" && time_len == "Weekly"):
        if (day_options == "Full Day") {
          var totalprice = oneweek;
    }
        else {
          var totalprice = oneweek / 2;
        break;
    }
    
    case (contract_len == "1 Year" && time_len == "Fortnightly"):
        if (day_options == "Full Day") {
          var totalprice = onefort;
    }
        else {
          var totalprice = onefort / 2;
        break;
    }

如果第一个 case 语句是 true,第一个 if 语句也是如此,则 totalprice 的值将等于 oneweek。问题是您忘记了 break。因此下一个 case 块将自动变为 true,因此 totalprice 将变为 onefort(再次假设 day_options="Full Day")。

这就解释了为什么你每次都得到最后一个值。

代码中的快速修复方法是将 break 语句放在 every if 语句的 case 块之后。

解决方案:

function myFunction() {
  
  var oneweek = 8640, onefort = 4696, onemonth = 2628, threeweek = 7728, threefort = 4290, threemont = 2401;
  contract_len = "1 Year"
  time_len = "Weekly"
  day_options = "Full Day"
  
  var totalprice = 0;

switch (true) { 
    case (contract_len == "1 Year" && time_len == "Weekly"):
        if (day_options == "Full Day") {
          var totalprice = oneweek;       
    }
        else {
          
          var totalprice = oneweek / 2;
    }
    break;
    
    case (contract_len == "1 Year" && time_len == "Fortnightly"):
        if (day_options == "Full Day") {
          var totalprice = onefort;
    }
        else {
          var totalprice = onefort / 2;        
    }
    break;
  
    case (contract_len == "1 Year" && time_len == "Monthly"):
        if (day_options == "Full Day") {
          var totalprice = onemonth;
    }
        else {
          var totalprice = onemonth / 2;
    }
        break;
    case (contract_len == "3 Years" && time_len == "Weekly"):
        if (day_options == "Full Day") {
          var totalprice = threeweek;
    }
        else {
          var totalprice = threeweek / 2;
    }
       break;
 
    case (contract_len == "3 Years" && time_len == "Fortnightly"):
        if (day_options == "Full Day") {
          var totalprice = threefort;
    }
        else {
          var totalprice = threefort / 2;
        
    }
    break;
  
    case (contract_len == "3 Years" && time_len == "Monthly"):
    if (day_options == "Full Day") {
        var totalprice = threemont;
    }
    else {
        var totalprice = threemont / 2;
        
    }
    break;
  }
  
}

如果您想改进您的代码,这不是提出此要求的正确平台。相反,我建议您在 code review 中发布问题。

但是,如果您想缩短代码,可以使用 ternary operators