无法比较谷歌脚本中两个文本框的内容

时间:2014-12-08 00:01:21

标签: google-apps-script

我已经设置了用户界面,无法使用。

我想检查两个textboxes是否为空,然后再做一些事情。

显示涉及以下主题的脚本,并可根据需要添加整个功能。

任何人都可以帮助我吗?当前代码生成一条无法检索长度的消息。

上一篇:日志表示除{for handlerFunction

运行调试时的应用程序外,一切都未定义
function handlerFunction(eventInfo) 
  {   
   var app=UiApp.getActiveApplication();
   var parameter = eventInfo.parameter;
  var panel = parameter.panel;
  var source = parameter.source;
  var text100 = parameter.text100;
  var text101 = parameter.text101; 
  var text102 = parameter.text102;
  var text103 = parameter.text103;
  var text104 = parameter.text104;
  var text105 = parameter.text105;
  var text106 = parameter.text106;
  var stations=parameter.stations;
  var stations2=parameter.stations2;
  var systems = parameter.systems;
  var systems2 = parameter.systems2;
  var products=parameter.products;
  var products2=parameter.products2;
  var but200 = parameter.but200;
  var but201 = parameter.but201; 

 switch (source)    
   {
        case '1':
            var app=UiApp.getActiveApplication();
            app.getElementById('text100').setText(systems)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '2':

            // get the value of last changed element in listbox written to editbox
            var app=UiApp.getActiveApplication();
            app.getElementById('text101').setValue(stations)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '3':
            var app=UiApp.getActiveApplication();
            app.getElementById('text102').setValue(products)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

               case '4':
            var app=UiApp.getActiveApplication();
            app.getElementById('text10').setText(systems2)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '5':

            // get the value of last changed element in listbox written to editbox
            var app=UiApp.getActiveApplication();
            app.getElementById('text11').setValue(stations)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '6':
            var app=UiApp.getActiveApplication();
            app.getElementById('text12').setValue(products2)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;


        case '200': // SAVE button cliked
         var app=UiApp.getActiveApplication();

        // var parameter = eventInfo.parameter;
        var test1=app.getElementById('text100').setText(parameter.text.length);
        var test2=app.getElementById('text101').setText(parameter.text.length);

            if (test1 >= '1' && test2 >= '1')
            {
              app.getElementById('text103').setValue('123');

            }

               return app;
          break;     

    }

 }

3 个答案:

答案 0 :(得分:1)

问题解决了,而不是说它的优雅:)

下面的脚本代码。

感谢您的关注,见到您。

问候

     function handlerFunction(eventinfo) 
  {   
  var app=UiApp.getActiveApplication();
  var parameter =eventinfo.parameter;
  var panel = parameter.panel;
  var source = parameter.source;
  var text100 = parameter.text100;
  var text101 = parameter.text101; 
  var text102 = parameter.text102;
  var text103 = parameter.text103;
  var text104 = parameter.text104;
  var text105 = parameter.text105;
  var text106 = parameter.text106;
  var stations=parameter.stations;
  var stations2=parameter.stations2;
  var systems = parameter.systems;
  var systems2 = parameter.systems2;
  var products=parameter.products;
  var products2=parameter.products2;
  var but200 = parameter.but200;
  var but201 = parameter.but201; 


     switch(parameter.source) 
     {   
        case '1':
            var app=UiApp.getActiveApplication();
       app.getElementById('text100').setText(systems)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '2':

            // get the value of last changed element in listbox written to editbox
            var app=UiApp.getActiveApplication();
            app.getElementById('text101').setValue(stations)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '3':
            var app=UiApp.getActiveApplication();
            app.getElementById('text102').setValue(products)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

               case '4':
            var app=UiApp.getActiveApplication();
            app.getElementById('text10').setText(systems2)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '5':

            // get the value of last changed element in listbox written to editbox
            var app=UiApp.getActiveApplication();
            app.getElementById('text11').setValue(stations)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '6':
            var app=UiApp.getActiveApplication();
            app.getElementById('text12').setValue(products2)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;


        case '200': // SAVE button cliked
        var app=UiApp.getActiveApplication();
        var val1=parameter.text100.length;
        var val2=parameter.text101.length;   

        if (val1 !=0 && val2 !=0 )
            {
              app.getElementById('text103').setValue(val1);

              return app;
            }
        break;
    }

}

答案 1 :(得分:0)

尝试更改此行:

var test1=app.getElementById('text100').setText(parameter.text.length);

到此:

var test1=app.getElementById('text100').setText(eventInfo.parameter.text.length);

我刚刚添加了eventInfo

它是如何在文档中的示例中使用的:

Google Documentation - Class TextBox

答案 2 :(得分:0)

继承所有脚本。

    function doGet() {

var app = UiApp.createApplication();
  app.setStyleAttribute("background", '#aaaaaa').setStyleAttribute("color", "white");
  app.setTitle("Elite Dangerous trading");
  app.setHeight('1100');
  app.setWidth('1850');




//  drawui();

     var app=UiApp.getActiveApplication();
  //create panel 
  var panel = app.createAbsolutePanel();
  panel.setStyleAttribute("position",0);
  panel.setStyleAttribute("height",1100);
  panel.setStyleAttribute("width",1850);
  panel.setStyleAttribute('backgroundImage',"url('http://www.incgamers.com/wp-content/uploads/2014/07/elite-dangerous.jpg')");

  var handle = app.createServerHandler("handlerFunction");
  // create grid1
 var mygrid1 = app.createGrid(3, 4)
 .setStyleAttribute("background", '#5F9EF5')
 .setBorderWidth(3)
 .setCellSpacing(8)
 .setCellPadding(8);


  // create grid2
 var mygrid2 = app.createGrid(3, 4)
 .setStyleAttribute("background", '#754F0E')
 .setBorderWidth(3)
 .setCellSpacing(8)
 .setCellPadding(8);

  // create 3 listboxes with handle for save

var systems = app.createListBox().setId('1').setName('systems').setPixelSize(200,24).addChangeHandler(handle)
.setStyleAttribute("background", "blue").setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center');

var stations = app.createListBox().setId('2').setName('stations').setPixelSize(200,24).addChangeHandler(handle)
.setStyleAttribute("background", "blue").setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center');

var products = app.createListBox().setId('3').setName('products').setPixelSize(200,24).addChangeHandler(handle)
.setStyleAttribute("background", "blue").setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center');


  // create 3 listboxes with handle for show

var systems2 = app.createListBox().setId('4').setName('systems2').setPixelSize(200,24).addChangeHandler(handle)
.setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center');

var stations2 = app.createListBox().setId('5').setName('stations2').setPixelSize(200,24).addChangeHandler(handle)
.setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center');

var products2 = app.createListBox().setId('6').setName('products2').setPixelSize(200,24).addChangeHandler(handle)
.setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center');



  //create 4 read only text boxes 

var text1 = app.createTextBox()
    .setStyleAttribute("background", "blue").setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center')
    .setName('text1')
    .setId('text1')
    .setText("System input")
    .setWidth(200)    
    .setReadOnly(true);


    var text2 = app.createTextBox()
    .setStyleAttribute("background", "blue").setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center')
    .setName('text2')
    .setId('text2')
    .setText("Station input")
    .setWidth(200)    
    .setReadOnly(true);


    var text3 = app.createTextBox()
    .setStyleAttribute("background", "blue").setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center')
    .setName('text3')
    .setId('text3')
    .setText("Product input")
    .setWidth(200)    
    .setReadOnly(true);


    // price pr. item textbox
     var text4= app.createTextBox()
    .setStyleAttribute("background", "blue").setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center')
    .setName('text4')
    .setId('text4')
    .setText("Station sellingprice a piece")
    .setWidth(200)    
    .setReadOnly(true);




    //create 4 read only text boxes for textbox showfields showing

var text9 = app.createTextBox()
    .setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center')
    .setName('text9')
    .setId('text9')
    .setWidth(200)    
    .setReadOnly(true);


    var text10= app.createTextBox()
    .setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center')
    .setName('text10')
    .setId('text10')
    .setWidth(200)    
    .setReadOnly(true);


    var text11 = app.createTextBox()
    .setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center')
    .setName('text11')
    .setId('text11')
    .setWidth(200)    
    .setReadOnly(true);


    // price pr. item textbox
     var text12= app.createTextBox()
    .setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center')
    .setName('text12')
    .setId('text12')
    .setWidth(200)    
    .setReadOnly(true);


      //create 4 read only text boxes for textbox showfields showing

var text5 = app.createTextBox()
    .setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center')
    .setName('text5')
    .setId('text5')
    .setText("System")
    .setWidth(200)    
    .setReadOnly(true);


    var text6 = app.createTextBox()
    .setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center')
    .setName('text6')
    .setId('text6')
    .setText("Station")
    .setWidth(200)    
    .setReadOnly(true);


    var text7 = app.createTextBox()
    .setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center')
    .setName('text7')
    .setId('text7')
    .setText("Product")
    .setWidth(200)    
    .setReadOnly(true);


    // price pr. item textbox
     var text8= app.createTextBox()
    .setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center')
    .setName('text8')
    .setId('text8')
    .setText("Station sellingprice a piece")
    .setWidth(200)    
    .setReadOnly(true);


 // create 4 textboxes 100,101,102,103, from which to save system,station,products input

     var text100 = app.createTextBox().addChangeHandler(handle)
    .setStyleAttribute("background", "blue").setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center')
    .setName('text100')
    .setId('text100')
    .setText("")
    .setWidth(200)    
    .setReadOnly(false);

     var text101= app.createTextBox().addChangeHandler(handle)
    .setStyleAttribute("background", "blue").setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center')
    .setName('text101')
    .setId('text101')
    .setText("")
    .setWidth(200)    
    .setReadOnly(false);

     var text102 = app.createTextBox().addChangeHandler(handle)
    .setStyleAttribute("background", "blue").setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center')
    .setName('text102')
    .setId('text102')
    .setText("")
    .setWidth(200)    
    .setReadOnly(false);


     var text103 = app.createTextBox().addChangeHandler(handle)
    .setStyleAttribute("background", "blue").setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center')
    .setName('text103')
    .setId('text103')
    .setText("")
    .setWidth(200)    
    .setReadOnly(false);



 //create  submit buttons
  var inputbutton = app.createButton().addClickHandler(handle)
  .setStyleAttribute("background", '#0000FF').setStyleAttribute("color", "white").setStyleAttribute('textAlign', 'center')
  .setId('200')
  .setWidth(80)
  .setHeight(80)
  .setText("SAVE");



 //create  submit buttons
  var showbutton = app.createButton().addClickHandler(handle)
  .setStyleAttribute("background", '#4D3308').setStyleAttribute("color", "yellow").setStyleAttribute('textAlign', 'center')
  .setId('201')
  .setWidth(80)
  .setHeight(80)
  .setText("SHOW");


  // fill listboxes save

  //Open the spreadsheet and get the sheet objects
var openedSS = SpreadsheetApp.openById("15tGpfnt72_yxVcO3Kp0wFBLfYpf8nr3vZCteprtX_Ss");
var sheetList1 = openedSS.getSheetByName("data");//Spreadsheet must match with sheet name

numItemList1 = sheetList1.getLastRow()-1;//-1 is to exclude header row
  //get the item array
  list1ItemArray = sheetList1.getRange(2,1,numItemList1,1).getValues();
  //Add the items in ListBox
  for(var i=0; i<list1ItemArray.length; i++){
    products.addItem(list1ItemArray[i][0]);
    }
  i=0;


numItemList2 = sheetList1.getLastRow()-1;//-1 is to exclude header row
  //get the item array
  list2ItemArray = sheetList1.getRange(2,3,numItemList2,1).getValues();
  //Add the items in ListBox
  for(var i=0; i<list2ItemArray.length; i++){
    stations.addItem(list2ItemArray[i][0]);
    }
  i=0;


  numItemList3 = sheetList1.getLastRow()-1;//-1 is to exclude header row
  //get the item array
  list3ItemArray = sheetList1.getRange(2,5,numItemList3,1).getValues();
  //Add the items in ListBox
  for(var i=0; i<list3ItemArray.length; i++){
    systems.addItem(list3ItemArray[i][0]);
    }
  i=0;


   // fill listboxes read

  //Open the spreadsheet and get the sheet objects
// var openedSS = SpreadsheetApp.openById("15tGpfnt72_yxVcO3Kp0wFBLfYpf8nr3vZCteprtX_Ss");
// var sheetList1 = openedSS.getSheetByName("data");//Spreadsheet must match with sheet name

numItemList4 = sheetList1.getLastRow()-1;//-1 is to exclude header row
  //get the item array
  list4ItemArray = sheetList1.getRange(2,1,numItemList1,1).getValues();
  //Add the items in ListBox
  for(var i=0; i<list4ItemArray.length; i++){
    products2.addItem(list4ItemArray[i][0]);
    }
  i=0;


numItemList5 = sheetList1.getLastRow()-1;//-1 is to exclude header row
  //get the item array
  list5ItemArray = sheetList1.getRange(2,3,numItemList2,1).getValues();
  //Add the items in ListBox
  for(var i=0; i<list5ItemArray.length; i++){
    stations2.addItem(list5ItemArray[i][0]);
    }
  i=0;


  numItemList6 = sheetList1.getLastRow()-1;//-1 is to exclude header row
  //get the item array
  list6ItemArray = sheetList1.getRange(2,5,numItemList3,1).getValues();
  //Add the items in ListBox
  for(var i=0; i<list6ItemArray.length; i++){
    systems2.addItem(list6ItemArray[i][0]);
    }
  i=0;


// fill up grid

  mygrid1.setWidget(0, 0, text1);
  mygrid1.setWidget(0, 1, text2);
  mygrid1.setWidget(0, 2, text3);
  mygrid1.setWidget(0, 3, text4);
  mygrid1.setWidget(1, 3, text103);
  mygrid1.setWidget(1, 0, text100);
  mygrid1.setWidget(1, 1, text101);
  mygrid1.setWidget(1, 2, text102);
  mygrid1.setWidget(2, 0, systems);
  mygrid1.setWidget(2, 1, stations);
  mygrid1.setWidget(2, 2, products);
  panel.add(mygrid1,24,100);
  panel.add(inputbutton,436,270);


  mygrid2.setWidget(0, 0, text5);
  mygrid2.setWidget(0, 1, text6);
  mygrid2.setWidget(0, 2, text7);
  mygrid2.setWidget(0, 3, text8);
  mygrid2.setWidget(1, 3, text9);
  mygrid2.setWidget(1, 0, text10);
  mygrid2.setWidget(1, 1, text11); 
  mygrid2.setWidget(1, 2, text12);
  mygrid2.setWidget(2, 0, systems2);
  mygrid2.setWidget(2, 1, stations2);
  mygrid2.setWidget(2, 2, products2);
  panel.add(mygrid2,24,570);
  panel.add(showbutton,436,740);




//add the panel to the application
  app.add(panel);

// add callbacks


 handle.addCallbackElement(inputbutton)
       .addCallbackElement(showbutton)
       .addCallbackElement(text100)
       .addCallbackElement(text101)
       .addCallbackElement(text102)
       .addCallbackElement(text103)
       .addCallbackElement(text9)
       .addCallbackElement(text10)
       .addCallbackElement(text11)
       .addCallbackElement(text12)
       .addCallbackElement(systems)
       .addCallbackElement(stations)
       .addCallbackElement(products2)
       .addCallbackElement(systems2)
       .addCallbackElement(stations2)
       .addCallbackElement(products2)
       .addCallbackElement(panel);




  var doc = SpreadsheetApp.getActive();
  doc.show(app);
   return app;

}


 function handlerFunction(eventInfo) 
  {   
  var app=UiApp.getActiveApplication();
  var parameter = eventInfo.parameter;


   // There's a lot of information in 'parameter' about the event too, but we'll focus here
   // only on the callback elements.
  var panel = parameter.panel;
  var source = parameter.source;
  var text100 = parameter.text100;
  var text101 = parameter.text101; 
  var text102 = parameter.text102;
  var text103 = parameter.text103;
  var text104 = parameter.text104;
  var text105 = parameter.text105;
  var text106 = parameter.text106;
  var stations=parameter.stations;
  var stations2=parameter.stations2;
  var systems = parameter.systems;
  var systems2 = parameter.systems2;
  var products=parameter.products;
  var products2=parameter.products2;
  var but200 = parameter.but200;
  var but201 = parameter.but201; 
  var test1 = parameter.test1;  
  var test2 = parameter.test2;  


     switch (source)    
   {
        case '1':
            var app=UiApp.getActiveApplication();
       app.getElementById('text100').setText(systems)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '2':

            // get the value of last changed element in listbox written to editbox
            var app=UiApp.getActiveApplication();
            app.getElementById('text101').setValue(stations)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '3':
            var app=UiApp.getActiveApplication();
            app.getElementById('text102').setValue(products)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

               case '4':
            var app=UiApp.getActiveApplication();
            app.getElementById('text10').setText(systems2)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '5':

            // get the value of last changed element in listbox written to editbox
            var app=UiApp.getActiveApplication();
            app.getElementById('text11').setValue(stations)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;

        case '6':
            var app=UiApp.getActiveApplication();
            app.getElementById('text12').setValue(products2)
            .setStyleAttribute('textAlign', 'center');
            return app;
        break;


        case '200': // SAVE button cliked
         var app=UiApp.getActiveApplication();
       var parameter = eventInfo.parameter;


        // var parameter = eventInfo.parameter;
     //   var test1=app.getElementById('text100').setText(parameter.text.length);
     //   var test2=app.getElementById('text101').setText(parameter.text.length);

       var test1=app.getElementById('text100').setValue(text100);
       var test2=app.getElementById('text101').setValue(text101);

            if (test1 >= '1' && test2 >= '1')
            {
              app.getElementById('text103').setValue('123');

            }

            return app;
          break;


    }



 }
相关问题