0

我正在尝试使用 Google Spreadhseets 开发成绩簿,但我只想根据用户电子邮件显示个人成绩。是否有一个 Google Apps 脚本功能可以查看已登录用户的电子邮件,在电子表格中搜索它(它将位于列的顶部),然后将该列显示给用户?

4

1 回答 1

0

我认为最好的解决方案可能是创建一个仅在 html 表中显示相关列的小型 webapp Ui。根据您的编程技能,它可以很容易设置。我本可以提出一个示例,但是您提供的有关电子表格中数据类型的信息太少...用户的数据在单个列中,但我想还有某种标题或行标识符必须显示给了解显示的内容吗?

用户必须授权 webapp 才能让您在他们登录时识别他们。


编辑:这里是一个可能的解决方案的简单示例,您可以从它开始。

我假设邮件在第 1 行,A 列中的描述。

var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheet/ccc?key=0AnqSFd3iikE3dGdQOXZpRmJnT0xjQklkdEZING5YREE#gid=0');
var sh1 = ss.getSheetByName('sheet1');
var logsheet = ss.getSheetByName('logsheet');
var data = sh1.getDataRange().getValues();
var user = Session.getEffectiveUser()


function doGet() {
       var app = UiApp.createApplication();
       if(!getcol(user)){
          var warn = app.createTextBox().setWidth('500').setValue("Your results are not available or you don't have permission to view these data");// if user is not in the list, warning + return
app.add(warn)
return app
          }
       var grid = app.createGrid(data.length, 2).setBorderWidth(1).setCellPadding(2);
       var text = app.createTextBox().setName('text').setId('text').setValue(user+'  | idx'+getcol(user) ).setWidth('300px');
       var btn = app.createButton('press here to confirm you have view these results');
       var handler = app.createServerHandler('log').addCallbackElement(grid);
       btn.addClickHandler(handler);
       var col = getcol(user)
       grid.setWidget(0,1,text).setText(0, 0, 'Results for');
       grid.setStyleAttribute('textAlign','center')
       for(n=1;n<data.length;++n){
         grid.setText(n, 0, data[n][0])
         grid.setText(n, 1, data[n][col])
         }
       app.add(grid).add(btn)
       return app
    }

function log(e){
      var text = e.parameter.text
      var app = UiApp.getActiveApplication();
    // add user name to the log sheet + timestamp + eventually send a mail etc...
    return app
    }


function getcol(mail){
  if(data[0].toString().indexOf(mail.toString())!=-1){
  for(zz=1;zz<data[0].length;++zz){
    if(data[0][zz] == mail){var colindex=zz;break}
  }
   return colindex
  }
  return false
}

EDIT2: 要打印具有所需小数位数的数字,您可以使用最近实现的printf功能,如下所示:

grid.setText(n, 1, Utilities.formatString('%.2f',data[n][col]));// 2 decimals, no leading 0

显示您想要使用的日期formatDate()

Utilities.formatDate(date, Session.getTimeZone(), "MM-dd")

编辑3:

这是一个处理不同类型数据的版本:

var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheet/ccc?key=0AnqSFd3iikE3dGdQOXZpRmJnT0xjQklkdEZING5YREE#gid=0');
var sh1 = ss.getSheetByName('sheet1');
var logsheet = ss.getSheetByName('logsheet');
var data = sh1.getDataRange().getValues();
var user = Session.getEffectiveUser()
Logger.log(user)


function doGet() {
       var app = UiApp.createApplication();
       if(!getcol(user)){
          var warn = app.createTextBox().setWidth('500').setValue("Your results are not available or you don't have permission to view these data");// if user is not in the list, warning + return
app.add(warn)
return app
          }
       var grid = app.createGrid(data.length, 2).setBorderWidth(1).setCellPadding(2).setStyleAttribute('background','#ffeeaa').setId('grid');
       var text = app.createTextBox().setName('text').setId('text').setValue(user+'  | idx'+getcol(user) ).setWidth('300px');
       var btn = app.createButton('press here to confirm you have view these results');
       var handler = app.createServerHandler('log').addCallbackElement(grid);
       btn.addClickHandler(handler);
       var col = getcol(user)
       grid.setWidget(0,1,text).setText(0, 0, 'Results for');
       grid.setStyleAttribute('textAlign','center')
       for(n=1;n<data.length;++n){
         grid.setText(n, 0, string(data[n][0]));
         grid.setText(n, 1, string(data[n][col]));
         }
       grid.setStyleAttributes(n-1, 0, {'fontWeight':'bold','background':'#ffff99'})
       grid.setStyleAttributes(n-1, 1, {'fontWeight':'bold','background':'#ffff99'})
       app.add(grid).add(btn)
       return app
    }

function log(e){
      var text = e.parameter.text
      var app = UiApp.getActiveApplication();
      app.getElementById('grid').setStyleAttribute('background','#bbffbb')
    // add user name to the log sheet + timestamp + eventually send a mail etc...
    return app
    }


function string(value){
Logger.log(typeof(value))
if (typeof(value)=='string'){return value};// if string then don't do anything
if (typeof(value)=='number'){return Utilities.formatString('%.1f / 20',value)};// if number ther format with 1 decimal
if (typeof(value)=='object'){return Utilities.formatDate(value, Session.getTimeZone(), "MM-dd")};//object >> date in this case, format month/day
return 'error'
}


function getcol(mail){
  if(data[0].toString().indexOf(mail.toString())!=-1){
  for(zz=1;zz<data[0].length;++zz){
    if(data[0][zz] == mail){var colindex=zz;break}
  }
   return colindex
  }
  return false
}
于 2013-04-14T10:33:41.207 回答