google-apps-script - 如何在事件处理程序中使用全局变量

标签 google-apps-script

我有一个全局变量联系人, var contacts = ContactsApp.getContacts();

我的文本框有一个名为 search() 的 KeyUphandler。

在搜索()中, contacts[0].getPrimaryEmail() 给出一个错误,“Cant call method getPrimaryEmail of undefined

但是 contacts[0].getPrimaryEmail() 在其他正常功能中工作正常。

我们不能在事件处理程序中使用全局变量吗?

代码如下,

//代码开始

   var contacts = ContactsApp.getContacts();

function ShowAuto() {

    var app = UiApp.createApplication().setTitle('Simple Autocomplete');

    var Panel = app.createVerticalPanel().setId('mainPanel').setWidth('555px');

    var textBox = app.createTextBox().setName('textBox').setWidth('330px').setId('textBox');

    var tBoxHandler = app.createServerKeyHandler('search');
    tBoxHandler.addCallbackElement(textBox);
    textBox.addKeyUpHandler(tBoxHandler);

    var listBox =    app.createListBox().setName('list').setWidth("330px").setId('list').setVisibleItemCount(5)
               .setStyleAttribute("border", "1px solid white")
               .setStyleAttribute("background", "white").setVisible(false);

   Panel.add(app.createLabel().setId('label'));



   Panel.add(textBox);
   Panel.add(listBox);

   app.add(Panel);


   var ownerEmail  = Session.getActiveUser().getEmail();


   // I used this method first.. It din work.
   // var contacts = ContactsApp.getContacts();
   // for (var i in contacts) 
   // {
   //  var emailStr = contacts[i].getPrimaryEmail();
   //  conts[i]=emailStr;
   // Browser.msgBox(emailStr);
   // }

   //These two calls are just to test, the contacts Array, They work fine :)

   getContact(0);
   getContact(1);

   var ss = SpreadsheetApp.getActiveSpreadsheet();
   ss.show(app);


   }

   function getContact(n)
   {

    // Browser.msgBox(contacts[n].getPrimaryEmail());
     Logger.log(contacts[n].getPrimaryEmail());  //works  fine 
     return contacts[n].getPrimaryEmail();
   }


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

   //Logger.log(contacts[0].getPrimaryEmail()); //Not working

     app.getElementById('label').setText(e.parameter.textBox.toString());
    // app.getElementById('label').setText(conts[0]);
     app.getElementById('list').setVisible(true);

     var searchKey = new RegExp(e.parameter.textBox.toString(),"gi");
     if (searchKey == "") 
       app.getElementById('textBox').setValue('');


     var listBoxCount = 0;

     for (i=0;i<5;i++){

       //Here is where i get error
        if(contacts[i].indexOf(e.parameter.textBox)!=-1 && listBoxCount < 5)
        {
          app.getElementById('list').addItem(conts[i]);
          listBoxCount++;
        }
     }

     }

对不起,如果我不清楚

最佳答案

嗯,我用过UserProperties.setPropertygetProperty 函数来避免使用全局变量。

每当调用服务器处理程序时,全局变量将按照 https://developers.google.com/apps-script/class_serverhandler 中的给定重新计算。 ,所以我的工作不正常。

感谢大家的支持:)

关于google-apps-script - 如何在事件处理程序中使用全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11156562/

相关文章:

javascript - 在 Google 应用程序脚本中使用 jquery AJAX Post 方法

javascript - Google Drive API 导出 GAS 文件特定版本

google-apps-script - 在 Google Apps 脚本中取消合并

javascript - 比较和获取谷歌脚本中两个数组的值

javascript - 在谷歌电子表格中连接日期和时间

javascript - 在 Google Apps 脚本中通过引用在函数之间传递变量

javascript - 如何将用户名放入单元格,谁对该行进行了最后修改?

google-apps-script - 使用 google java 客户端复制电子表格后,自定义 google 应用程序脚本不起作用

google-apps-script - anyoneCanAddSelf() Google CalendarEvent - 用户如何将自己添加为访客?

pdf - 如何将单张 PDF 文件直接下载到我的本地设备(不导出到 Google Drive)?