如何制作迭代上传文件?

时间:2014-03-05 09:37:12

标签: google-apps-script

我有一个google apps脚本来上传文件:

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

doPost function (e) {
... 
code add file to folder 
...
return app; 
} 

我想重复n次文件上传。我能怎么做? 感谢

拉​​菲尔

1 个答案:

答案 0 :(得分:0)

通过一些help from Serge,我能够完成我的工作。

//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;
}