Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

javascript - Student Gradebook. Unable to complete script because of "Access denied: DriveApp"

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.

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!

The code is enclosed

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
 
 }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have the same problem with trashing files, that I don't own. I found that

  1. If you are on the list getEditors() or getViewers() I have the solution: Just revoke permission from yourself. file.revokePermissions(Session.getActiveUser()); and file will disappear.
  2. Example 1 doesn't work if file has set isShareableByEditors() - if it's "false", then you can't do anything.
  3. If you've added file to your drive which is publicly accessible for everyone with link, you can't revoke permissions, because you don't have one. app script return access denied. So, I thought that I can setTrashed(true). I was wrong. Google also return Access denied: DriveAp.

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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...