2

我想了解当您选择谷歌驱动器脚本http://www.google.com/script/start/时作为示例列出的 moveFileToFolder 脚本(单击“开始脚本”,然后在“创建脚本”中选择“驱动器”选项“ 菜单)

代码如下。我的问题是如何更改此代码,以便将文件“Austin”移动到文件夹“Texas”中?这是假设文件“奥斯汀”是一个谷歌文档,目前位于我的主谷歌驱动器文件列表中,文件夹“德克萨斯”目前也位于我的主谷歌驱动器列表中。

还有一些关于在驱动器中移动文件的其他帖子,我了解主要概念,但我似乎无法成功获取文件或文件夹 ID。任何帮助非常感谢?

/**
 * This script moves a specific file from one parent folder to another.
 * For more information on interacting with files, see
 * https://developers.google.com/apps-script/class_file
 */
function moveFileToFolder(fileId, targetFolderId) {
  var targetFolder = DocsList.getFolderById(targetFolderId);
  var file = DocsList.getFileById(fileId);
  file.addToFolder(targetFolder);
};
4

3 回答 3

4

现在我有同样的任务并使用了这段代码:

  var file = DriveApp.getFileById(fileId)
  var folder = DriveApp.getFolderById(folderId); 
  folder.addFile(file);
于 2016-10-07T13:46:43.513 回答
0

查看https://gist.github.com/suntong001/7955694getFolderByName中的函数,了解如何通过名称获取文件夹 ID。对于文件,原理是相同的。

于 2013-12-14T04:34:17.590 回答
0
function addDocToPublic() {
// Create random filename
var fn = "doc" + Math.floor(10000 * Math.random()) + ".doc";
// Create file with very MIME TYPE and return file ID
var fh = DriveApp.createFile(fn, "Das ist das " + fn + " Testfile", MimeType.MICROSOFT_WORD);
// Get first/next Iterator ID ( = folder ID) for Folder (with name PUBLIC)
var fid = DriveApp.getFoldersByName("PUBLIC").next();
// Copy File to Folder (using file and folder IDs
fh.makeCopy(fn,fid);
// Remove Source File
fh.setTrashed(true);
}

……也许那样……

于 2015-01-29T16:14:35.033 回答