Copy, rename and move a document

2019-06-09 13:58发布

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 :)

1条回答
女痞
2楼-- · 2019-06-09 15:02

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);
}
查看更多
登录 后发表回答