在特定文件夹中创建Google文档

时间:2013-03-02 23:56:55

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

我正在尝试在变量chapterOne中保存的特定文件夹中创建一个google文档。

我用Google搜索并用Google搜索,我找到答案的最接近的答案就是这里的代码:

function Test() {
  DocsList.createFolder('Folder1').createFolder('Subfolder1').createFile('File1', 'Empty');
}

但问题是它创建了一个文件,而不是谷歌文档。我正在寻找的东西会以某种方式调用DocumentApp ......

非常感谢任何帮助!

〜诺勒

2 个答案:

答案 0 :(得分:1)

您可能希望使用DocumentApp创建文档,然后将其移动到其他文件夹。

var docName = "YOUR_DOC_NAME";
var doc = DocumentApp.create(docName); //this creates the doc in the root folder
var file = DocsList.getFileById(doc.getId());
file.removeFromFolder(DocsList.getRootFolder());
file.addToFolder(DocsList.getFolder("path/to/folder"));

答案 1 :(得分:0)

DocsList现在已贬值,因此对于可能遇到此线程的任何人,这里是更新的答案:

function createDoc() {
  // Creates doc in root directory
  var doc = DocumentApp.create('MyDocument');
  var docFile = DriveApp.getFileById(doc.getId());

  // Copy doc to the directory we want it to be in. Delete it from root.
  DriveApp.getFoldersByName('Subfolder1').next().addFile(docFile);
  DriveApp.getRootFolder().removeFile(docFile);

  // Returns the ID of the newly created Doc
  return DriveApp.getFilesByName('MyDocument').next().getId();
}

这是我找到此答案的帖子的链接:Create a Google Doc file directly in a Google Drive folder