简体   繁体   English

Google Apps脚本文件上传状态指示器clickhandler

[英]Google Apps Script file upload status indicator clickhandler

Taking components from a couple other examples . 从另外两个示例中选取组件。 I pieced together the following script (can run it here ): 我拼凑了以下脚本(可以在此处运行):

var submissionSSKey = '0AhpMXVz35LNCdG1Qa0c2V1hHRnhOUU00SjQ0SEdlRVE';
var Panelstyle = {'background':'#dddddd','padding':'40px','borderStyle':'solid','borderWidth':'10PX','borderColor':'#bbbbbb'}

function doGet() {

  var app = UiApp.createApplication().setTitle('PQOT File Upload').setStyleAttribute('padding','50PX');
  var panel = app.createFormPanel().setStyleAttributes(Panelstyle).setPixelSize(500, 200);
  var title = app.createHTML('<B>File Upload</B>').setStyleAttribute('color','grey').setStyleAttribute('fontSize','25PX');
  var grid = app.createGrid(8,4).setId('grid');
  var RequestID = app.createTextBox().setWidth('250px').setName('reqID');
  var unix = app.createTextBox().setWidth('250px').setName('unix');
  var upLoad = app.createFileUpload().setName('uploadedFile');
  var submitButton = app.createSubmitButton('<B>Upload</B>'); 
  var warning = app.createHTML('Please fill in all fields')
      .setStyleAttribute('background','#ff6262')
      .setStyleAttribute('fontSize','18px');
  var uploadtracker = app.createTextBox().setVisible(false);

  var cliHandler2 = app.createClientHandler()
      .validateLength(RequestID, 5, 5)
      .validateInteger(RequestID)
      .validateLength(unix, 1, 12)
      .validateMatches(uploadtracker, 'selected')
      .forTargets(submitButton).setEnabled(true)
      .forTargets(warning).setHTML('File upload ready')
      .setStyleAttribute('background','#99FF99')
      .setStyleAttribute('fontSize','18px');

  var cliHandler3 = app.createClientHandler()
      .forTargets(uploadtracker)
      .setText('selected')

  grid.setWidget(0, 1, title)
      .setText(1, 0, 'PQOT Request # (5 digits):')
      .setWidget(1, 1, RequestID.addKeyUpHandler(cliHandler2))
      .setText(2, 0, 'Enter Unix ID:')
      .setWidget(2, 1, unix.addKeyUpHandler(cliHandler2))
      .setText(3, 0, 'Select File:')
      .setWidget(3, 1, upLoad.addChangeHandler(cliHandler3).addChangeHandler(cliHandler2))
      .setText(5, 0, 'Completion Check:')
      .setWidget(5, 1, warning)
      .setWidget(6, 3, submitButton)
      .setWidget(7,0,uploadtracker).addClickHandler(cliHandler2);


  var cliHandler = app.createClientHandler()
      .forTargets(warning).setHTML('FILE UPLOADING...')
      .setStyleAttribute('background','#FEFE22')
      .setStyleAttribute('fontSize','18px')
      .setVisible(true);

  submitButton.addMouseUpHandler(cliHandler)
      .setEnabled(false);  



   panel.add(grid);
   app.add(panel);
   return app;
}



function doPost(e) {
  var app = UiApp.getActiveApplication();
  var reqID = e.parameter.reqID;
  var unix = e.parameter.unix;
  var fileBlob = e.parameter.uploadedFile;
  var doc = DocsList.createFile(fileBlob);
  var timestamp = Utilities.formatDate(new Date(), "PST", "MM-dd-yyyy HH:mm:ss"); 
  var description = 'Requestor Unix: '+ unix + '  PQOT Request ID: ' + reqID + ' Timestamp: ' + timestamp
  doc.setDescription(description)
  var folder = DocsList.getFolder ('PQOT Upload')
  doc.addToFolder(folder);
  var sheet = SpreadsheetApp.openById(submissionSSKey).getSheetByName('Sheet1');
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 1, 3).setValues([[reqID,unix,doc.getUrl()]]);
  var panel = app.createFormPanel().setStyleAttributes(Panelstyle).setPixelSize(500, 200);
  var label = app.createLabel('Thank you!  Your file was uploaded successfully.  To submit another, please refresh your browser: (windows - F5, mac - ⌘ + r). ');


   label.setStyleAttribute("color", "green")
        .setStyleAttribute("fontSize", "large");
        label.setStyleAttribute("text-align","left");
        label.setSize(300,200);

  panel.add(label);
  app.add(panel)      
   return app;
}

Everything is working great however I noticed that the intermediate message "File uploading..." is not displaying. 一切正常,但是我注意到中间消息“文件上传中……”没有显示。 This section: 这个部分:

  var cliHandler = app.createClientHandler()
      .forTargets(warning).setHTML('FILE UPLOADING...')
      .setStyleAttribute('background','#FEFE22')
      .setStyleAttribute('fontSize','18px')
      .setVisible(true);

I verified that the bug is also occurring in the similar scripts. 我验证了类似脚本中也存在该错误。 I'm thinking it is something to do with the introduction of the 3rd click handler. 我认为这与引入第三点击处理程序有关。 Anyway, this section of the code doesn't seem to be running or the widget is not visible for some reason. 无论如何,这段代码似乎没有在运行,或者由于某种原因该小部件不可见。

Here is a working version of your code, I made very few changes but I still have a couple of questions : 这是您的代码的有效版本,我做了很少的更改,但仍然有两个问题:

  1. What is the uploadtracker widget for ? uploadtracker小部件的用途是什么? I removed the handler on this (invisible) widget and it started working... It was target AND source altogether... not a good idea ;-) 我删除了这个(不可见的)小部件上的处理程序,它开始工作了……这完全是目标和源……不是一个好主意;-)
  2. In the doPost you used a formPanel to put your "thank you" label... did it work in your test ? 在doPost中,您使用了formPanel来放置“谢谢”标签...它在您的测试中起作用了吗? In my test it caused an error, so I replaced by a verticalPanel and it fixed the error. 在我的测试中,它导致了错误,因此我换成了verticalPanel并修复了错误。 Had you an idea behind that ? 你背后有一个主意吗?

Now the code : 现在的代码:

function doGet() {
  var app = UiApp.createApplication().setTitle('PQOT File Upload').setStyleAttribute('padding','50PX');
  var panel = app.createFormPanel().setStyleAttributes(Panelstyle).setPixelSize(500, 200);
  var title = app.createHTML('<B>File Upload</B>').setStyleAttribute('color','grey').setStyleAttribute('fontSize','25PX');
  var grid = app.createGrid(8,4).setId('grid');
  var RequestID = app.createTextBox().setWidth('250px').setName('reqID');
  var unix = app.createTextBox().setWidth('250px').setName('unix');
  var upLoad = app.createFileUpload().setName('uploadedFile');
  var submitButton = app.createSubmitButton('<B>Upload</B>').setEnabled(false); 
  var warning = app.createHTML('Please fill in all fields')
  .setStyleAttribute('background','#ff6262')
  .setStyleAttribute('fontSize','18px');
  var uploadtracker = app.createTextBox().setVisible(false);

  var cliHandler1 = app.createClientHandler()
  .forTargets(warning).setHTML('FILE UPLOADING...')
  .setStyleAttribute('background','#FEFE22')
  .setStyleAttribute('fontSize','18px')
  .setVisible(true);

  var cliHandler2 = app.createClientHandler()
  .validateLength(RequestID, 5, 5)
  .validateInteger(RequestID)
  .validateLength(unix, 1, 12)
  .validateMatches(uploadtracker, 'selected')
  .forTargets(submitButton).setEnabled(true)
  .forTargets(warning).setHTML('File upload ready')
  .setStyleAttribute('background','#99FF99')
  .setStyleAttribute('fontSize','18px');

  var cliHandler3 = app.createClientHandler()
  .forTargets(uploadtracker)
  .setText('selected');;

  grid.setWidget(0, 1, title)
      .setText(1, 0, 'PQOT Request # (5 digits):')
      .setWidget(1, 1, RequestID.addKeyUpHandler(cliHandler2))
      .setText(2, 0, 'Enter Unix ID:')
      .setWidget(2, 1, unix.addKeyUpHandler(cliHandler2))
      .setText(3, 0, 'Select File:')
      .setWidget(3, 1, upLoad.addChangeHandler(cliHandler3).addChangeHandler(cliHandler2))
      .setText(5, 0, 'Completion Check:')
      .setWidget(5, 1, warning)
      .setWidget(6, 3, submitButton)
      .setWidget(7,0,uploadtracker);



  submitButton.addClickHandler(cliHandler1);  



   panel.add(grid);
   app.add(panel);
   return app;
}



function doPost(e) {
  var app = UiApp.getActiveApplication();
  var reqID = e.parameter.reqID;
  var unix = e.parameter.unix;
  var fileBlob = e.parameter.uploadedFile;
  var doc = DocsList.createFile(fileBlob);
  var timestamp = Utilities.formatDate(new Date(), "PST", "MM-dd-yyyy HH:mm:ss"); 
  var description = 'Requestor Unix: '+ unix + '  PQOT Request ID: ' + reqID + ' Timestamp: ' + timestamp
  doc.setDescription(description)
  var folder = DocsList.getFolder ('PQOT Upload')
  doc.addToFolder(folder);
  var sheet = SpreadsheetApp.openById(submissionSSKey).getSheetByName('Sheet1');
  var lastRow = sheet.getLastRow();
  var targetRange = sheet.getRange(lastRow+1, 1, 1, 3).setValues([[reqID,unix,doc.getUrl()]]);
  var panel = app.createVerticalPanel().setStyleAttributes(Panelstyle).setPixelSize(500, 200);
  var label = app.createLabel('Thank you!  Your file was uploaded successfully.  To submit another, please refresh your browser: (windows - F5, mac - ⌘ + r). ');


   label.setStyleAttribute("color", "green")
        .setStyleAttribute("fontSize", "large");
        label.setStyleAttribute("text-align","left");
        label.setSize(300,200);

  panel.add(label);
  app.add(panel)      
   return app;
}

EDIT : NOTE 编辑:注意

Even with the "selected" trick it doesn't prevent the case of a user going back and delete some fields, same for the fileupload if one remove the file it doesn't update the validation of the button. 即使使用“选定”技巧,它也不会阻止用户返回并删除某些字段的情况,这与fileupload相同,如果删除文件不会更新按钮的有效性。 I'm afraid it's a job to handle in the server handler (doPost) to trap all errors and re-submit the form. 恐怕要在服务器处理程序(doPost)中进行处理以捕获所有错误并重新提交表单是一项工作。 There is an interesting recent post from Mogsdad about multi step forms : link Mogsdad最近有一篇有趣的文章,涉及多步表单: 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM