如何在Google Apps Script中添加supportsAllDrives=true适配共享驱动器
解决方法:在Google Apps Script中使用
supportsAllDrives=true参数 你当前用的DriveApp是简化版服务,不支持共享驱动器的supportsAllDrives参数,必须切换到Advanced Drive Service的原生API方法来实现。下面针对你代码里的核心操作,给出具体修改方式:
1. 创建主文件夹(替换DriveApp.createFolder)
原来的DriveApp.createFolder(fldrname)要改成Drive.Folders.insert(),同时传入supportsAllDrives:true参数:
var fldrname = SpreadsheetApp.getActiveSpreadsheet().getRange("D2").getValue(); // 创建主文件夹,支持共享驱动器 var mainFolder = Drive.Folders.insert( {title: fldrname}, // 文件夹名称 null, // 父文件夹ID(这里创建根目录下的文件夹,传null) {supportsAllDrives: true} // 关键参数 ).id; var finaldestination = DriveApp.getFolderById(mainFolder); // 依然可以用DriveApp获取文件夹对象
2. 创建子文件夹(替换DriveApp.createFolder)
创建子文件夹时,需要指定父文件夹ID,同样用Drive.Folders.insert并带上参数:
// 示例:创建"0.FDOB Newsletter + UX Survey"文件夹,作为主文件夹的子文件夹 var destFolder = Drive.Folders.insert( {title: allFolders[0], parents: [{id: mainFolder}]}, // 指定父文件夹 null, {supportsAllDrives: true} ).id; // 创建"1. Communications Sent (Email,Template)"子文件夹 var fdob1 = Drive.Folders.insert( {title: allFolders[11]}, // 先创建在根目录,之后移动 null, {supportsAllDrives: true} ).id;
3. 移动文件夹(替换moveTo方法)
原来的moveTo方法要改成Drive.Folders.update(),更新文件夹的父目录,同时传入参数:
var fdobfldrs1 = DriveApp.getFolderById(fdob1); // 移动文件夹到目标目录 Drive.Folders.update( {parents: [{id: destFolder}]}, // 新的父文件夹ID fdobfldrs1.getId(), // 要移动的文件夹ID {supportsAllDrives: true} ); // 同理处理第二个文件夹 Drive.Folders.update( {parents: [{id: destFolder}]}, DriveApp.getFolderById(fdob2).getId(), {supportsAllDrives: true} );
完整修改后的代码片段
对应你提供的代码,修改后如下:
function siteBuild() { var fldrname = SpreadsheetApp.getActiveSpreadsheet().getRange("D2").getValue(); // 创建主文件夹(支持共享驱动器) var mainFolder = Drive.Folders.insert( {title: fldrname}, null, {supportsAllDrives: true} ).id; var finaldestination = DriveApp.getFolderById(mainFolder); //Creates First Layer of Folders var allFolders =["0.FDOB Newsletter + UX Survey","1.Cost Control","2.Network + Low Voltage","3.Audio Visual","4.Construction Docs","5.Schedules","1.Circuit Information","2.Design","3.Installation & Commissioning","4.Photos","5.As Builts","1. Communications Sent (Email,Template)","2. UX Survey and Results","Heatmaps","Final","Installation","Site Survey","1.Design","2.Installation & Commissioning","3.Photos","4.As Builts","Architecturals + Schematics","Programming Files"] // 创建第一个层级文件夹(作为主文件夹的子文件夹) var destFolder = Drive.Folders.insert( {title: allFolders[0], parents: [{id: mainFolder}]}, null, {supportsAllDrives: true} ).id; // 创建需要移动的子文件夹 var fdob1 = Drive.Folders.insert( {title: allFolders[11]}, null, {supportsAllDrives: true} ).id; var fdob2 = Drive.Folders.insert( {title: allFolders[12]}, null, {supportsAllDrives: true} ).id; // 移动文件夹到目标目录 Drive.Folders.update( {parents: [{id: destFolder}]}, fdob1, {supportsAllDrives: true} ); Drive.Folders.update( {parents: [{id: destFolder}]}, fdob2, {supportsAllDrives: true} ); }
核心逻辑就是:所有涉及共享驱动器的创建、移动操作,都替换为Advanced Drive Service的Drive.Folders.insert或Drive.Folders.update方法,并在第三个参数中传入{supportsAllDrives: true}。你可以按照这个模式,把整个脚本里的DriveApp相关操作都替换掉。
内容的提问来源于stack exchange,提问作者uhsleep
相关产品推荐
相关产品推荐

