0

I am trying to create a Google Doc based on form data. I have done this with other forms that take the Doc and save them as PDFs. With this script, I have been able to create new docs and rename the file, but I need to move them to a different folder. The code I used for doing this with PDFs works (at the very bottom) without problems but I can't figure out what I am doing wrong. I have not had any luck searching for working solutions online either.

var TEMPLATE_ID = 'template id number'; 
var folder = DriveApp.getFolderById('folder ID number');

// When Form Gets submitted
function onFormSubmit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheet = ss.getSheetByName("Goal Tracking"),
activeSheet = SpreadsheetApp.getActiveSheet(),
copyFile = DriveApp.getFileById(TEMPLATE_ID).makeCopy(),
copyId = copyFile.getId(),
copyDoc = DocumentApp.openById(copyId),
copyBody = copyDoc.getActiveSection(),
numberOfColumns = sheet.getLastColumn(),
activeRowIndex = sheet.getActiveRange().getRowIndex(),
activeRow = activeSheet.getRange(activeRowIndex, 1, 1, numberOfColumns).getValues(),
headerRow = activeSheet.getRange(1, 1, 1, numberOfColumns).getValues(),
columnIndex = 0,

fileName = "Goals " + activeRow[0][2] + ' ' + activeRow[0][3]+'_'+activeRow[0][5]; 

//Get information from form and set as variables.  

var lastName = e.values[2];
var firstName = e.values[3];
var programCode = e.values[4];
var goal1 = e.values[6];
var goal2 = e.values[7];
var goal3 = e.values[8];

// Replace place holder keys,in our google doc template

copyBody.replaceText('%First Name%', firstName);
copyBody.replaceText('%Last Name%', lastName);
copyBody.replaceText('%Program Code%', programCode);
copyBody.replaceText('%Goal Statement 1%', goal1);
copyBody.replaceText('%Goal Statement 2%', goal2);
copyBody.replaceText('%Goal Statement 3%', goal3);  

 // Save and close the temporary document

 copyDoc.saveAndClose();
 copyDoc.setName(fileName);

(continuing on)
Here is where the problem is

 folder.addFile(newFile);
  }

I do get an error in the execution transcript but I don't understand why I am getting it: 'Cannot find method addFile(Document)." I also tried to use:

    copyDoc.makeCopy(fileName, folder).getId;

to set a file name and folder location but it also failed.

For what its worth, here is what I have used to create a PDF that does work in this script.

copyDoc.saveAndClose();

var  pdfFile = DriveApp.createFile(copyFile.getAs("application/pdf"));
var  pdfFile2 = pdfFile.setName(fileName); 

 folder.addFile(pdfFile2); 
    copyFile.setTrashed(true);

Thanks :)

TimW
  • 11
  • 1
  • 3
  • Use a `Logger.log('folder: ' + folder);` statement right before the `folder.addFile(newFile);` statement. In the Logs, it should show **folder: folder** If the folder is null or undefined, that would cause the error. The only other possibility is that there is something wrong with the `newFile` parameter. I don't see where `newFile` is being set. – Alan Wells Mar 08 '16 at 17:32
  • Logger.log doesn't help, except it hows the correct folder in the log/transcript, so I assume it must be newFile parameter you mentioned. I did try to set the newFile parameter by creating: `var newFile = copyDoc.setName(fileName);` It does set the name but doesn't move the file to a different folder. The exeution transcript still says `Cannot find method addFile (document)` – TimW Mar 09 '16 at 14:36
  • I figured it out. Had to change `copyDoc.setName(fileName)` to `copyFile.setName(fileName)`. I can't say I fully understand why that is except I am a newbee anyway. Thanks for you help. – TimW Mar 09 '16 at 14:44
  • Always look at the return types returned from a method. The `setName()` method of the document Class returns a document, not a file. [Google documentation](https://developers.google.com/apps-script/reference/document/document#setnamename) The `setName()` method of the DriveApp class returns a file. – Alan Wells Mar 09 '16 at 15:34

1 Answers1

3

This code moves the folder, and rename the file.

function myFunction() {
  var dstFolderId = "0B1ECfqTCcLE8c09YZUtqTkVwU3c";

  var targetFileId = "0B1ECfqTCcLE8SS1PRnJ1cVgxVlk";
  var targetFile = DriveApp.getFileById(targetFileId);

  var newFileName = "HelloWorld.pdf";

  // Remove current parent folders
  var parentFolders = targetFile.getParents();
  while(parentFolders.hasNext()) {
    var parent = parentFolders.next();
    parent.removeFile(targetFile);
  }

  // Add the target file into the new folder
  var dstFolder = DriveApp.getFolderById(dstFolderId);
  dstFolder.addFile(targetFile);

  // Rename if you want.
  targetFile.setName(newFileName);
}
wf9a5m75
  • 6,100
  • 3
  • 25
  • 59