我到處尋找這個問題的答案,但似乎無法找到它。
簡而言之,我想將所有檔案和檔案夾權限重置為“受限”。在整個驅動器上。
谷歌驅動器上的權限不會級聯,因此從父檔案夾中洗掉它們不會在子檔案夾和檔案中洗掉它們。而且我有大量的檔案和檔案夾。
我發現了一個帶有腳本的過時帖子,但不推薦使用“Uiapp”功能(評論中有人說了一些關于它的內容可能會洗掉一些檔案,所以不是我真正想要的)
這是類似(不再作業)的過時帖子:https : //webapps.stackexchange.com/questions/37592/how-do-i-reset-permissions-for-google-drive-documents-in-all-子檔案夾
uj5u.com熱心網友回復:
回答:
您可以使用 Drive API 檢索所有檔案,然后回圈訪問它們,將權限設定為PRIVATE.
更多資訊:
這里最棘手的部分是您擁有的檔案數量無疑意味著腳本執行時間將遠遠超過 Apps 腳本限制。但是,您可以通過最初存盤所有檔案 ID,然后回圈遍歷它們,從上次執行時中斷的地方繼續來解決此問題。
代碼:
由于 Drive API 比內置的快DriveApp,我將在我的示例中使用它。確保在運行前啟用 Drive Advanced Service。
function getAllFileIds() {
const fileIds = []
let files = Drive.Files.list({
pageSize: 1000
})
while (files.nextPageToken) {
console.log(files.nextPageToken)
fileIds.push(...files.items.map(x => x.id))
files = Drive.Files.list({
pageSize: 1000,
pageToken: files.nextPageToken
})
}
fileIds.push(...files.items.map(x => x.id))
const ss = SpreadsheetApp.create("Files List")
const sheet = ss.getSheetByName("Sheet1")
sheet.getRange(1, 1, fileIds.length, 1).setValues(fileIds.map(x => [x]))
PropertiesService.getScriptProperties().setProperty("sheet", ss.getId())
}
function changePermissions() {
const props = PropertiesService.getScriptProperties()
const sheet = SpreadsheetApp.openById(props.getProperty("sheet")).getSheetByName("Files List")
let ids
const curr = parseInt(props.getProperty("index"))
if (curr) {
ids = sheet.getRange(curr, 1, sheet.getLastRow() - curr, 1).getValues().flat(2)
}
else {
ids = sheet.getDataRange().getValues().flat(2)
}
ids.forEach(function(id, index) {
try {
console.log(id)
DriveApp.getFileById(id).setSharing(DriveApp.Access.PRIVATE, DriveApp.Permission.VIEW)
}
catch(e) {
console.info(e)
}
props.setProperty("index", index 1)
})
}
這個想法是這樣的:
- 運行函式
getAllFileIds()。這會: - 使用 Drive API 一次最多可檢索 1000 個檔案,并將它們的所有 ID 添加到一個陣列中。
- 創建一個電子表格來保存所有的 ID。
- 將電子表格 ID 保存到腳本的屬性以供以后訪問。
我選擇記錄每個檔案,nextPageToken以便您可以看到它的運行情況 - 我的云端硬碟中有大約 16,800 個檔案和檔案夾,此功能需要一分鐘左右的時間才能運行。
然后,您可以運行該功能changePermissions()。這會:
- 檢查電子表格 ID 的腳本屬性和已完成行數的索引
- 打開電子表格
- 如果有保存的
index值,則腳本從上次停止的地方開始 - 如果沒有保存的
index值,腳本從串列的開頭開始 - 按 ID 回圈遍歷每個檔案并將 Access 設定為
PRIVATE. - 每次更改權限時,新索引都會保存到腳本屬性中。
You will have to run this multiple times. I suggest creating a time based trigger so that the function runs multiple times without the need to manually re-run. You can find the instructions for this in this answer.
NB: Apps Script execution time limit is 6 minutes for Gmail accounts and 30 minutes for Business/Enterprise. Please bear this in mind when setting up an installable trigger so that individual script executions do not overlap.
I hope this is helpful to you!
References:
- Files:list | Google Drive API | Google Developers
- Class SpreadsheetApp | Apps Script | Google Developers
- Class PropertiesService | Apps Script | Google Developers
- Class DriveApp | Apps Script | Google Developers
- Class File | Apps Script | Google Devlopers
- Enum Permission | Apps Script | Google Developers
- Enum Access | Apps Script | Google Developers
- Installable Triggers | Apps Script | Google Developers
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/327417.html
上一篇:檢查幻燈片文本框的字串相等性
