将文件保存到指定的文件夹

时间:2014-09-05 17:16:52

标签: google-apps-script

我有一个脚本,它使用表单和键值中的数据来填写文档并将文档转换为pdf。现在我无法将pdf保存到我的google驱动器中的指定文件夹。

function myfunction(e) {
  var docTemplate = "1pKwW-RcjaVV8xmm00hWDx1u2QxzQwaSjPJwVY2ux2nI";
var docName = "Form"; 
var full_name = "Form"; 
var targetFolderId = "Work Order Forms";

function onFormSubmit(e) { 
  var Assign_To_1 = e.value[2];
  var Assign_To_2 = e.value[3];
  var Timestamp = e.values[0];
  var Date_Of_Request = e.value[1];
  var Requested_By = e.value[7];
  var Principal_Approval = e.value[5];
  var Urgency = e.value[4];
  var Building_Room_Number = e.value[10];
  var Description_of_Work_To_Be_Done = e.value[6];
  var Parts_Needed = e.value[8];
  var Parts_In_Stock = e.value[9];
  var Invoice_Number = e.value[10];
  var Completed_Signature = e.value[22];
  var Incomplete_Signature = e.value[23];
  var On_Hold_Signature = e.value[24];
  var Estimated_Hours = e.value[14];
  var Number_Of_Employees_To_Complete = e.value[18];
  var Budget = e.value[11];
  var Follow_Up = e.value[16];
  var Overtime = e.value[15];
  var copyId = DocsList.getFileById(docTemplate)
  .makeCopy(docName+' for '+full_name)
  .getId();
  var copyDoc = DocumentApp.openById(copyId);
  var copyBody = copyDoc.getActiveSection();
  copyBody.replaceText('KeyAssignedTo1',Assign_To_1);
  copyBody.replaceText('KeyAssignedTo2',Assign_To_2);
  copyBody.replaceText('KeyTimestamp',Timestamp);
  copyBody.replaceText('KeyDateOfRequest',Date_Of_Request);
  copyBody.replaceText('KeyRequestedBy',Requested_By); 
  copyBody.replaceText('KeyPrincipalApproval',Principal_Approval);
  copyBody.replaceText('KeyUrgency',Urgency);
  copyBody.replaceText('KeyBuildingRoomNumber',Building_Room_Number);
  copyBody.replaceText('KeyDescriptionofWorkToBeDone',Description_of_Work_To_Be_Done);
  copyBody.replaceText('KeyPartsNeeded', Parts_Needed);
  copyBody.replaceText('KeyPartsInStock', Parts_In_Stock);
  copyBody.replaceText('KeyInvoiceNumber', Invoice_Number);
  copyBody.replaceText('KeyCompletedSignature', Completed_Signature);
  copyBody.replaceText('KeyIncompleteSignature', Incomplete_Signature);
  copyBody.replaceText('KeyOnHoldSignature', On_Hold_Signature);
  copyBody.replaceText('KeyEstimatedHours', Estimated_Hours);
  copyBody.replaceText('KeyNumberOfEmployeesToComplete', Number_Of_Employees_To_Complete);
  copyBody.replaceText('KeyBudget', Budget);
  copyBody.replaceText('KeyFoolowUp', Follow_Up);
  copyBody.replaceText('KeyOvertime', Overtime);
  copyDoc.saveAndClose();
  var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
  var doc = DocsList.getFileById(copyId);
 function moveFileToFolder(fileId, targetFolderId) {
  var targetFolder = DocsList.getFolderById(targetFolderId);
  var file = DocsList.getFileById(fileId);
  file.addToFolder(targetFolder);
 }}}

到目前为止,这是我的代码。除了保存在指定的文件夹部分外,一切正常。请帮助我对此很新。

1 个答案:

答案 0 :(得分:0)

请参阅此旧帖子:https://stackoverflow.com/questions/13532114/google-apps-script-addtofolder-adds-to-folder-and-mydrive/13536275#13536275

代码如下:

function moveFromRoot(){
folder=DocsList.createFolder("MyFolder");
var file=DocsList.createFile('File2', 'Empty');
file.addToFolder(folder);
file.removeFromFolder(DocsList.getRootFolder());
}

但如答案中所述,您可以使用文件夹对象本身创建文件(folder.createFile('blabla')