Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 读取用户电子邮件并从电子表格返回信息的应用程序脚本_Google Apps Script_Google Sheets_Google Apps For Education - Fatal编程技术网

Google apps script 读取用户电子邮件并从电子表格返回信息的应用程序脚本

Google apps script 读取用户电子邮件并从电子表格返回信息的应用程序脚本,google-apps-script,google-sheets,google-apps-for-education,Google Apps Script,Google Sheets,Google Apps For Education,我正试图为我的学生制作一个脚本,放在我的谷歌网站上,这样他们就可以在家里查找MyMath的登录详细信息。我有一个电子表格,上面有他们所有的电子邮件,这些电子邮件与他们的登录详细信息相匹配,但是当他们访问页面时,我希望他们能够看到他们的行。我正在为此使用Session.getActiveUser().getEmail(),下面是我的所有代码,但它不起作用 任何帮助都将不胜感激 var userID = Session.getActiveUser().getEmail(); var userLogi

我正试图为我的学生制作一个脚本,放在我的谷歌网站上,这样他们就可以在家里查找MyMath的登录详细信息。我有一个电子表格,上面有他们所有的电子邮件,这些电子邮件与他们的登录详细信息相匹配,但是当他们访问页面时,我希望他们能够看到他们的行。我正在为此使用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;
}
var userID=Session.getActiveUser().getEmail();
var用户登录;
var-userPass;
var用户名;
var文本;
//从电子邮件中查找登录名和密码并添加到变量的函数
函数查找(){
var spreadsheetKey='1Uftmlegjpof353x1dp2IRQLIJA-70y8m-xnbyfj8ZA';
var sheet=电子表格app.openById(电子表格键);
var end=SpreadsheetApp.getActiveSheet().getLastRow();
var范围=sheet.getRange(1,0,end,4);
var values=range.getValues();
对于(i=0;i
您实现此功能的方式行不通。您将许多变量定义为“全局”,以便每个函数都可以使用它,但尽管这是真的,但函数将无法修改它们…,换句话说:它们无法为这些变量赋值

我建议以完全不同的方式构建它:一个调用查找函数的主doGet,后者返回适当的值(作为数组或对象)

代码是这样的,我没有测试它,因为我没有数据来测试它,但它应该能按预期工作

顺便说一句,考虑使用一些样式使它看起来更漂亮:-)参见展示一种聪明的方法

//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;
}
//在站点页面上创建用户界面的函数
函数doGet(){
var userID=Session.getEffectiveUser().getEmail();
var result=查找(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');
表.setText(0,1,'Login');
表.setText(0,2,'密码');
table.setText(1,0,result.userName);
setText(1,1,result.userLogin);
table.setText(1,2,result.userPass);
面板。添加(标签);
面板。添加(表);
应用程序添加(面板);
返回应用程序;
}
函数查找(用户ID){
var result={};
var spreadsheetKey='1Uftmlegjpof353x1dp2IRQLIJA-70y8m-xnbyfj8ZA';
var sheet=电子表格app.openById(电子表格键);
var end=SpreadsheetApp.getActiveSheet().getLastRow();
var范围=sheet.getRange(1,0,end,4);
var values=range.getValues();
对于(i=0;i
请注意,我们可以对对象属性使用更简单的表示法,而不是结果['text']=。。。我们可以写result.text=。。。我只是觉得它更清楚,但毕竟可能不是:-)