上传数据&将图片导入Google电子表格

时间:2014-10-26 03:31:18

标签: javascript file-upload google-apps-script google-form

我正在尝试让用户填写表单,我在此处完成表单 https://docs.google.com/forms/d/1mjDLcGvcVlSG0Tjfx2ZEvGdrFqvQo-ezaB1A4vaBFK8/viewform

我在这里阅读了答案Upload an image to a Google spreadsheet 我有两个问题:

  1. 如何调用我拥有的表单,而不是此代码中的表单

    var submissionSSKey = 'google sskey';
    var docurl = 'google form url'
    var listitems = ['Gender','Male','Female']
    var Panelstyle = {'background':'#dddddd','padding':'40px','borderStyle':'solid','borderWidth':'10PX','borderColor':'#bbbbbb'}
    
    function doGet() {
      var app = UiApp.createApplication().setTitle('Biodata').setStyleAttribute('padding','50PX');
      var panel = app.createFormPanel().setStyleAttributes(Panelstyle).setPixelSize(400, 200);
      var title = app.createHTML('<B>89 Law School Class Alumni Biodata</B>').setStyleAttribute('color','grey').setStyleAttribute('fontSize','25PX');
      var grid = app.createGrid(6,2).setId('grid');
      var list1 = app.createListBox().setName('list1').setWidth('130');
       for(var i in listitems){list1.addItem(listitems[i])}    
      var Textbox1 = app.createTextBox().setWidth('150px').setName('TB1');
      var email = app.createTextBox().setWidth('150px').setName('mail');
      var upLoad = app.createFileUpload().setName('uploadedFile');
      var submitButton = app.createSubmitButton('<B>Submit</B>'); 
      var warning = app.createHTML('Please fill in all fields').setStyleAttribute('background','#bbbbbb').setStyleAttribute('fontSize','18px');
      //file upload
      var cliHandler2 = app.createClientHandler()
      .validateLength(Textbox1, 1, 40).validateNotMatches(list1,'Select a category').validateEmail(email).validateNotMatches(upLoad, 'FileUpload')
      .forTargets(submitButton).setEnabled(true)
      .forTargets(warning).setHTML('Now you can submit your form').setStyleAttribute('background','#99FF99').setStyleAttribute('fontSize','12px');
    
      //Grid layout of items on form
      grid.setWidget(0, 1, title)
          .setText(1, 0, 'Category')
          .setWidget(1, 1, list1.addClickHandler(cliHandler2))
          .setText(2, 0, 'Name')
          .setWidget(2, 1, Textbox1.addClickHandler(cliHandler2))
          .setText(3, 0, 'Email')
          .setWidget(3, 1, email)
          .setText(4, 0, 'Image File')
          .setWidget(4, 1, upLoad.addChangeHandler(cliHandler2))
          .setWidget(5, 0, submitButton)
          .setWidget(5, 1, warning);
    
      var cliHandler = app.createClientHandler().forTargets(warning).setHTML('<B>PLEASE WAIT WHILE THE FILE IS UPLOADING<B>').setStyleAttribute('background','yellow');
      submitButton.addClickHandler(cliHandler).setEnabled(false);  
      panel.add(grid);
      app.add(panel);
      return app;
    }
    
    
    function doPost(e) {
      var app = UiApp.getActiveApplication();
      var ListVal = e.parameter.list1;
      var textVal = e.parameter.TB1;
      var Email = e.parameter.mail;
      var fileBlob = e.parameter.uploadedFile;
      var blob = fileBlob.setContentTypeFromExtension()
      var img = DocsList.createFile(blob);
      try{
      var folder = DocsList.getFolder('photos');
      }catch(e){DocsList.createFolder('photos');var folder = DocsList.getFolder('photos')}
      img.addToFolder(folder);
      img.removeFromFolder(DocsList.getRootFolder());
      var weight = parseInt(img.getSize()/1000);
      var sheet = SpreadsheetApp.openById(submissionSSKey).getSheetByName('Sheet1');
      var lastRow = sheet.getLastRow();
      var targetRange = sheet.getRange(lastRow+1, 1, 1, 4).setValues([[ListVal,textVal,Email,"https://drive.google.com/uc?export=view&id="+img.getId()]]);
      var imageInsert = sheet.getRange(lastRow+1, 5).setFormula('=image("https://drive.google.com/uc?export=view&id='+img.getId()+'")');
      sheet.setRowHeight(lastRow+1, 80);
      var GDoc = DocumentApp.openByUrl(docurl)
      GDoc.appendTable([['Category : '+ListVal,'Name : '+textVal,'Email : '+Email]])
      var inlineI = GDoc.appendImage(img);
      var width = inlineI.getWidth();
      var newW = width;
      var height = inlineI.getHeight();
      var newH = height;
      var ratio = width/height;
      Logger.log('w='+width+'h='+height+' ratio='+ratio);
      if(width>640){
      newW = 640;
      newH = parseInt(newW/ratio);
      }
      inlineI.setWidth(newW).setHeight(newH)
      GDoc.appendParagraph('IMAGE size : '+width+' x '+height+' (eventually) resized to '+newW+' x '+newH+' for PREVIEW ('+weight+' kB)   ');
      GDoc.appendHorizontalRule();
      GDoc.saveAndClose();
      app.add(app.createLabel('Thank you for submitting'));
      return app
    }
    
    1. 当我运行doPost(e)
    2. 时,我从“未定义”获取“无法读取属性”参数

      知道我该怎么做,请问?我有点落后于时间表

      非常感谢

1 个答案:

答案 0 :(得分:0)

与您的2个问题有关:

  1. 您无法混合使用FormApp构建的表单和使用UiApp构建的表单,如果您想保留文件,则必须使用UiApp构建整个表单上传功能。作为替代方案,如果您对html更熟悉,您当然可以使用HTMLService构建整个表单...
  2. 如果您尝试从脚本编辑器运行doPost函数,您将始终获得e的未定义值,因为e实际上是包含{{1}返回的所有元素的事件信息调用(也称为回调元素)
  3. 我一直在查看你的表单,我认为在UiApp中转换它应该很容易,因为所有字段都是简单的textBox元素。您使用的帖子使用clientHandlers来强制某些字段,因此您拥有构建自己的表单所需的所有信息。

    如果你想做到这一点&#34;看起来像&#34;您的表单只是更改面板的样式属性,并最终为某些项添加更多样式属性。类似样式的可用CSS是viewable here.