将UiApp转换为HTMLService

时间:2019-02-26 07:16:53

标签: spreadsheet

我是Anthonyteacher.com http://www.anthonyteacher.com/blog/the-ultimate-google-gradebook-with-individual-score-reports博客中很酷的代码的用户。几年来我一直在使用脚本,没有任何问题,但是现在我不能再次使用该脚本,因为我必须将UiApp转换为HTMLservice。问题是如何转换下面的代码,使其可以在HTMLService上使用?

由于我是应用程序脚本的初学者,所以希望有人可以帮助我。 预先谢谢你。

// Script-as-app template.
function doGet() {
  var app = UiApp.createApplication();

  var button = app.createButton('Click Me');
  app.add(button);

  var label = app.createLabel('The button was clicked.')
                 .setId('statusLabel')
                 .setVisible(false);
  app.add(label);

  var handler = app.createServerHandler('myClickHandler');
  handler.addCallbackElement(label);
  button.addClickHandler(handler);

  return app;
}

function myClickHandler(e) {
  var app = UiApp.getActiveApplication();
  var label = app.getElementById('statusLabel');
  label.setVisible(true);

  app.close();
  return app;
}
var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheet/ccc?key=[INSERT SPREADHSEET KEY HERE]');//Put your spreadsheet key here
var sh1 = ss.getSheetByName('StudentGrades');//Super-sheet name
var logsheet = ss.getSheetByName('logsheet');
var data = sh1.getDataRange().getValues();
var user = Session.getEffectiveUser()
Logger.log(user)


function doGet() {
       var app = UiApp.createApplication().setTitle('Your Grades');//App title
       if(!getcol(user)){
          var warn = app.createLabel().setWidth('500').setText("Your results are not available or your instructor has the wrong email address. Please contact your instructor ASAP.");// if user is not in the list, warning + return
app.add(warn)
return app
          }
  var grid = app.createGrid(data.length, 2).setWidth('300px').setBorderWidth(1).setCellPadding(0).setCellSpacing(0).setStyleAttribute('borderCollapse','collapse').setId('grid');//Overall table styling
       var text = app.createLabel(user).setWidth('300px');
       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':'#fff','border':'1px solid #000'});//left column css attributes
         grid.setStyleAttributes(n-1, 1, {'fontWeight':'bold','background':'#fff','border':'1px solid #000'});//right column css attributes
       }
       app.add(grid);
       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',value)};// if number then 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
}

0 个答案:

没有答案