最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create a Google Doc in a Specific Folder - Stack Overflow

programmeradmin0浏览0评论

I am trying to create a google doc in a specific folder held in the variable chapterOne.

I've googled and googled and the closest I've found to an answer is this code here:

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

But the problem is that it creates a file, not a google doc. What I'm looking for would call DocumentApp somehow...

Any help would be greatly appreciated!

~Noelle

I am trying to create a google doc in a specific folder held in the variable chapterOne.

I've googled and googled and the closest I've found to an answer is this code here:

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

But the problem is that it creates a file, not a google doc. What I'm looking for would call DocumentApp somehow...

Any help would be greatly appreciated!

~Noelle

Share Improve this question asked Mar 2, 2013 at 23:56 nellygrlnellygrl 6573 gold badges16 silver badges34 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

DocsList is depreciated now, so for anyone who may e across this thread, here is an updated answer:

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

Here is a link to the post where I found this answer: Create a Google Doc file directly in a Google Drive folder

You will probably want to create the document using DocumentApp, then move it to a different folder.

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

Instead of creating a Google Doc in your Google Drive root and then moving to a specific folder, I was able to create a file directly in a specific Drive folder. But for this, I am maintaining an empty Google Document somewhere else.

function createNewDocInDriveFolder(){
    var emptyDocId = "XXX-XXXXXvqadhYQiYoAno8aam2HJhRNxgO84qF9VXXXX";
    var destFolderId = "XXXXPsu8uKcmkhJ1-oiSRNzE1-XXXX_XX"; 
    var destFolder = DriveApp.getFolderById(destFolderId);
    var newDocFile = DriveApp.getFileById(emptyDocId).makeCopy("New Doc", destFolder);     
    return DocumentApp.openById(newDocFile.getId());
}
发布评论

评论列表(0)

  1. 暂无评论