bal*_*oss 3 google-apps-script
我有一个全局变量contacts, var contacts = ContactsApp.getContacts();
我的文本框中有一个名为search()的KeyUphandler.
在search()中,contacts [0] .getPrimaryEmail()发出错误," Cant调用方法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++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
对不起,如果我不清楚
好吧,我已经使用UserProperties.setProperty和getProperty函数来避免使用全局变量.
每当调用服务器处理程序时,将按照https://developers.google.com/apps-script/class_serverhandler中的说明重新计算全局变量,因此我的工作不正常.
感谢你们的支持 :)
| 归档时间: |
|
| 查看次数: |
3826 次 |
| 最近记录: |