简体   繁体   English

如何在Google Apps脚本中编写if / and语句

[英]How to write an if/and statement in google apps script

I am trying to write an if/and statement based on two different drop down lists. 我试图基于两个不同的下拉列表编写if / and语句。 Is it possible to write something with the logic below? 可以使用下面的逻辑写点东西吗? The "if &&" statement does not work properly, but will work just fine with a single "if" statement. “ if &&”语句不能正常工作,但是仅使用一个“ if”语句就可以正常工作。 I understand the is a bit of code, but this is the only way I can show exactly what is happening. 我知道这是一些代码,但这是我可以确切显示正在发生的事情的唯一方法。

    function doGet(e) {
  var app = UiApp.createApplication();

  var containerSizeList = app.createListBox().setId('containerSizeList').setName('containerSizeList');
  containerSizeList.addItem("20ft Long");
  containerSizeList.addItem("40ft Long");

  var loadingStyleList = app.createListBox().setId('loadingStyleList').setName('loadingStyleList');
  loadingStyleList.addItem("On Pallets");
  loadingStyleList.addItem("Floor Loaded");

    var tenPalletPanel = app.createHorizontalPanel().setId('tenPalletPanel').setVisible(true);
    var tenPalletLabel = app.createLabel('10 Pallets Available').setId('tenPalletLabel');
    var twentyPalletPanel = app.createHorizontalPanel().setId('twentyPalletPanel').setVisible(false);
    var twentyPalletLabel = app.createLabel('20 Pallets Available').setId('twentyPalletLabel');
    var loadingNotePanel = app.createHorizontalPanel().setId('loadingNotePanel').setVisible(false);
    var loadingNoteLabel = app.createLabel('Note: Only certain products may be floor loaded')
    .setId('loadingNoteLabel');

    var containerGrid = app.createGrid(1, 2);
    containerGrid.setWidget(0, 0, containerSizeList);
    containerGrid.setWidget(0, 1, loadingStyleList);

    var handlerJ = app.createServerClickHandler('palletChangeMe'); 
    containerSizeList.addChangeHandler(handlerJ);
    loadingStyleList.addChangeHandler(handlerJ);
    handlerJ.addCallbackElement(containerGrid);       

  app.add(containerGrid);
  app.add(tenPalletPanel);
  tenPalletPanel.add(tenPalletLabel);
  app.add(twentyPalletPanel);
  twentyPalletPanel.add(twentyPalletLabel);
  app.add(loadingNotePanel);
  loadingNotePanel.add(loadingNoteLabel);

  return app;
}

function palletChangeMe(e){
  var app = UiApp.getActiveApplication();

  if (e.parameter.containerSizeList == "40ft Long" && e.parameter.loadingStyleList == "On Pallets"){
    app.getElementById('tenPalletPanel').setVisible(false);
    app.getElementById('twentyPalletPanel').setVisible(true);
    app.getElementById('loadingNotePanel').setVisible(false);
}

  return app;
}

Instead of: 代替:

if (e.parameter.containerSizeList == "20ft Long"){
  and if (e.parameter.loadingStyleList == "On Pallets"){

Should be: 应该:

if (e.parameter.containerSizeList == "20ft Long" && e.parameter.loadingStyleList == "On Pallets"){

Note: && means logical AND. 注意: &&表示逻辑AND。 Read more about logical operators at MDN. 在MDN上了解有关逻辑运算符的更多信息。

and you need to remove last } in each if statement, so final code would be like: 你需要最后去除} if语句中的每个,所以最终代码会是这样:

if (e.parameter.containerSizeList == "20ft Long" && e.parameter.loadingStyleList == "On Pallets"){
    app.getElementById('tenPalletPanel').setVisible(true);
    app.getElementById('twentyPalletPanel').setVisible(false);
}

if (e.parameter.containerSizeList == "40ft Long" && e.parameter.loadingStyleList == "On Pallets"){
    app.getElementById('tenPalletPanel').setVisible(false);
    app.getElementById('twentyPalletPanel').setVisible(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM