2

I have a Spreadsheet and Apps Script in the background that is serving a function CONFIRM(cell). This function performs several actions in the background

Code that is causing trouble:

function convertPDF(docId){
  const targetDir = "Redacted";
  var sourceFile = DriveApp.getFileById(docId);
  let blob = sourceFile.getBlob().getAs('application/pdf');
  let docPDF = targetDir.createFile(blob);
  let fileName = templateFile.getName().replace(".", "");
  docPDF.setName(fileName + ".pdf");
  let filePDF = docPDF.getId();
  let mailContent = "Redacted
    to: user,
    subject: "Redacted "+diplomaId,
    htmlBody: mailContent,
    attachments: filePDF,
  })
  return "SENT";
}

THe problem is that I am getting an error:

Exception: You do not have permission to call DriveApp.getFileById. Required permissions: (https://www.googleapis.com/auth/drive.readonly || https://www.googleapis.com/auth/drive) (line 24).

Line 24:

var sourceFile = DriveApp.getFileById(docId);

I completely do not know what's the issue here. Script works fine when ran with data provided in the script, but when ran as a function, it does not work.

Rubén
  • 34,714
  • 9
  • 70
  • 166
BrejSki
  • 21
  • 1
  • 3

1 Answers1

3

Custom functions cannot use APIs that require authentication. All methods in the DriveApp class require authentication, and will thus not work. You will need to run your code through a custom menu item, a button or an installable trigger.

doubleunary
  • 13,842
  • 3
  • 18
  • 51