Google Apps脚本:根据用户电子邮件显示单列 [英] Google Apps Script: Display single column based on user email

查看:156
本文介绍了Google Apps脚本:根据用户电子邮件显示单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

我认为最好的解决方案可能是创建一个小的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()


函数doGet(){
var app = UiApp.createApplication();
if(!getcol(user)){
var warn = app.createTextBox()。setWidth('500')。setValue(您的结果不可用,或者您没有权限查看这些数据); //如果用户不在列表中,则警告+返回
app.add(警告)
返回应用程序
}
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('按此处确认您已查看这些结果');
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 grid.setText(n,0,data [b]
grid.setText(n,1,data [n] [col])
}
app.add(grid).add(btn)
返回应用程序
}

函数日志(e){
var text = e.parameter.text
var app = UiApp.getActiveApplication();
//将用户名添加到日志表+时间戳+最终发送邮件等...
返回应用程序
}


函数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
}
返回false
}

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

  grid.setText(n,1,Utilities.formatString('%2f',data [n] [col])); // 2位小数,无前导0 

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



$ $ p $ Utilities.formatDate(date,Session.getTimeZone(),MM-dd)
pre>




编辑3:

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

  var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheet / CCC键= 0AnqSFd3iikE3dGdQOXZpRmJnT0xjQklkdEZING5YREE#GID = 0' )?; 
var sh1 = ss.getSheetByName('sheet1');
var logsheet = ss.getSheetByName('logsheet');
var data = sh1.getDataRange()。getValues();
var user = Session.getEffectiveUser()
Logger.log(用户)


函数doGet(){
var app = UiApp.createApplication );
if(!getcol(user)){
var warn = app.createTextBox()。setWidth('500')。setValue(您的结果不可用,或者您没有权限查看这些数据); //如果用户不在列表中,则警告+返回
app.add(警告)
返回应用程序
}
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('按此处确认您已查看这些结果');
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 grid.setText(n,0,string (数据[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
}

函数日志(e){
var text = e.parameter.text
var app = UiApp.getActiveApplication();
app.getElementById('grid')。setStyleAttribute('background','#bbffbb')
//将用户名添加到记录表+时间戳+最终发送邮件等...
$返回应用程序



函数字符串(值){
Logger.log(typeof(value))
if(typeof(value) =='string'){return value}; // if string then does not do anything
if(typeof(value)=='number'){return Utilities.formatString('%。1f / 20' ,value)}; //如果数字格式有一个十进制
if(typeof(value)=='object'){return Utilities.formatDate(value,Session.getTimeZone(),MM-dd) }; // object>>日期格式月/日
返回'错误'
}


函数getcol(函数){
if(data [0] .toString()。indexOf(mail.toString())!= - 1){
for(zz = 1; zz if

return colindex

return false


I am trying to develop a gradebook with Google Spreadhseets, but I only want to display individual grades based on user email. Is there a Google Apps Script function that looks at a logged in user's email, searches the spreadhseet for it (it would be at the top of a column) and then displays that column back to the user?

解决方案

I think the best solution would probably be to create a small webapp Ui that shows only the relevant column in a html table. Depending on your programming skills it can be quite easy to setup. I could have suggested an example but you gave too few information about the type of data that are in the spreadsheet... User's data are in a single column but I suppose there is also some kind of headers or row identifiers that must be shown to understand what is shown ?

Users will have to authorize the webapp in order to let you identify them when they are logged in.


Edit : here is a simple example of a possible solution that you can start from.

I assumed mails are in row 1, descriptions in column 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 : To print numbers with the desired number of decimals you can use the recently implemented printf function like this :

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

to show dates like you want use formatDate()

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


EDIT3 :

here is a version that handles the different types of data :

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
}

这篇关于Google Apps脚本:根据用户电子邮件显示单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆