google-apps-scriptgoogle-appsgoogle-apps-for-education

OpenById Error when generate Google Doc from Google Form Submission


I am currently working in a Google Education domain and I would like to automatically generate a Google Doc via an onSubmit trigger from a Google Form. I have a template file that I would like to copy and then replace fields (e.g., {{name}}) with responses from the form.

Upon submission, the template Google doc is successfully copied and renamed, but I cannot open the doc to replace the text fields.

var templateFile = DriveApp.getFileById(id);
var targetFolder = DriveApp.getFolderById(id);
var newDocName = title + "_" + name + "_" + submissionDate;
var newFile = templateFile.makeCopy(newDocName, targetFolder);
var newFileId = newFile.getId();
var doc = DocumentApp.openById(newFileId);
var body = doc.getBody();

The doc is copied and renamed, but throws an error at "DocumentApp.openById"

You do not have permission to call DocumentApp.openById. Required permissions: https://www.googleapis.com/auth/documents at onSubmit(Code:81)

I don't know why because I am the owner of both the form and the template!


Solution

  • As one user said, you need to run the code (the function) directly from the editor at least once. This will prompt you the oauth consent screen so you can authorize the scopes needed for your code (drive and docs API scopes, in this case) and will update your project with these scopes.