I'm trying to create a document using google app scripts from a sheet. I have the following function
function createDoc() {
DocumentApp.create('TEST_DOCUMENT');
}
But when I run it I get the error
Exception: You do not have permission to call DocumentApp.create. Required permissions: https://www.googleapis.com/auth/documents (linje 26).
In my google app scripts appscript.json
I've added
"oauthScopes": [
"https://www.googleapis.com/auth/documents"
],
For the necessary permissions. But I'm still getting the same error. Anyone know why?
EDIT:
My createDoc() function (at the bottom)
How I call the createDoc() function
The error message
CodePudding user response:
From a Custom Function it is not possible to use functions that require authorization. As explained in the documentation:
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.
As a workaround I recommend you to add your function to a Custom Menu:
function onOpen() {
SpreadsheetApp
.getUi()
.createMenu('Custom Menu')
.addItem('Create new Doc', 'createDoc')
.addToUi()
}
function createDoc(){
DocumentApp.create(`My new Doc - ${new Date().toLocaleDateString()}`)
}