【问题标题】:Create Google Doc file directly in folder rather than in Root folder using Google Apps Script使用 Google Apps 脚本直接在文件夹而不是根文件夹中创建 Google Doc 文件
【发布时间】:2018-11-27 14:05:15
【问题描述】:

我有这个运行良好的 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 Doc,其中包含来自表单的响应的电子表格中的内容。它与触发器一起工作,当表单发送时,文档就被制作了。

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

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

【问题讨论】:

标签: google-apps-script google-docs


【解决方案1】:

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

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

您需要使用 Advanced Drive API。它必须在两个地方启用。

  • 从代码编辑器中选择“资源”,然后选择“高级 Google 服务”
  • 点击“Drive API”按钮,使其处于开启状态。
  • 点击链接:Google Cloud Platform API Dashboard。
  • 搜索驱动器 API
  • 从搜索列表中选择 Google Drive API
  • 启用 Drive 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();

}

【讨论】:

  • 感谢您的帮助。不幸的是,我无法让您的解决方案起作用,但我找到了另一种方法。我制作了一个不同的脚本,在根目录中搜索具有正确名称的文档并将它们移动到正确的文件夹中。
  • 您应该发布您的解决方案作为答案。您可以回答自己的问题。
【解决方案2】:

我自己找到了答案。

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

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

    }

【讨论】:

    猜你喜欢
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 2014-08-05
    相关资源
    最近更新 更多