Google 脚本 API - 如何在特定文件夹中创建 google 文档
Google script API - How to create a google doc in a particular folder
基本上,这个问题给出了我想做的事情的答案:
Google script to create a folder and then create a Google Doc in that folder
但它已经 5 岁了,我想知道现在是否有更简单的方法。在我的例子中,我有很多现有的文件夹,我想通过一个每隔一段时间运行的脚本在每个文件夹中创建一个特定的文件。
谢谢。
编辑:
好的,当文件夹 'test folder' 位于共享驱动器中时,下面的代码可以正常工作(在答案中使用修改后的示例脚本 1)。
function testFunction() {
const notesFileName = 'test doc';
const folderName = 'test folder';
const query = "title = '" + folderName + "'";
// Find all folders that match the query.
var folders = DriveApp.searchFolders(query);
while (folders.hasNext()) {
// In all the sub-folders, of each found folder, create a file with the same name.
var folder = folders.next();
Logger.log("Found a folder: " + folder);
var subFolders = folder.getFolders();
while( subFolders.hasNext() ) {
var subFolder = subFolders.next();
Logger.log('Folder id: ' + subFolder.getId());
const doc = Drive.Files.insert({title: notesFileName, mimeType: MimeType.GOOGLE_DOCS, parents: [{id: subFolder.getId()}]}, null, {supportsAllDrives: true});
}
}
}
我相信你的目标如下。
- 您想创建新的 Google 文档到特定文件夹。
- 您有很多文件夹要创建新的 Google 文档。所以你想降低脚本的处理成本。
在这种情况下,我建议使用驱动器 API 创建新的 Google 文档。当使用 Drive API 时,每个文件夹的 Google 文档的创建可以 运行 使用异步过程。示例脚本如下
示例脚本 1:
在您使用此脚本之前,please enable Drive API at Advanced Google services。在此示例脚本中,新的 Google 文档循环创建到特定文件夹。
function sample1() {
const titleOfDocument = "sample document";
const folderIds = ["### folder ID1 ###", "### folder ID 2 ###",,,];
const documentIds = folderIds.map(id => Drive.Files.insert({title: titleOfDocument, mimeType: MimeType.GOOGLE_DOCS, parents: [{id: id}]}).id);
console.log(documentIds)
}
- 在此脚本中,返回创建的文档 ID。
- 如果您的实际情况下该脚本处理时间较长,请测试以下示例脚本2。
示例脚本 2:
在您使用此脚本之前,please enable Drive API at Advanced Google services。在此示例脚本中,使用 Google Apps 脚本库使用 Drive API 的批处理请求将新 Google 文档创建到特定文件夹。至此,任务运行与异步进程
请安装 Google Apps 脚本库以使用批处理请求。 Ref
function sample2() {
const titleOfDocument = "sample document";
const folderIds = ["### folder ID1 ###", "### folder ID 2 ###",,,];
const requests = {
batchPath: "batch/drive/v3",
requests: folderIds.map(id => ({
method: "POST",
endpoint: `https://www.googleapis.com/drive/v3/files`,
requestBody: {name: titleOfDocument, mimeType: MimeType.GOOGLE_DOCS, parents: [id]},
})),
accessToken: ScriptApp.getOAuthToken(),
};
const result = BatchRequest.EDo(requests); // Using GAS library
const documentIds = result.map(({id}) => id);
console.log(documentIds)
}
注:
对于上面的示例脚本,如果你想检索特定文件夹下的文件夹ID,你也可以使用for folderIds
如下。
const topFolderId = "### top folder ID ###";
const folders = DriveApp.getFolderById(topFolderId).getFolders();
const folderIds = [];
while (folders.hasNext()) {
folderIds.push(folders.next().getId());
}
顺便说一句,在当前阶段,为了移动文件,可以使用moveTo(destination). Using this, the script of this,可以修改如下。
function createDoc(folderID, name) {
var folder = DriveApp.getFolderById(folderID);
var doc = DocumentApp.create(name);
DriveApp.getFileById(doc.getId()).moveTo(folder);
return doc;
}
参考文献:
基本上,这个问题给出了我想做的事情的答案:
Google script to create a folder and then create a Google Doc in that folder
但它已经 5 岁了,我想知道现在是否有更简单的方法。在我的例子中,我有很多现有的文件夹,我想通过一个每隔一段时间运行的脚本在每个文件夹中创建一个特定的文件。
谢谢。
编辑:
好的,当文件夹 'test folder' 位于共享驱动器中时,下面的代码可以正常工作(在答案中使用修改后的示例脚本 1)。
function testFunction() {
const notesFileName = 'test doc';
const folderName = 'test folder';
const query = "title = '" + folderName + "'";
// Find all folders that match the query.
var folders = DriveApp.searchFolders(query);
while (folders.hasNext()) {
// In all the sub-folders, of each found folder, create a file with the same name.
var folder = folders.next();
Logger.log("Found a folder: " + folder);
var subFolders = folder.getFolders();
while( subFolders.hasNext() ) {
var subFolder = subFolders.next();
Logger.log('Folder id: ' + subFolder.getId());
const doc = Drive.Files.insert({title: notesFileName, mimeType: MimeType.GOOGLE_DOCS, parents: [{id: subFolder.getId()}]}, null, {supportsAllDrives: true});
}
}
}
我相信你的目标如下。
- 您想创建新的 Google 文档到特定文件夹。
- 您有很多文件夹要创建新的 Google 文档。所以你想降低脚本的处理成本。
在这种情况下,我建议使用驱动器 API 创建新的 Google 文档。当使用 Drive API 时,每个文件夹的 Google 文档的创建可以 运行 使用异步过程。示例脚本如下
示例脚本 1:
在您使用此脚本之前,please enable Drive API at Advanced Google services。在此示例脚本中,新的 Google 文档循环创建到特定文件夹。
function sample1() {
const titleOfDocument = "sample document";
const folderIds = ["### folder ID1 ###", "### folder ID 2 ###",,,];
const documentIds = folderIds.map(id => Drive.Files.insert({title: titleOfDocument, mimeType: MimeType.GOOGLE_DOCS, parents: [{id: id}]}).id);
console.log(documentIds)
}
- 在此脚本中,返回创建的文档 ID。
- 如果您的实际情况下该脚本处理时间较长,请测试以下示例脚本2。
示例脚本 2:
在您使用此脚本之前,please enable Drive API at Advanced Google services。在此示例脚本中,使用 Google Apps 脚本库使用 Drive API 的批处理请求将新 Google 文档创建到特定文件夹。至此,任务运行与异步进程
请安装 Google Apps 脚本库以使用批处理请求。 Ref
function sample2() {
const titleOfDocument = "sample document";
const folderIds = ["### folder ID1 ###", "### folder ID 2 ###",,,];
const requests = {
batchPath: "batch/drive/v3",
requests: folderIds.map(id => ({
method: "POST",
endpoint: `https://www.googleapis.com/drive/v3/files`,
requestBody: {name: titleOfDocument, mimeType: MimeType.GOOGLE_DOCS, parents: [id]},
})),
accessToken: ScriptApp.getOAuthToken(),
};
const result = BatchRequest.EDo(requests); // Using GAS library
const documentIds = result.map(({id}) => id);
console.log(documentIds)
}
注:
对于上面的示例脚本,如果你想检索特定文件夹下的文件夹ID,你也可以使用for
folderIds
如下。const topFolderId = "### top folder ID ###"; const folders = DriveApp.getFolderById(topFolderId).getFolders(); const folderIds = []; while (folders.hasNext()) { folderIds.push(folders.next().getId()); }
顺便说一句,在当前阶段,为了移动文件,可以使用moveTo(destination). Using this, the script of this,可以修改如下。
function createDoc(folderID, name) { var folder = DriveApp.getFolderById(folderID); var doc = DocumentApp.create(name); DriveApp.getFileById(doc.getId()).moveTo(folder); return doc; }