GAS e.parameter undefined, uploadWidget won't setName

前端 未结 1 1630
后悔当初
后悔当初 2021-01-27 06:36

I\'m working on an answer for another question found here. I have modified a different tutorial script from here, but I\'m having an issue that the e.parameter for the fileUplo

相关标签:
1条回答
  • 2021-01-27 07:20

    File upload in forms needs a doPost function to work, this is not an option ;)

    In such a structure (doGet/doPost) you don't have to define a handler nor a callBackElement, the formPanel is supposed to include all its elements automatically.

    So I tried your modified code and still get one major issue with the numFiles value that is on the table tag : I can't get it...

    If I replace it with a fixed value then everything works nicely, I get the files in the right folder.

    So this answer is not a good answer because it doesn't bring a full solution but at least it reduces its initial scope to that point : how to get this #@!#! numFiles value ?


    EDIT : Found the issue : table doesn't support setName method so its value can't be retrieved in the submitHandler. We should use another widget to hold that value.

    new working Code below : (I used a textBox as a "hidden" widget for test only, please replace by a hiddenWidget when in production)

    function doGet() {
      var app = UiApp.createApplication();
      var panel = app.createVerticalPanel();
      var formPanel = app.createFormPanel();
      var folderLabel = app.createLabel('Folder Name (temp placeholder to remember to use .getFolderById(folderId) to place in specific folder)');
      var folderNameTextBox = app.createTextBox().setId('folderName').setName('folderName');
      var filesLabel = app.createLabel('Add Files to Upload');
      var table = app.createFlexTable().setId('table').setTag('0'); //Here tag will count the number of members
      //Write the header for the table
      var headerArray = ['File(s)'];
      for(var i=0; i<headerArray.length; i++){
        table.setWidget(0, i, app.createLabel(headerArray[i]));
      }
      //Add the first row of form elelments to input Member information
      addMemebrRow(app);
      var hidden = app.createTextBox().setName('hidden').setId('hidden').setValue(table.getTag());// used to hold the number of files, replace with createHidden()
      //Add a button to submit the info
      var button = app.createSubmitButton('Upload File(s)');
      panel.add(folderLabel)
        .add(folderNameTextBox)
        .add(filesLabel)
        .add(table)
        .add(button);
      formPanel.add(panel.add(hidden));
      app.add(formPanel);
      return app;
    }
    
    function addMemebrRow(app){
      var table = app.getElementById('table');
      var tag = Number(table.getTag());
      Logger.log('tag='+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);
      Logger.log(uploadWidgetId.getId());
      Logger.log(uploadWidgetName);
      table.setWidget(numRows, 0, uploadWidget);
      table.setTag(numRows);
      addButtons(app);
    }
    
    function addButtons(app){
      var table = app.getElementById('table');
      var numRows = Number(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 hidden = app.getElementById('hidden');
      var table = app.getElementById('table');
      var tag = Number(e.parameter.table_tag);
      var source = e.parameter.source;
      //Logger.log(tag);
      if(source == 'addOne'){
        table.setTag(tag.toString());
        hidden.setValue(tag+1);
        addMemebrRow(app);
      }
      else if(source == 'removeOne'){
        if(tag > 1){
          //Dcrement 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 foldername = e.parameter.folderName;
      Logger.log('foldername = '+foldername);
      var numFiles = Number(e.parameter.hidden);
      Logger.log('numFiles = '+numFiles);
      for (var i = 1; i<=numFiles; i++){
        Logger.log(i);
        var fileBlob = e.parameter['file'+i];
        var newFile = DocsList.getFolderById("0B3qSFd3iikE3QXdubnVoMXlGMkk").createFile(fileBlob);
      }
      var app = UiApp.getActiveApplication();
      var label = app.createLabel(numFiles +' file(s) uploaded successfully');
      app.add(label);
      return app;
    }
    
    0 讨论(0)
提交回复
热议问题