Upload an image to a Google spreadsheet

后端 未结 5 975
长情又很酷
长情又很酷 2020-11-30 06:59

I am making a photo shooting contest, the competitor should register using a Google registration form, and upload his photo as well. I searched all over the internet to find

5条回答
  •  野性不改
    2020-11-30 07:42

    EDIT : I upgrade change the code a little bit, because as yyk mentionned, UiApp "doclist" deprecated since December 11, 2014. I use it to create a trombinoscope (i don't know the word in english, group gallery perhaps) in a google doc, people uploaded their picture nd name in a form. I don't use a spreadsheet. Here is the code :

       function doGet(e) {
    var app = UiApp.createApplication().setTitle('Trombi');
    var panel = app.createFormPanel();
    var grid = app.createGrid(3,2).setId('registrationGrid');
    var nameLabel = app.createLabel('Name');
    var nameTextbox = app.createTextBox().setWidth('150px').setName('Name');
    var submitButton = app.createSubmitButton('send'); 
    var warning = app.createHTML('Please wait').setStyleAttribute('background','yellow').setVisible(false)
    //file upload
    var upLoadTypeLabel = app.createLabel('File to Upload');
    var upLoad = (app.createFileUpload().setName('thefile'));
    //Grid layout of items on form
    grid.setWidget(0, 0, nameLabel)
        .setWidget(0, 1, nameTextbox)
        .setWidget(1, 0, upLoadTypeLabel)
        .setWidget(1, 1, upLoad)
        .setWidget(2, 0, submitButton)
        .setWidget(2, 1, warning)
    var cliHandler = app.createClientHandler().forTargets(warning).setVisible(true)
    submitButton.addClickHandler(cliHandler);  
    panel.add(grid);
    app.add(panel);
    return app;}
    
    function doPost(e) {
    var app = UiApp.getActiveApplication();  
    var Name = e.parameter.Name;
    //app.getElementById('info').setVisible(true).setStyleAttribute('color','red');
     // data returned is a blob for FileUpload widget
    var fileBlob = e.parameter.thefile;  
    var doc = DriveApp.createFile(fileBlob).setName(Name); 
    var doc = DocumentApp.openById('your key');
    var body = doc.getBody();
    var inlineI = body.appendImage(fileBlob);
      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>200){
      newW = 200;
      newH = parseInt(newW/ratio);
      }
      inlineI.setWidth(newW).setHeight(newH)
      body.appendParagraph(Name);
      body.appendHorizontalRule();
    doc.saveAndClose();
    app.add(app.createLabel('success')); 
    return app
     }

提交回复
热议问题