title: How can I generate a new G-Drive folder and duplicate template files upon a Google Form submission
body: <p>I've created a Google Form which will capture details of upcoming projects. I am trying to write a script so that when a new Google Form is submitted (i.e. a new project is added) two things happen;</p>
  
  <p>1) A new folder is created and named using the project code and project title from the Google Form spreadsheet
  2) A number of generic template files (Google Docs) will be copied and placed inside this new folder and certain fields will be auto-filled from the spreadsheet.</p>
  
  <p>I've done my best to cobble together code from a number of different places and I've come close a few times. I am fairly new to Google Apps Script and coding in general so I've hit a bit of a brick wall!</p>
  
  <pre><code>function createProjectFolder() {
  
  // identify the sheet where the data resides
  var ss = SpreadsheetApp.openById("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  SpreadsheetApp.setActiveSpreadsheet(ss);
  
  //identify the cells that will be used to name things
  
  var timestamp = sheet.getRange(sheet.getLastRow(), 1).getValue(); 
  var projectCode = sheet.getRange(sheet.getLastRow(), 2).getValue(); 
  var learningArea = sheet.getRange(sheet.getLastRow(), 3).getValue(); 
  var projectTitle = sheet.getRange(sheet.getLastRow(), 4).getValue(); 
  var extendedTitle = sheet.getRange(sheet.getLastRow(), 5).getValue(); 
  var elevatorPitch = sheet.getRange(sheet.getLastRow(), 6).getValue(); 
  var downloadableDescription  = sheet.getRange(sheet.getLastRow(), 7).getValue();
  var tags = sheet.getRange(sheet.getLastRow(), 8).getValue(); 
  
  //identify the parent folder the new folder will be in 
  var parentFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  
  //create the new folder 
  var newFolder = parentFolder.createFolder(projectCode + " - " + projectTitle);
  
  }
  
  function createGoogleDoc() {
  
  // identify the sheet where the data resides 
  var ss = SpreadsheetApp.openById("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
  SpreadsheetApp.setActiveSpreadsheet(ss);
  
  //identify the cells that will be used to name things
  var timestamp = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange(ss.getLastRow(), 1).getValue();
  var projectCode = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange(ss.getLastRow(), 2).getValue();
  var learningArea = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange(ss.getLastRow(), 3).getValue();
  var projectTitle = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange(ss.getLastRow(), 4).getValue();
  var extendedTitle = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange(ss.getLastRow(), 5).getValue();
  var elevatorPitch = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange(ss.getLastRow(), 6).getValue();
  var downloadableDescription  = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange(ss.getLastRow(), 7).getValue();
  var tags = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange(ss.getLastRow(), 8).getValue();
  
  //file is the template file, and you get it by ID
  var file = DriveApp.getFileById('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); 
  
  //We can make a copy of the template, name it, and optionally tell it what folder to live in
  //file.makeCopy will return a Google Drive file object
  var folder = DriveApp.getFolderById('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
  var copy = file.makeCopy(projectCode + " - " + projectTitle, folder);
  
  //Once we've got the new file created, we need to open it as a document by using its ID
  var doc = DocumentApp.openById(copy.getId());
  
  //Since everything we need to change is in the body, we need to get that
  var body = doc.getBody();
  
  //Then we call all of our replaceText methods
  body.replaceText('{{Project Code}}', projectCode); 
  body.replaceText('{{Project Title}}', projectTitle); 
  body.replaceText('{{Extended Title}}', extendedTitle);
  body.replaceText('{{Learning Area}}', learningArea);
  body.replaceText('{{Elevator Pitch}}', elevatorPitch);
  body.replaceText('{{Tags}}', tags);
  
  //Since everything else we need to change is in the header, we need to get that
  var header = doc.getHeader();
  
  //Then we call all of our replaceText methods
  header.replaceText('{{Project Code}}', projectCode); 
  header.replaceText('{{Project Title}}', projectTitle);
  header.replaceText('{{Learning Area}}', learningArea);
  
  //Lastly we save and close the document to persist our changes
  doc.saveAndClose(); 
  }
  </code></pre>
  
  <p>A new folder is generating but it is coming up with "  -  "  instead of the project code and project name. I suspect it is something to do with "getLastRow" but I'm not 100% sure. Also not sure if I need a new function for the 'createGoogleDoc' part of if it can be combined with the first. I'm also really unsure how to identify a folder that has just been created! Again, very new to this so apologies if it's a bit of a dog's breakfast.</p>