問題描述
我的腳本從工作表學生"中獲取 35 個學生姓名.然后找到帶有他們名字的正確文件夾(即'Smith Peter'),刪除舊的pdf文檔,其中舊成績保留他們的名字(即'Smith Peter.pdf')并將新制作的pdf保存到文件夾('Smith Peter')帶有新成績的文檔('Smith Peter.pdf').并從名單中帶走另一個學生.
My script takes 35 student names from the Sheet 'Studenti'. Then finds the proper folder with their name (i.e. 'Smith Peter'), removes old pdf document with old grades holding their name (i.e 'Smith Peter.pdf') and saves to the folder ('Smith Peter') a newly made pdf document with new grades ('Smith Peter.pdf'). And goes to take another student from the list.
問題:在 1,2 或 3 次迭代后,腳本停止說:訪問被拒絕:DriveApp.(第 22 行,文件代碼":files.next().setTrashed(true);).一周前腳本運行沒有問題,我嘗試在這里更改名稱、文件夾、目的地、源表和檢查建議.但沒有成功.我不知道第22行有什么問題.請幫忙!
The problem: after 1,2 or 3 iterations the script stops saying: Access denied: DriveApp. (line 22, file "Code": files.next().setTrashed(true);). A week ago the script was working without problems, I tried to change names, folders, destinations, source sheets and checked advice here. But no success. I don't know what is wrong with the line 22. Please help!
附上代碼
執行腳本(顯示腳本停止的位置)
Execution transcript (shows where the script has stopped)
function generatePdf() {
var report = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // Gradebook ready to save as pdf
var zasobnik = SpreadsheetApp.openById('1eygLDH0iJoXfcVOMIin0hnKpMF59HszmXZosPKvwYWc');
var zasobsheet = zasobnik.getSheetByName("Studenti"); // list of 35 students with names, Folder IDs
var data = zasobsheet.getDataRange().getValues();
for (var i = 1; i < data.length; i++){
report.getRange('B2').setValue(data[i][0]); // setting student's name in a gradebook (first column)
report.getRange('B3').setValue(' '); // just to jump from B2 to B3
var pdf = DriveApp.getFileById('1WHwm7xK28Orj22RxaKWEsCl3UWvWxYUhRg4ZGf87-GQ'); // ID of Gradebook (IF of ActiveSpreadsheet)
var theBlob = pdf.getBlob().getAs('application/pdf').setName(data[i][0] + ".pdf");
var folder = DriveApp.getFolderById(data[i][2]); // third column
var files = DriveApp.getFilesByName(data[i][0] + ".pdf"); // pdf with old grades
while (files.hasNext()) {
files.next().setTrashed(true);
}
var newFile = folder.createFile(theBlob); // creating pdf with new grades in the student's folder
}
}
推薦答案
我也遇到了同樣的問題,即我不擁有的垃圾文件.我發現了
I have the same problem with trashing files, that I don't own. I found that
- 如果您在 getEditors() 或 getViewers() 列表中,我有解決方案:只需撤銷您自己的權限即可.file.revokePermissions(Session.getActiveUser());文件將消失.
- 如果文件設置了 isShareableByEditors(),則示例 1 不起作用 - 如果它為false",則您無法執行任何操作.
- 如果您已將文件添加到您的驅動器,該文件可供擁有鏈接的所有人公開訪問,則您無法撤消權限,因為您沒有權限.應用腳本返回訪問被拒絕.所以,我認為我可以 setTrashed(true).我錯了.Google 還返回拒絕訪問:DriveAp.
在案例 2 和 3 到應用程序腳本的情況下,我沒有找到從驅動器中刪除不需要的文件的方法.但是仍然可以通過單擊它們并選擇刪除"選項來實現.
I didn't find a way to remove unwanted files from my drive in case 2 and 3 thru apps script. But it's still possible by clicking on them and choosing option "Remove".
這篇關于學生成績冊.由于“拒絕訪問:DriveApp"而無法完成腳本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!