Google Apps脚本:根据用户电子邮件显示单个列

时间:2013-04-13 22:55:10

标签: google-apps-script

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

1 个答案:

答案 0 :(得分:0)

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

用户必须授权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")

EDIT3:

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

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
}