直接在Google云端硬盘文件夹中创建Google文档文件

时间:2015-07-31 06:43:50

标签: google-apps-script google-drive-api google-docs

如何在Google云端硬盘文件夹中创建Google文档?

我知道我可以在这样的文件夹中创建一个文件。

var folder = DriveApp.createFolder("A Folder")
folder.createFile("filename.file", "Some text")

但我如何使用Google Apps脚本在文件夹中创建文档

6 个答案:

答案 0 :(得分:18)

The other answer is correct but the file will exist in your folder AND in your "drive" folder (the root folder).

To avoid that, simply remove it from there !

code :

  var doc = DocumentApp.create('Document Name'),
      docFile = DriveApp.getFileById( doc.getId() );
  DriveApp.getFolderById('0B3°°°°°°°1lXWkk').addFile( docFile );
  DriveApp.getRootFolder().removeFile(docFile);

答案 1 :(得分:8)

对于文档,它有点不同,首先创建它然后移动到文件夹:

var doc = DocumentApp.create('Document Name'),
      docFile = DriveApp.getFileById( doc.getId() );

DriveApp.getFolderById(foldId).addFile( docFile );

答案 2 :(得分:1)

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

请注意,此代码不会 首先创建一个Doc文件,将其移动,然后在根驱动器中删除该文件。此代码直接在文件夹中创建一个新的Google Doc类型文件。

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

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

}

答案 3 :(得分:1)

Serge insas 的回答对我有所帮助,但这些方法现已在 2021 年弃用。以下是对我有用的方法。

var folder = 'Your Folder Id'
var title = 'Your File Name'    
var doc = DocumentApp.create(title);
docFile = DriveApp.getFileById(doc.getId());
docFile.moveTo(folder);

答案 4 :(得分:0)

添加到@Kriggs答案。

要从根文件夹中删除:

DriveApp.getRootFolder().removeFile(file)

否则,它将位于根级别新位置。

enter image description here

答案 5 :(得分:0)

var folder = DriveApp.createFolder ("A Folder");
var file = DriveApp.createFile ("filename.file", "Some text");
file.moveTo (folder);