如何在Google云端硬盘文件夹中创建Google文档?
我知道我可以在这样的文件夹中创建一个文件。
var folder = DriveApp.createFolder("A Folder")
folder.createFile("filename.file", "Some text")
但我如何使用Google Apps脚本在文件夹中创建文档。
答案 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。必须在两个地方启用它。
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)
答案 5 :(得分:0)
var folder = DriveApp.createFolder ("A Folder");
var file = DriveApp.createFile ("filename.file", "Some text");
file.moveTo (folder);