Google Apps 脚本将文件移动到 Team Drive 文件夹
Google Apps Script moving file to Team Drive folder
背景:
问题的核心是我有一个 Alteryx 作业将文件放入我的 google 驱动器。实际上,这些文件需要位于 Team Drive 文件夹中。尽我所能,我在任何地方都找不到让 Alteryx 做到这一点的方法。因此需要这个脚本。
实际问题:
所以这里是标准:我使用相同的命名约定创建文件,只是日期发生变化。我需要这些文件从我的驱动器转到最终处理它们的团队驱动器。
使用堆栈上已有的资源,我在这里找到了很好的解决方案: and here 2 我能够拼凑出一个工作脚本。
了解我是一个边缘功能 python 数据分析程序员。 所以我的 JS 和 Google 脚本充其量只是初级的。我第一次测试脚本时,它起作用了。奇妙的是,直到它没有。
它毫无问题地移动了我的第一个文件。然后我在驱动器中创建了同一个文件的几个副本,看看它如何处理多个。我现在收到一个错误:
Exception: No item with the given ID could be found, or you do not
have permission to access it. (line 15, file "CodeA1")
这是我的代码:
function SearchFiles() {
//searches based on naming criteria
var searchFor ='title contains "Reference Data Performance"'; //test file
var names =[];
var fileIds=[];
var files = DriveApp.searchFiles(searchFor);
while (files.hasNext()) {
var file = files.next();
var fileId = file.getId();// To get FileId of the file
fileIds.push(fileId);
var name = file.getName();
names.push(name);
}
var file = DriveApp.getFileById(fileIds);
supportsTeamDrives: true;
supportTeamDrives: true;
var targetFolder = DriveApp.getFolderById('TEAMDriveID');
targetFolder.addFile(file);
}
Exception: No item with the given ID could be found, or you do not
have permission to access it.
如果
,则此错误最常发生
TeamDriveId
不正确
- 您运行脚本所在的帐户不是团队驱动器的成员
Also note:
supportsTeamDrives: true;
supportTeamDrives: true;
are parameters
for the Drive API
, not to be confused with DriveApp
更新:
我最终选择了这个脚本的简化版本。我让 Alteryx 以小时为增量安排文件创建(三个不同的文件)。然后在 Google Apps 中触发 运行 脚本,直接在每次 Alteryx 计划下降后的时间。简单的?是的。不雅?可能是。 运行整整一周,文件到达目的地供人们处理。
function SearchFiles() {
//searches based on naming criteria
var searchFor ='title contains "Reference Data Performance"'; //looks for file that matches requirements
var files = DriveApp.searchFiles(searchFor);
while (files.hasNext()) {
var file = files.next();
var fileId = file.getId();// To get FileId of the file
var file = DriveApp.getFileById(fileId); //grabs file ready to pass to google drive.
supportsTeamDrives: true;
supportTeamDrives: true;
var targetFolder = DriveApp.getFolderById('FOLDERID');
targetFolder.addFile(file);//good for getting a single file needs loop to grab multiple from Array
}
}
背景: 问题的核心是我有一个 Alteryx 作业将文件放入我的 google 驱动器。实际上,这些文件需要位于 Team Drive 文件夹中。尽我所能,我在任何地方都找不到让 Alteryx 做到这一点的方法。因此需要这个脚本。
实际问题:
所以这里是标准:我使用相同的命名约定创建文件,只是日期发生变化。我需要这些文件从我的驱动器转到最终处理它们的团队驱动器。
使用堆栈上已有的资源,我在这里找到了很好的解决方案:
了解我是一个边缘功能 python 数据分析程序员。 所以我的 JS 和 Google 脚本充其量只是初级的。我第一次测试脚本时,它起作用了。奇妙的是,直到它没有。 它毫无问题地移动了我的第一个文件。然后我在驱动器中创建了同一个文件的几个副本,看看它如何处理多个。我现在收到一个错误:
Exception: No item with the given ID could be found, or you do not have permission to access it. (line 15, file "CodeA1")
这是我的代码:
function SearchFiles() {
//searches based on naming criteria
var searchFor ='title contains "Reference Data Performance"'; //test file
var names =[];
var fileIds=[];
var files = DriveApp.searchFiles(searchFor);
while (files.hasNext()) {
var file = files.next();
var fileId = file.getId();// To get FileId of the file
fileIds.push(fileId);
var name = file.getName();
names.push(name);
}
var file = DriveApp.getFileById(fileIds);
supportsTeamDrives: true;
supportTeamDrives: true;
var targetFolder = DriveApp.getFolderById('TEAMDriveID');
targetFolder.addFile(file);
}
Exception: No item with the given ID could be found, or you do not have permission to access it.
如果
,则此错误最常发生TeamDriveId
不正确- 您运行脚本所在的帐户不是团队驱动器的成员
Also note:
supportsTeamDrives: true; supportTeamDrives: true;
are parameters for the
Drive API
, not to be confused withDriveApp
更新:
我最终选择了这个脚本的简化版本。我让 Alteryx 以小时为增量安排文件创建(三个不同的文件)。然后在 Google Apps 中触发 运行 脚本,直接在每次 Alteryx 计划下降后的时间。简单的?是的。不雅?可能是。 运行整整一周,文件到达目的地供人们处理。
function SearchFiles() {
//searches based on naming criteria
var searchFor ='title contains "Reference Data Performance"'; //looks for file that matches requirements
var files = DriveApp.searchFiles(searchFor);
while (files.hasNext()) {
var file = files.next();
var fileId = file.getId();// To get FileId of the file
var file = DriveApp.getFileById(fileId); //grabs file ready to pass to google drive.
supportsTeamDrives: true;
supportTeamDrives: true;
var targetFolder = DriveApp.getFolderById('FOLDERID');
targetFolder.addFile(file);//good for getting a single file needs loop to grab multiple from Array
}
}