我正在尝试根据表单数据创建Google文档。我已经使用其他形式来完成此操作,将Doc文件另存为PDF文件。使用此脚本,我可以创建新文档并重命名该文件,但我需要将它们移动到其他文件夹。我用PDF做的代码工作(在最底层)没有问题,但我无法弄清楚我做错了什么。我也没有运气在线搜索工作解决方案。
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);
(继续)
这就是问题所在
folder.addFile(newFile);
}
我在执行记录中遇到错误,但我不明白为什么我会得到它:'找不到方法addFile(Document)。“我也尝试使用:
copyDoc.makeCopy(fileName, folder).getId;
设置文件名和文件夹位置,但也失败了。
为了它的价值,这里是我用来创建一个在这个脚本中工作的PDF。
copyDoc.saveAndClose();
var pdfFile = DriveApp.createFile(copyFile.getAs("application/pdf"));
var pdfFile2 = pdfFile.setName(fileName);
folder.addFile(pdfFile2);
copyFile.setTrashed(true);
谢谢:)
答案 0 :(得分:0)
此代码移动文件夹,并重命名该文件。
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);
}