使用 Google Apps 脚本直接在文件夹而不是根文件夹中创建 Google Doc 文件

Create Google Doc file directly in folder rather than in Root folder using Google Apps Script

我有这个 Google Apps 脚本可以正常工作:

function myFunction(e) {

  var name = e.values[1];
  var kantoor = e.values[2];

  var doc = DocumentApp.create('Sleuteloverdracht ' + name);
  var body = doc.getBody();
  body.appendParagraph('Sleuteloverdracht ' + name);
  body.appendParagraph('Uw naam is '+ name + '\n' + 'U heeft de sleutel van kantoor '+ kantoor);
  doc.saveAndClose();

}

它制作了一个 Google 文档,其中包含电子表格中的内容,其中填充了来自表单的回复。它与触发器一起工作,当发送表单时生成文档。

但问题是 Google Doc 文件放在我的云端硬盘而不是与表格和电子表格相同的文件夹中。

编辑:这个Create a Google Doc file directly in a Google Drive folder主题中的问题不一样。我已经有一个 Doc,不想创建一个。现有的 Doc 只需要移动到另一个文件。有关我使用的解决方案,请参见下面的答案。

这是在特定文件夹中创建新 Google 文档的方法。创建Doc时,必须设置mime类型。

请注意,此代码 NOT 首先创建一个 Doc 文件,移动它,然后删除根驱动器中的文件。还有另一个流行的答案可以做到这一点。此代码直接在文件夹中创建一个新的 Google Doc 类型文件。

您将需要使用高级驱动器API。它必须在两个地方启用。

  • 从代码编辑器中选择 "Resources" 然后选择 "Advanced Google services"
  • 单击 "Drive API" 的按钮,使其处于打开状态。
  • 单击 link:Google 云平台 API 仪表板。
  • 搜索驱动器API
  • 从搜索列表中选择 Google 驱动器 API
  • 启用驱动器API

代码:

function myFunction(e) {
  var doc,fileResource,folder,ID_of_newFile,multipleFolders,newFile,
      newDocName,rng,sh,ss,ssFile,ssID;

  var name = e.values[1];
  var kantoor = e.values[2];

  newDocName = 'Sleuteloverdracht ' + name;//Set the name of the new file

  rng = e.range;//Get range in the spreadsheet that received the data
  sh = rng.getSheet();//Get sheet tab
  ss = sh.getParent();//Get spreadsheet
  ssID = ss.getSheetId();//Get file ID of spreadsheet - Integer

  ssFile = DriveApp.getFileById(ssID);//Get the spreadsheet file in order to get the
    //folder to put the new file in
  multipleFolders = ssFile.getParents();//Get all parent folders of the spreadsheet file

  folder = multipleFolders.next();//Get the folder of the spreadsheet

  fileResource = {
    'title': newDocName,
    "parents": [{'id':folder.getId()}],  //<--By setting this parent ID to the 
    //folder's ID, it creates this file in the correct folder
    'mimeType': 'application/vnd.google-apps.document'
  };

  newFile = Drive.Files.insert(fileResource);//Create the new document file
   // directly into the same folder as the spreadsheet
  ID_of_newFile = newFile.getId();

  //Logger.log(ID_of_newFile)

  doc = DocumentApp.openById(newFile.getId());//Open the new file as a Google document

  var body = doc.getBody();
  body.appendParagraph('Sleuteloverdracht ' + name);
  body.appendParagraph('Uw naam is '+ name + '\n' + 
    'U heeft de sleutel van kantoor '+ kantoor);
  doc.saveAndClose();

}

我自己找到了答案。

我使用这个脚本(它是我在网上找到的两个脚本的组合)来找到具有正确名称的文件并将它们删除到目标文件夹。之后,它会删除根目录中的文件。它与触发器一起使用,该功能每分钟触发一次,因为在我的例子中,创建文档(需要移动到目标文件夹)的表单每小时使用多次(创建多个文档)。

   function SearchFiles() {

      var searchFor ='title contains "NAME/LETTER"';
      var names =[];
      var fileIds=[];
      var files = DriveApp.searchFiles(searchFor);

      while (files.hasNext()) {
        var file = files.next();
        var fileId = file.getId(); 
        fileIds.push(fileId);
        var name = file.getName();
        names.push(name);


   }

      var docFile = DriveApp.getFileById(fileId);
      DriveApp.getFolderById('FOLDER_ID_DESTINATION_FOLDER').addFile(docFile);
      DriveApp.getRootFolder().removeFile(docFile);

    }