0

I have a google apps script to upload files:

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

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

I want to repeat n times the file uploads. How can I do? thanks

raffaele

raffaele
  • 51
  • 2
  • 9
  • I have an idea, but can you give me more information? 1. Will all files be saved in the same folder? If not, are the names of the folders already set, or will they be generated from the file names? 2. Do you know how many files you will upload when you start? Will you be upload 5 files every time, or will the number vary (5 today, 3 tomorrow, etc.)? 3. Can I see more of the context of your script? What have you tried already, and where is it breaking? I can't tell from the few words you have what your structure looks like. If you can give me more info, I can probably answer this. – thoughtcrime Mar 07 '14 at 01:06
  • 1. Will all files be saved in the same folder? yes - 2. Do you know how many files you will upload when you start? no - Will you be upload 5 files every time, or will the number vary (5 today, 3 tomorrow, etc.)? the number vary - 3. Can I see more of the context of your script? yes ([linkhttps://github.com/drewdepriest/AppScriptUpload/blob/master/CheckboxUploadScript.gs]). I modified the script but the structure is. thanks to the availability. raffaele – raffaele Mar 07 '14 at 14:59
  • I'm almost to an answer: https://sites.google.com/site/iteratedfileupload/ It's not exactly perfect, but it allows using the + and - buttons to add another upload option. I am still getting an error in the handler, but I'm trying to work it out. – thoughtcrime Mar 08 '14 at 10:03
  • my code and tes page: https://sites.google.com/a/paparella.it/uploadfile/ – raffaele Mar 09 '14 at 12:02
  • I think you need to set your page public so it can be seen. We don't have permission to view it. – thoughtcrime Mar 09 '14 at 14:40

1 Answers1

0

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;
}
Community
  • 1
  • 1
thoughtcrime
  • 293
  • 3
  • 9
  • Thank you for help me thoughtcrime. – raffaele Mar 09 '14 at 18:15
  • If this is answers your question, can you accept the answer so it won't show up as unanswered in the queue? Thanks! – thoughtcrime Mar 09 '14 at 19:42
  • Hi thoughtcrime, I have error: var fileBlob = e.parameter['file'+i]; Logger.log('fileBlob' + fileBlob); say: fileBlob undefined. You can see? thank you – raffaele Mar 12 '14 at 07:35
  • Double check your code against mine for typos. It sounds like you are creating a fileBlob, it's getting assigned to i, then in that doPost line that is throwing the error you are adding fileblobs to each other, or numFiles is being considered a fileBlob, which means that the hidden textbox has a fileblob associated with it instead of a number. – thoughtcrime Mar 12 '14 at 11:20
  • I have copy and past originale code. You can see? : https://sites.google.com/a/paparella.it/uploadfile/upload-file-2 – raffaele Mar 12 '14 at 20:02
  • I see where you've added the code, verbatim from this site to that iframe, but I can't see the actual code that is running behind the app. I get the error, but can't see why. Copy the code from my answer and the code you have that is actually running your script into http://text-compare.com/ and look to see if there are any differences. There will at least be differences in this line: var newFile = DocsList.getFolderById("YOUR FILE FOLDER ID").createFile(fileBlob);//replace string with folder id where you want to place your files } because you have to change the folder Id to one of yours – thoughtcrime Mar 12 '14 at 20:08
  • I inserted id folder and is the only difference detected by text-compare.com you see in the image [https://sites.google.com/a/paparella.it/uploadfile/img. This error can be overcome by canceling var newFile = DocsList.getFolderById("YOUR FILE FOLDER ID").createFile(fileBlob); and adding: var = docfile DocsList.createFile (fileBlob);      var folder = DocsList.getFolder (FolderName);          docFile.addToFolder (folder); If I make this change, as I told you I get the error reported (var fileBlob e.parameter = ['file' + i]; Logger.log ('fileBlob' + fileBlob) say: fileBlob undefined.). – raffaele Mar 13 '14 at 07:45
  • I edited the file to fix it. I messed up when I was trimming down some lines, and took out the uploadWidget.setId and setName lines on line 36 and 37 before I posted my answer. I tested it after the fix and it works. Just copy the code from the answer again and change the file Id, and you should be good to go. – thoughtcrime Mar 13 '14 at 12:07
  • OK thoughtcrime now it's work fine. thanks for your patience and availability. – raffaele Mar 13 '14 at 14:12
  • I've updated the script handling the selection of a master folder where to upload the files. See https://sites.google.com/a/paparella.it/uploadfile/upload-file-2 here. Missing 2 points if you want to do it. I tried but did not succeed. 1. check that the selected folder. 2. check that a file is selected before activating the "Upload File (s). – raffaele Mar 13 '14 at 17:41
  • Add validation to the fileUpload widget and disable the button until a file is selected. The original script I modified my answer from uses validation on text boxes. The link to that script is my first commented line at the top of the script. See if you can figure out how to add validation to activate the button there. – thoughtcrime Mar 13 '14 at 17:49