Google apps script Google应用程序脚本:基于用户电子邮件显示单个列
我正在尝试用Google Spreadhseets开发成绩册,但我只想根据用户电子邮件显示个人成绩。是否有Google Apps脚本功能可以查看登录用户的电子邮件,在spreadhseet中搜索它(它将位于列的顶部),然后将该列显示回用户?我认为最好的解决方案可能是创建一个小的webapp Ui,只显示html表中的相关列。根据您的编程技能,它可以很容易地设置。我本可以举个例子,但您提供的关于电子表格中数据类型的信息太少。。。用户的数据在一列中,但我想还必须显示某种标题或行标识符才能理解显示的内容 用户必须授权webapp,以便您在登录时识别他们Google apps script Google应用程序脚本:基于用户电子邮件显示单个列,google-apps-script,Google Apps Script,我正在尝试用Google Spreadhseets开发成绩册,但我只想根据用户电子邮件显示个人成绩。是否有Google Apps脚本功能可以查看登录用户的电子邮件,在spreadhseet中搜索它(它将位于列的顶部),然后将该列显示回用户?我认为最好的解决方案可能是创建一个小的webapp Ui,只显示html表中的相关列。根据您的编程技能,它可以很容易地设置。我本可以举个例子,但您提供的关于电子表格中数据类型的信息太少。。。用户的数据在一列中,但我想还必须显示某种标题或行标识符才能理解显示的内
编辑:下面是一个简单的示例,说明了一种可能的解决方案,您可以从中开始 我假设邮件在第一行,描述在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
}
要显示所需的日期,请使用formatDate()
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
}
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(用户)
函数doGet(){
var app=UiApp.createApplication();
如果(!getcol(用户)){
var warn=app.createTextBox().setWidth('500').setValue(“您的结果不可用或您没有查看这些数据的权限”);//如果用户不在列表中,则warning+return
应用程序添加(警告)
返回应用程序
}
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(网格);
btn.addClickHandler(处理程序);
var col=getcol(用户)
setWidget(0,1,text).setText(0,0,'Results for');
grid.setStyleAttribute('textAlign','center')
对于(n=1;没问题。这很好。到目前为止它还可以工作,但我有几个问题。这个应用程序假设他们是谷歌用户,对吗?我是否可以将其链接到我的Wordpress数据库,并通过get_userdata函数拉入用户数据?此外,日期样式为“2013年3月11日周一00:00:00 GMT+0900(JST)”,但我只需要一个简单的“3/11”风格。这是在哪里控制的?如果你想让自动登录id像本例中那样工作,用户必须有一个Google帐户(不一定是gmail地址,可以将任何电子邮件“耦合”到Google帐户)但是你也可以用用户名和密码设置手动登录,即使这更容易破解…关于日期:可能可以在电子表格中导入wordpress用户数据库,但我不确定这是否有用。在脚本中这样做,简单的CSV导入将很容易做到,这样你就可以填充列标题。如果你想要另一个解决方案,请在这里发表另一篇文章,它有点脱离主题;-)。我尝试在编辑2中添加这两个函数。我将format(date)函数放在grid.SetStyleAttribute下面,但它没有做任何更改。我将把另一个函数放在哪里?对不起,这对我来说是全新的?grid.setText(n,1,Utilities.formatString('%.2f',data[n][col]);
取代了第1版中的同一行(grid.setText(n,1…
)至于日期,我不知道您在哪里需要它……也许您可以用您使用的代码编辑您的问题和/或解释/显示您拥有的数据类型。
Utilities.formatDate(date, Session.getTimeZone(), "MM-dd")
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
}