Advertisement
Guest User

export

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