Dave Barrowman
Dave Barrowman

Reputation: 13

Accessing Google Docs from GMail Add-On

I am having problems accessing Google Docs from a GMail Add-On.

I want to copy particular Google Docs file from my GMail Add-On, and then edit the new doc. I am able to create a new doc from my template doc, using:

  var docTemplate="<DOCUMENT ID>";
  var docName = "Generated Document 123";
  var copyId = DriveApp.getFileById(docTemplate).makeCopy(docName).getId();

However, when I try to open the file using this code:

  var copyDoc = DocumentApp.openById(copyId);

I get an error in the GMail Add-On pane saying "You do not have permission to call openById [line: 36, function: getContextualAddOn, file: Code]"

I've used this same code successfully in a Apps Script in Google Sheets, so I assume I haven't set up my permissions correctly.

In my manifest file, I set the oauthScopes values as:

  "oauthScopes": [
    "https://www.googleapis.com/auth/gmail.addons.execute",
    "https://www.googleapis.com/auth/gmail.readonly",
    "https://www.googleapis.com/auth/drive",
    "https://www.googleapis.com/auth/script.external_request"
  ]

The /auth/drive permission allows me to call DriveApp.getFileById(), but what different permission do I need to call DocumentApp.openById()? Or is there some other issue?

Any pointers / guidance here would be greatly appreciated!

Upvotes: 1

Views: 232

Answers (2)

Ronnie Headen
Ronnie Headen

Reputation: 269

Add this to your scopes: "https://www.googleapis.com/auth/documents"

Upvotes: 1

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17613

This is mentioned in Using Apps Script services:

Unlike most other types of Apps Scripts, custom functions never ask users to authorize access to personal data. Consequently, they can only call services that do not have access to personal data, specifically the following:

> Cannot open other spreadsheets (SpreadsheetApp.openById() or SpreadsheetApp.openByUrl())

Upvotes: 1

Related Questions