如何在谷歌应用程序脚本中编写if /和声明 [英] How to write an if/and statement in google apps script

查看:119
本文介绍了如何在谷歌应用程序脚本中编写if /和声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据两个不同的下拉列表编写一个if /和statement。是否可以用下面的逻辑来写一些东西? 如果&&声明无法正常工作,但只需使用单个if语句即可正常工作。我知道这是一些代码,但这是我能够准确显示发生了什么的唯一方式。

 函数doGet( e){
var app = UiApp.createApplication();
$ b var containerSizeList = app.createListBox()。setId('containerSizeList')。setName('containerSizeList');
containerSizeList.addItem(20ft Long);
containerSizeList.addItem(40ft Long);
$ b var loadStyleList = app.createListBox()。setId('loadingStyleList')。setName('loadingStyleList');
loadingStyleList.addItem(On Pallets);
loadingStyleList.addItem(Floor Loaded);

var tenPalletPanel = app.createHorizo​​ntalPanel()。setId('tenPalletPanel')。setVisible(true);
var tenPalletLabel = app.createLabel('10 Pallets Available')。setId('tenPalletLabel');
var twentyPalletPanel = app.createHorizo​​ntalPanel()。setId('twentyPalletPanel')。setVisible(false);
var twentyPalletLabel = app.createLabel('20 Pallets Available')。setId('twentyPalletLabel');
var loadingNotePanel = app.createHorizo​​ntalPanel()。setId('loadingNotePanel')。setVisible(false);
var loadingNoteLabel = app.createLabel('注意:只有某些产品可能是平面加载的)
.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);

返回应用;
}

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

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

返回应用;


解决方案

>

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

应该是:

  if(e.parameter.containerSizeList ==20ft Long&& e.parameter.loadingStyleList ==On Pallets){
&&
表示逻辑与。 了解更多关于MDN的逻辑运算符。



,你需要删除每个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(假)。
app.getElementById('twentyPalletPanel')。setVisible(true);
}


I am trying to write an if/and statement based on two different drop down lists. 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. 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. Read more about logical operators at MDN.

and you need to remove last } in each if statement, so final code would be like:

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);
}

这篇关于如何在谷歌应用程序脚本中编写if /和声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆