我正在尝试使用Apps脚本将Google Apps脚本文件的副本放置在共享驱动器中。
我的代码如下:
function copyFileToSharedDrive(){
var sharedDriveId = "sharedriveidcomeshere";
var sharedDrive = DriveApp.getFolderById(sharedDriveId);
var appsScriptFileId = "appsscriptfileidcomeshere";
DriveApp.getFileById(appsScriptFileId).makeCopy(sharedDrive).setName("This is a copy of the original apps script file");
}
但是,结果是一份应用程序脚本文件的副本,但是它在我的Google云端硬盘的根文件夹中,而不是在共享驱动器中。
如果我使用电子表格,Google Doc或幻灯片做完全相同的事情,那么代码的工作原理就像一个超级按钮。
我还尝试了高级Google服务,并使用了Drive API。那里没有运气...文件仍在执行代码的用户的根文件夹中创建。
Drive.Files.copy(
{title: "This is a copy of the appsscript file", parents: [{id: sharedDriveId}]},
"appsScriptFileId",
{supportsAllDrives: true}
);
有帮助吗?
答案 0 :(得分:1)
这个答案怎么样?
makeCopy
将Google Apps脚本文件直接复制到共享驱动器。该文件被复制到MyDrive的根驱动器。我认为这可能是一个错误。那么在这种情况下,如何使用Drive API?修改脚本后,它如下所示。
运行脚本之前,please enable Drive API at Advanced Google services。
function copyFileToSharedDrive(){
var sharedDriveId = "sharedriveidcomeshere";
var appsScriptFileId = "appsscriptfileidcomeshere";
Drive.Files.copy(
{title: "This is a copy of the original apps script file", parents: [{id: sharedDriveId}]},
appsScriptFileId,
{supportsAllDrives: true}
);
}
为此,以下脚本如何?在此脚本中,使用以下流程。
function copyFileToSharedDrive(){
var destinationFolderId = "###"; // Please set the destination folder ID of shared Drive.
var appsScriptFileId = "###"; // Please set the GAS script ID.
var file = DriveApp.getFileById(appsScriptFileId)
var copiedFile = file.makeCopy().setName(file.getName()).getId();
Drive.Files.patch(
{title: "This is a copy of the original apps script file"},
copiedFile,
{removeParents: "root", addParents: destinationFolderId, supportsAllDrives: true}
);
}
关于supportsAllDrives
,the official document如下。
已弃用-请求的应用程序是否同时支持“我的驱动器”和共享驱动器。该参数仅在2020年6月1日之前有效。之后,假定所有应用程序都支持共享驱动器。
supportsAllDrives
,也可以使用共享的云端硬盘。