以下のnoteを参照。
Instantly share code, notes, and snippets.
Last active
December 29, 2020 07:45
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save W-Yoshida/b6c86b2209678d04dbe117dca98e65d2 to your computer and use it in GitHub Desktop.
Google共有ドライブを作成、メンバー変更するGAS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var createDriveSheet = ss.getSheetByName("共有ドライブ作成"); | |
var updateDriveMemberSheet = ss.getSheetByName("共有ドライブ権限変更"); | |
//実行メニューを作成 | |
function onOpen() { | |
var ui = SpreadsheetApp.getUi(); | |
var menu = ui.createMenu("GAS実行"); | |
menu.addItem("共有ドライブ作成", "createSharedDrive"); | |
menu.addItem("共有ドライブメンバー変更", "updateDriveMember"); | |
menu.addToUi(); | |
} | |
//Drive API v2を使用 | |
function createSharedDrive() { | |
var lastColum = createDriveSheet.getLastColumn(); | |
var lastRow = createDriveSheet.getLastRow(); | |
var startRow = 2; | |
var numRows = lastRow - startRow + 1; | |
var numColumns = lastColum ; | |
var dataRange = createDriveSheet.getRange(startRow, 1, numRows, numColumns); | |
var data = dataRange.getValues(); | |
for (var i = 0; i < data.length; ++i) { | |
var row = data[i]; | |
row.rowNumber = i + 2; | |
//Result列がブランクであれば処理を実行 | |
if (!row[numColumns - 1]) { | |
var result = ""; | |
try | |
{ | |
//同名の共有ドライブが作成できてしまうので、既に存在しないかチェックする。 | |
var driveID = searchDriveIDbyName(row[0]); | |
if(driveID=="Error") | |
{ | |
throw "共有ドライブIDの取得でエラーが発生しました"; | |
} | |
else if(driveID=="Duplicate"){ | |
throw "同じ名前の共有ドライブが複数存在します"; | |
} | |
else if(driveID=="Nothing") | |
{ | |
var insertResources = { | |
"name": row[0], | |
}; | |
//共有ドライブ設定の引数チェック | |
var isAdminManagedRestrictions; | |
var isDomainUsersOnly; | |
var isDriveMembersOnly; | |
var isCopyRequiresWriterPermission; | |
if(row[1] == "true"){ | |
isAdminManagedRestrictions = true; | |
}else if(row[1] == "false") | |
{ | |
isAdminManagedRestrictions = false; | |
}else{ | |
throw "引数の設定に誤りがあります"; | |
} | |
if(row[2] == "true"){ | |
isDomainUsersOnly = true; | |
}else if(row[2] == "false") | |
{ | |
isDomainUsersOnly = false; | |
}else{ | |
throw "引数の設定に誤りがあります"; | |
} | |
if(row[3] == "true"){ | |
isDriveMembersOnly = true; | |
}else if(row[3] == "false") | |
{ | |
isDriveMembersOnly = false; | |
}else{ | |
throw "引数の設定に誤りがあります"; | |
} | |
if(row[4] == "true"){ | |
isCopyRequiresWriterPermission = true; | |
}else if(row[4] == "false") | |
{ | |
isCopyRequiresWriterPermission = false; | |
}else{ | |
throw "引数の設定に誤りがあります"; | |
} | |
var updateOptions = { | |
"restrictions": { | |
"adminManagedRestrictions": isAdminManagedRestrictions, | |
"domainUsersOnly": isDomainUsersOnly, | |
"driveMembersOnly": isDriveMembersOnly, | |
"copyRequiresWriterPermission" : isCopyRequiresWriterPermission | |
} | |
}; | |
//共有ドライブを作成 | |
//requestIdが必須なので、UUIDを発行して渡す | |
var uuid = Utilities.getUuid(); | |
//実行するユーザーに共有ドライブ作成を管理コンソールの設定で許可しておかないとエラーになるので注意 | |
var res = Drive.Drives.insert(insertResources, uuid); | |
//共有ドライブの設定を更新 | |
//共有ドライブの設定権限がないユーザーで実行すると、エラーが出ずに変更もされていないので注意 | |
var updateResources = { | |
"useDomainAdminAccess": true | |
}; | |
var res_update = Drive.Drives.update(updateResources, res.id, updateOptions); | |
result = "Success"; | |
}else | |
{ | |
result = "同じ名前の共有ドライブが既に存在します"; | |
} | |
}catch(e){ | |
result = "Error:" + e; | |
} | |
//実行結果をResult列にセット | |
createDriveSheet.getRange(row.rowNumber, numColumns).setValue(result); | |
} | |
} | |
} | |
function updateDriveMember() { | |
var lastColum = updateDriveMemberSheet.getLastColumn(); | |
var lastRow = updateDriveMemberSheet.getLastRow(); | |
var startRow = 2; | |
var numRows = lastRow - startRow + 1; | |
var numColumns = lastColum ; | |
var dataRange = updateDriveMemberSheet.getRange(startRow, 1, numRows, numColumns); | |
var data = dataRange.getValues(); | |
for (var i = 0; i < data.length; ++i) { | |
var row = data[i]; | |
row.rowNumber = i + 2; | |
//Result列がブランクであれば処理を実行 | |
if (!row[numColumns - 1]) { | |
var result = ""; | |
try | |
{ | |
//共有ドライブ名からのドライブIDを取得 | |
var driveID = searchDriveIDbyName(row[0]); | |
if(driveID=="Error") | |
{ | |
result = "ドライブIDの取得でエラーが発生しました"; | |
} | |
else if(driveID=="Duplicate"){ | |
result = "同じ名前の共有ドライブが複数存在します"; | |
} | |
else if(driveID=="Nothing") | |
{ | |
result = "対象の共有ドライブが存在しません"; | |
}else | |
{ | |
if(row[2]=="add") | |
{ | |
//対象の共有ドライブにメンバーを追加 | |
//グループではなくユーザーで決め打ち | |
//https://developers.google.com/drive/api/v3/reference/permissions/create | |
var member = { | |
"value": row[1], | |
"type": "user", | |
"role": row[3], | |
}; | |
var options = { | |
"sendNotificationEmails" : false, | |
"supportsAllDrives": true, | |
"supportsTeamDrives": true, | |
"useDomainAdminAccess": true, | |
}; | |
Drive.Permissions.insert(member, driveID, options); | |
result = "Success"; | |
} | |
else if (row[2]=="remove") | |
{ | |
//対象の共有ドライブのPermissionsを取得 | |
var listOptions = { | |
"supportsAllDrives": true, | |
"supportsTeamDrives": true, | |
"useDomainAdminAccess": true, | |
}; | |
var permissionList = Drive.Permissions.list(driveID, listOptions); | |
//対象のメンバーを含むPermissionのみを抽出 | |
var permission = permissionList.items.filter(function(perm) { | |
return perm.emailAddress == row[1]; | |
}); | |
//対象の共有ドライブからメンバーを削除 | |
var removeOptions = { | |
"sendNotificationEmails" : false, | |
"supportsAllDrives": true, | |
"supportsTeamDrives": true, | |
"useDomainAdminAccess": true, | |
}; | |
Drive.Permissions.remove(driveID, permission[0].id,removeOptions); | |
result = "Success"; | |
}else | |
{ | |
result = "Actionの指定が正しくありません"; | |
} | |
} | |
}catch(e){ | |
result = "Error:" + e; | |
} | |
//実行結果をResult列にセット | |
updateDriveMemberSheet.getRange(row.rowNumber, numColumns).setValue(result); | |
} | |
} | |
} | |
//引数で受け取った文字列の共有ドライブが存在する場合は"driveID"を、 | |
//ドライブが存在しない場合は"Nothing"、 | |
//複数のドライブが存在する場合は"Duplicate"、 | |
//エラーが発生した場合は"Error"を返す | |
function searchDriveIDbyName(driveName) { | |
var driveID = ''; | |
try | |
{ | |
var options = { | |
"q": "name='" + driveName + "'", | |
"useDomainAdminAccess": true, | |
}; | |
var driveList = Drive.Drives.list(options); | |
if(driveList.items.length==1) | |
{ | |
driveID = driveList.items[0].id; | |
} | |
else if(driveList.items.length==0) | |
{ | |
driveID ="Nothing"; | |
} | |
else | |
{ | |
driveID ="Duplicate"; | |
} | |
}catch(e){ | |
driveID = "Error"; | |
} | |
return driveID | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment