gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:00:36 27 4
gpt4 key购买 nike

我有一个全局变量联系人, 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com