You do not have permission to call DocumentApp.openById

4,409 views
Skip to first unread message

Ollie Bednal

unread,
Mar 7, 2023, 8:49:36 PM3/7/23
to Google Apps Script Community
Hi there, trying to set up a script to automatically fill a template doc from a spreadsheet once a trigger is activated

I'm getting the error 
Exception: You do not have permission to call DocumentApp.openById. Required permissions: https://www.googleapis.com/auth/documents

I'm using my google enterprise workspace account
I replaced the real ID's with "ID"

function createNewGoogleDocs() {
  //This value should be the id of your document template that we created in the last step
  const googleDocTemplate = DriveApp.getFileById('id');
  
  //This value should be the id of the folder where you want your completed documents stored
  const destinationFolder = DriveApp.getFolderById('id')
  //Here we store the sheet as a variable
  const sheet = SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheetByName('Processed Data')
  
  //Now we get all of the values as a 2D array
  const rows = sheet.getDataRange().getValues();
  
  //Start processing each spreadsheet row
  rows.forEach(function(rowindex){
    //Here we check if this row is the headers, if so we skip it
    if (index === 0return;
    //Here we check if a document has already been generated by looking at 'Document Link', if so we skip it
    if (row[11]) return;
    //Using the row data in a template literal, we make a copy of our template document in our destinationFolder
    const copy = googleDocTemplate.makeCopy(`${row[0]} Representation Agreement` , destinationFolder)
    //Once we have the copy, we then open it using the DocumentApp
    const doc = DocumentApp.openById(copy.getId())
    //All of the content lives in the body, so we get that for editing
    const body = doc.getBody();
    //In this line we do some friendly date formatting, that may or may not work for you locale
    const friendlyDate = new Date(row[3]).toLocaleDateString();
    
    //In these lines, we replace our replacement tokens with values from our spreadsheet row
    body.replaceText('{{Name}}'row[0]);
    body.replaceText('{{Address}}'row[1]);
    body.replaceText('{{Email}}'row[2]);
    body.replaceText('{{Phone number}}'row[3]);
    body.replaceText('{{Baseline}}'row[4]);
    body.replaceText('{{Term 1}}'row[5]);
    body.replaceText('{{Term 1 Words}}'row[6]);
    body.replaceText('{{Term 2}}'row[7]);
    body.replaceText('{{Term 2 Words}}'row[8]);
    body.replaceText('{{Term 3}}'row[9]);
    body.replaceText('{{Term 3 Words}}'row[10]);
    
    //We make our changes permanent by saving and closing the document
    doc.saveAndClose();
    //Store the url of our new document in a variable
    const url = doc.getUrl();
    //Write that value back to the 'Document Link' column in the spreadsheet. 
    sheet.getRange(index + 16).setValue(url)
    
  })
  
}

Jonathan Butler

unread,
Mar 9, 2023, 5:45:53 PM3/9/23
to google-apps-sc...@googlegroups.com
Have you converted your script to a Google Cloud Platform (GCP) Project? When you do, all permissions need to be added manually and won't be updated automatically like it normally does in a Google Apps Script handled project. If that is not the issue, maybe you made the change but need to provide authorization. That second possibility is a stretch because I believe it has a different error.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/ab727ff2-ce35-4922-9011-3506cec41c7cn%40googlegroups.com.
Message has been deleted

Nerio Villalobos

unread,
Apr 12, 2023, 8:59:02 AM4/12/23
to google-apps-sc...@googlegroups.com
The error message suggests that the script needs authorization to access the DocumentApp service. To grant the necessary authorization, follow these steps:

Save the script by clicking on the floppy disk icon.
Run the createNewGoogleDocs function manually by clicking on the "Run" button in the toolbar or by selecting "Run" from the "Run" menu. This will prompt you to grant the necessary authorization.
Once the authorization has been granted, the createNewGoogleDocs function should be able to run automatically as a trigger.
Note that the first time you run the script, you may also need to grant authorization to access the Spreadsheet and Drive services if you have not done so already.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/ab727ff2-ce35-4922-9011-3506cec41c7cn%40googlegroups.com.


--
__________________________
Nerio Enrique Villalobos Morillo
Buenos Aires, Argentina
Reply all
Reply to author
Forward
0 new messages