📌  相关文章
📜  switch case google script - Go 编程语言 - Go 编程语言(1)

📅  最后修改于: 2023-12-03 15:20:25.629000             🧑  作者: Mango

switch case in Google Apps Script

Switch case is a control statement used in programming to perform different actions based on different conditions. In Google Apps Script, which is based on JavaScript, switch case can be used to execute different code blocks depending on the value of a variable or an expression.

Syntax

The syntax for a switch case statement in Google Apps Script is as follows:

switch (expression) {
  case value1:
    // code to be executed if expression matches value1
    break;
  case value2:
    // code to be executed if expression matches value2
    break;
  case value3:
    // code to be executed if expression matches value3
    break;
  default:
    // code to be executed if expression doesn't match any cases
}
  • The expression is evaluated and compared against the values in each case.
  • If a case matches the value of the expression, the code block associated with that case is executed.
  • The break statement is used to exit the switch case block after a case is matched and executed. Without the break, execution would continue to the next case even if it doesn't match.
  • The default case is optional and is executed when none of the cases match the expression.
Example

Here is an example that demonstrates the usage of switch case in Google Apps Script:

function myFunction() {
  var fruit = "banana";

  switch (fruit) {
    case "apple":
      Logger.log("Selected fruit is apple");
      break;
    case "banana":
      Logger.log("Selected fruit is banana");
      break;
    case "orange":
      Logger.log("Selected fruit is orange");
      break;
    default:
      Logger.log("Selected fruit is not apple, banana, or orange");
  }
}

When you run the above function (myFunction), it will log the message "Selected fruit is banana" since the value of fruit matches the case "banana".

Remember to replace Logger.log() with appropriate code that fits your use case.

Switch case is a handy control statement in Google Apps Script, allowing you to organize your code and perform different actions based on specific conditions.

Note: This response is returned in Markdown format.