我正在尝试为我的学生制作一个脚本放在我的Google网站上,以便他们可以在家查找他们的mymaths登录详细信息。我有一个电子表格,其所有电子邮件都与他们的登录详细信息相匹配,但当他们访问该页面时,我希望能够看到他们的行。我正在使用Session.getActiveUser()。getEmail(),我的所有代码都在下面,但它不起作用!
非常感谢任何帮助。
var userID = Session.getActiveUser().getEmail();
var userLogin;
var userPass;
var userName;
var text;
//Function to lookup login and password from email and add to variables
function lookup() {
var spreadsheetKey = '1UftMLEgJPof3533X1Dp2IRqLIJa-70y8m-xnbyfj8ZA';
var sheet = SpreadsheetApp.openById(spreadsheetKey);
var end = SpreadsheetApp.getActiveSheet().getLastRow();
var range = sheet.getRange(1, 0, end, 4);
var values = range.getValues();
for (i = 0; i < values.length; ++i) {
if (values[i][0] == userID) {
text = 'Here are your login details for MyMaths. Now get on with your homework!'
userName = string(values[i][1]);
userLogin = string(values[i][2]);
userPass = string(values[i][3]);
}
else {
text = 'Sorry, but there is an error. You will need to check in your book or ask your teacher for your MyMaths login details.'
}
}
}
//Function to create Userinterface on site page
function doGet() {
var app = UiApp.createApplication();
var panel = app.createAbsolutePanel();
var label = app.createLabel(text);
var table = app.createGrid(2,3);
table.setText(0, 0, 'Name');
table.setText(0, 1, 'Login');
table.setText(0, 2, 'Password');
table.setText(1, 0, userName);
table.setText(1, 1, userLogin);
table.setText(1, 2, userPass);
panel.add(label);
panel.add(table);
app.add(panel);
return app;
}
答案 0 :(得分:0)
您实施此方法的方式无效。您将许多变量定义为&#34; global&#34;所以每个函数都可以使用它但是虽然这是真的,但函数不能修改它们......换句话说:它们不能为这些变量赋值。
我建议以完全不同的方式构建它:一个调用查找函数的主doGet,后者返回适当的值(作为数组或对象)。
代码是这样的,我没有测试它,因为我没有数据这样做,但它应该按预期工作。
顺便说一句,考虑使用一些样式使它看起来更好:-)看this post from Anees Hameed显示一种聪明的方法。//Function to create Userinterface on site page
function doGet() {
var userID = Session.getEffectiveUser().getEmail();
var result = lookup(userID);
var app = UiApp.createApplication();
var panel = app.createAbsolutePanel();
var label = app.createLabel(result.text);
var table = app.createGrid(2,3);
table.setText(0, 0, 'Name');
table.setText(0, 1, 'Login');
table.setText(0, 2, 'Password');
table.setText(1, 0, result.userName);
table.setText(1, 1, result.userLogin);
table.setText(1, 2, result.userPass);
panel.add(label);
panel.add(table);
app.add(panel);
return app;
}
function lookup(userID) {
var result = {};
var spreadsheetKey = '1UftMLEgJPof3533X1Dp2IRqLIJa-70y8m-xnbyfj8ZA';
var sheet = SpreadsheetApp.openById(spreadsheetKey);
var end = SpreadsheetApp.getActiveSheet().getLastRow();
var range = sheet.getRange(1, 0, end, 4);
var values = range.getValues();
for (i = 0; i < values.length; ++i) {
if (values[i][0] == userID) {
result['text'] = 'Here are your login details for MyMaths. Now get on with your homework!'
result['userName'] = string(values[i][1]);
result['userLogin'] = string(values[i][2]);
result['userPass'] = string(values[i][3]);
}
else {
result['text'] = 'Sorry, but there is an error. You will need to check in your book or ask your teacher for your MyMaths login details.'
result['userName'] = '-';
result['userLogin'] = '-';
result['userPass'] = '-';
}
}
return result;
}