andrefecto

Spreadsheet to Document Exporter for Google Script

Aug 12th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2.  
  3. /*
  4. Template Generator By: Andre Fecteau - klutch2013@gmail.com
  5. Original Code From: kiszal@gmail.com (Found in the template gallery by searching "Templates" It is the first One.
  6. Major Help from: Serge Insas On Stack Overflow (He did most of the work.)
  7. Link 1: http://stackoverflow.com/questions/18147798/e-undefined-google-script-error
  8. Link 2: http://stackoverflow.com/questions/18132837/have-a-listbox-populate-with-every-folder-in-mydrive
  9.  
  10. How To Use:
  11. First: each column is designated in your Template by {Column Letter} for example for column A it would be {A}
  12. Second: You can change this, but your Template must be in a folder called "Templates." This folder can be anywhere in your drive.
  13. Third: Click "Generate Documents Here!" Then click "Export Row to Document"
  14. Fourth: Type in the row you want to export. Chose your Folder Path. Click Submit.
  15. NOTE ON FOURTH STEP: If you want your number to skip the header row add a +1 to line 32.
  16. This would mean if you typed "2" in the row box it actually exports row 3. I took this out because it can get confusing at times.
  17.  
  18. NOTE: Line 71 you can edit the word "Templates" to whatever folder you saved your Template into.
  19. NOTE: Line 36 you can edit to change what comes up in the name of the file that is created. To do this just edit the column letter in the following piece: "+Sheet.getRange('E'+row).getValue()+"
  20. You can then delete any other columns you do not want by deleteing the section of code that looks like the following: '+Sheet.getRange('D'+row).getValue()+'
  21.  
  22. Feel free to edit this code as you wish and for your needs. That is what I did with the original code.
  23. So there is no reason I should restrict what others do with this code.
  24. */
  25.  
  26. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  27.  
  28. function generateDocument(e) {
  29.   var template = DocsList.getFileById(e.parameter.Templates);
  30.   Logger.log(template.getName());
  31.   var Sheet = SpreadsheetApp.getActiveSpreadsheet();
  32.   var row = Number(e.parameter.row)//+1 ; // Remove the // in this line next to the +1 to skip headers
  33.   Logger.log(row);
  34.   var currentFID = e.parameter.curFID;
  35.   Logger.log(currentFID);
  36.   var myDocID = template.makeCopy(Sheet.getRange('B'+row).getValue()+' - '+Sheet.getRange('E'+row).getValue()+' - '+Sheet.getRange('D'+row).getValue()+' - '+Sheet.getRange('X'+row).getValue()).getId();
  37.   var myDoc = DocumentApp.openById(myDocID);
  38.   var copyBody = myDoc.getActiveSection();
  39.   var Sheet = SpreadsheetApp.getActiveSpreadsheet();
  40.   row--; // decrement row number to be in concordance with real row numbers in sheet
  41.   var myRow = SpreadsheetApp.getActiveSpreadsheet().getRange(row+":"+row);
  42.   for (var i=1;i<Sheet.getLastColumn()+1;i++){
  43.     var myCell = myRow.getCell(1, i);
  44.     copyBody.replaceText("{"+myCell.getA1Notation().replace(row,"")+"}", myCell.getValue());
  45.   }
  46.  
  47.   myDoc.saveAndClose();
  48.   var destFolder = DocsList.getFolderById(currentFID);
  49.   Logger.log(myDocID);
  50.   var doc = DocsList.getFileById(myDocID);// get the document again but using docsList this time...
  51.   doc.addToFolder(destFolder);// add it to the desired folder
  52.   doc.removeFromFolder(DocsList.getRootFolder());// I did it step by step to be more easy to follow
  53.   var pdf = DocsList.getFileById(myDocID).getAs("application/pdf");
  54.   destFolder.createFile(pdf);// this will create the pdf file in your folder
  55.   var app = UiApp.getActiveApplication();
  56.   app.close();
  57.   return app;
  58. }
  59.  
  60. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61.  
  62. function getTemplates() {
  63.   var doc = SpreadsheetApp.getActiveSpreadsheet();
  64.   var app = UiApp.createApplication().setTitle('Generate from template');
  65.   // Create a grid with 3 text boxes and corresponding labels
  66.   var grid = app.createGrid(5, 2);
  67.   grid.setWidget(0, 0, app.createLabel('Template name:'));
  68.   var list = app.createListBox();
  69.   list.setName('Templates');
  70.   grid.setWidget(0, 1, list);
  71.   var docs = DocsList.getFolder("Templates").getFilesByType("document"); //Change the word "Templates" to whatever folder you saved your template into
  72.   for (var i = 0; i < docs.length; i++) {
  73.     list.addItem(docs[i].getName(),docs[i].getId());
  74.   }
  75.  
  76.   grid.setWidget(1, 0, app.createLabel('Row:'));
  77.   var row = app.createTextBox().setName('row');
  78.   row.setValue(SpreadsheetApp.getActiveSpreadsheet().getActiveRange().getRow());
  79.   grid.setWidget(1, 1, row);
  80.   var curFN = app.createTextBox().setText('MyDrive/').setName('curFN').setId('curFN').setWidth('400');
  81.   var curFID = app.createTextBox().setText(DocsList.getRootFolder().getId()).setName('curFID').setId('curFID').setVisible(false);
  82.   var listF = app.createListBox().setName('listF').setId('listF').addItem('Please Select Folder','x');
  83.   grid.setText(2,0,'Type Path:').setWidget(2,1,curFN).setText(3,0,'OR').setText(4,0, 'Choose Path:').setWidget(4,1,listF).setWidget(3,1,curFID);
  84.   var folders = DocsList.getRootFolder().getFolders();
  85.   for (var i = 0; i < folders.length; i++) {
  86.     listF.addItem(folders[i].getName(),folders[i].getId())
  87.   }
  88.  
  89.   var handlerF = app.createServerHandler('folderSelect').addCallbackElement(grid);
  90.   listF.addChangeHandler(handlerF);
  91.   var panel = app.createVerticalPanel();
  92.   panel.add(grid);
  93.   var button = app.createButton('Submit');
  94.   var handler = app.createServerClickHandler('generateDocument');
  95.   handler.addCallbackElement(grid);
  96.   button.addClickHandler(handler);
  97.   // Add the button to the panel and the panel to the application, then display the application app in the Spreadsheet doc
  98.   panel.add(button);
  99.   app.add(panel);
  100.   doc.show(app);
  101. }
  102.  
  103. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104.  
  105. function folderSelect(e){
  106.   var app = UiApp.getActiveApplication();
  107.   var currentFN = e.parameter.curFN;
  108.   var currentFID = e.parameter.listF;
  109.   Logger.log(currentFID);
  110.   var listF = app.getElementById('listF');
  111.   var curFN = app.getElementById('curFN');
  112.   var curFID = app.getElementById('curFID');
  113.   if(currentFID=='x'){currentFID=DocsList.getRootFolder().getId() ; curFN.setText('MyDrive/')};
  114.   var startFolder = DocsList.getFolderById(currentFID);
  115.   var folders = startFolder.getFolders();
  116.   listF.clear().addItem('No More Sub Folders!','x').addItem('Go back to Root','x');
  117.   if(folders.length>0){listF.clear(); listF.addItem('Select Sub Folder','x')};
  118.   for (var i = 0; i < folders.length; i++) {
  119.     listF.addItem(folders[i].getName(),folders[i].getId())
  120.   }
  121.  
  122.   curFN.setText(currentFN+DocsList.getFolderById(currentFID).getName()+'/');
  123.   if(currentFID==DocsList.getRootFolder().getId()){curFN.setText('MyDrive/')};
  124.   curFID.setText(currentFID);
  125.   return app;
  126. }
  127.  
  128. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  129.  
  130. function onOpen() {  
  131.   var ss = SpreadsheetApp.getActiveSpreadsheet();
  132.   var menuEntries = [{name: "Export Row to Document", functionName: "getTemplates"}];  
  133.   ss.addMenu("Generate Documents Here!", menuEntries);  
  134. }
Add Comment
Please, Sign In to add comment