How can I make an iterated upload files?

后端 未结 1 521
梦如初夏
梦如初夏 2021-01-27 07:55

I have a google apps script to upload files:

function doGet () {
... 
select code file 
... 
return app; 
} 

doPost function (e) {
... 
code add file to folder         


        
相关标签:
1条回答
  • 2021-01-27 08:23

    With some help from Serge, I was able to finish what I started on this.

    //modified from script found here http://www.googleappsscript.org/miscellaneous/creating-form-elements-dynamically-using-google-apps-script-gas
    
    function doGet() {
      var app = UiApp.createApplication();
      var panel = app.createVerticalPanel();
      var formPanel = app.createFormPanel();
      var instructionsLabel = app.createLabel('Put your instructions here');
      var filesLabel = app.createLabel('Add Files to Upload');
      var table = app.createFlexTable().setId('table').setTag('0'); //Here tag will count the number of files
      //Write the header for the table
      var header = app.createLabel('File(s)');
      table.setWidget(0, 0, header);
    
      //Add the first row of files
      addFirstRow(app);
      var hidden = app.createHidden().setId('hiddenRowHolder').setName('hidden').setValue(table.getTag());
      //Add a button to submit the info
      var button = app.createSubmitButton('Upload File(s)');
      panel.add(instructionsLabel).add(filesLabel).add(table).add(hidden).add(button);
      formPanel.add(panel);
      app.add(formPanel);
      return app;
    }
    
    function addFirstRow(app){
      var table = app.getElementById('table');
      var tag = parseInt(table.getTag());
      Logger.log(tag);
      var numRows = tag+1;
      if(numRows >1){
        table.removeCell(numRows-1, 5);
        table.removeCell(numRows-1, 4);
      }
      Logger.log(numRows);
      var uploadWidget = app.createFileUpload();
      var uploadWidgetName = uploadWidget.setName('file'+numRows);
      var uploadWidgetId = uploadWidget.setId('file'+numRows);
      table.setWidget(numRows, 0, uploadWidget);
      table.setTag(numRows.toString());
      addButtons(app);
    }
    
    function addButtons(app){
      var table = app.getElementById('table');
      var numRows = parseInt(table.getTag());
    
    
      //Create handler to add/remove row
      var addRemoveRowHandler = app.createServerHandler('addRemoveRow');
      addRemoveRowHandler.addCallbackElement(table);
    
      //Add row button and handler
      var addRowBtn = app.createButton('+').setId('addOne').setTitle('Add row');
      table.setWidget(numRows, 4, addRowBtn);
      addRowBtn.addMouseUpHandler(addRemoveRowHandler);
    
      //remove row button and handler
      var removeRowBtn = app.createButton('-').setId('removeOne').setTitle('Remove row');
      table.setWidget(numRows, 5, removeRowBtn);
      removeRowBtn.addMouseUpHandler(addRemoveRowHandler);
    }
    
    function addRemoveRow(e){
      Logger.log(e.parameter.source);
      var app = UiApp.getActiveApplication();
      var table = app.getElementById('table');
      var tag = parseInt(e.parameter.table_tag);
      var hidden = app.getElementById('hiddenRowHolder');
      var source = e.parameter.source;
      //Logger.log(tag);
      if(source == 'addOne'){
        table.setTag(tag.toString());
        hidden.setValue(tag+1);
        addFirstRow(app);
      }
      else if(source == 'removeOne'){
        if(tag > 1){
          //decrement the tag by one
          var numRows = tag-1;
          table.removeRow(tag);
          //Set the new tag of the table
          table.setTag(numRows);
          hidden.setValue(numRows);
          //Add buttons in previous row
          addButtons(app); 
        }
      }
      return app;
    }
    
    function doPost(e) {
      var numFiles = Number(e.parameter.hidden);
      Logger.log(numFiles);
      for (var i = 1; i<=numFiles; i++){
        var fileBlob = e.parameter['file'+i];
        var newFile = DocsList.getFolderById("Your File Id Goes Here").createFile(fileBlob);//replace string with folder id where you want to place your files
      }
      var app = UiApp.getActiveApplication();
      var label = app.createLabel(numFiles +' file(s) uploaded successfully');
      app.add(label);
      return app;
    }
    
    0 讨论(0)
提交回复
热议问题