我可以使用具有只读权限的 DocumentApp.openById() 吗?
Can I use DocumentApp.openById() with read only permission?
我正在为 Google 电子表格创建一个 Google Apps 脚本插件,但它需要能够访问单独的 Google 文档的内容,我正在使用 DocumentApp.openById(). I have given the script these scopes:
"oauthScopes": [
"https://www.googleapis.com/auth/documents.readonly",
"https://www.googleapis.com/auth/script.container.ui",
"https://www.googleapis.com/auth/spreadsheets.currentonly"
]
但显然,这还不够。该脚本告诉我它需要 https://www.googleapis.com/auth/documents
权限才能正常工作。但是,当它只需要能够查看一个文件的内容时,授予附加组件编辑所有 Google 文档文件的权限似乎过分了。我错过了什么吗?有没有办法让它以只读方式访问单独的 Google 文档文件?
这是我用于测试的函数,大部分文档 ID 都被删除了:
function getDoc() {
var id = '1NLH----------------------------------------'
var templateFile = DocumentApp.openById(id)
var templateText = templateFile.getBody().getText()
Logger.log(templateText)
}
谢谢!
我相信你的目标如下。
您想使用以下脚本从 Google 文档中检索文本数据。
function getDoc() {
var id = '1NLH----------------------------------------'
var templateFile = DocumentApp.openById(id)
var templateText = templateFile.getBody().getText()
Logger.log(templateText)
}
您想使用 https://www.googleapis.com/auth/documents.readonly
和 Google Apps 脚本的范围来实现此目的。
问题和解决方法:
现阶段使用DocumentApp.openById
的Document服务,需要使用https://www.googleapis.com/auth/documents
的范围。这似乎是当前的规范。因此,在这个答案中,作为一种解决方法,我想建议使用 Google Docs API 而不是 Document 服务。当使用Google Docs API时,你的脚本可以使用https://www.googleapis.com/auth/documents.readonly
的范围来实现。
当您使用Google Docs API修改上述脚本时,它变成如下。
示例脚本:
在您使用此脚本之前,please enable Google Docs API at Advanced Google services。此脚本仅适用于 https://www.googleapis.com/auth/documents.readonly
.
的范围
function myFunction() {
const documentId = "###"; // Please set the Document ID.
const obj = Docs.Documents.get(documentId);
const text = obj.body.content.reduce((s, c) => {
if (c.paragraph && c.paragraph.elements) {
s += c.paragraph.elements.map(e => e.textRun.content).join("");
}
return s;
}, "");
console.log(text)
}
参考:
我正在为 Google 电子表格创建一个 Google Apps 脚本插件,但它需要能够访问单独的 Google 文档的内容,我正在使用 DocumentApp.openById(). I have given the script these scopes:
"oauthScopes": [
"https://www.googleapis.com/auth/documents.readonly",
"https://www.googleapis.com/auth/script.container.ui",
"https://www.googleapis.com/auth/spreadsheets.currentonly"
]
但显然,这还不够。该脚本告诉我它需要 https://www.googleapis.com/auth/documents
权限才能正常工作。但是,当它只需要能够查看一个文件的内容时,授予附加组件编辑所有 Google 文档文件的权限似乎过分了。我错过了什么吗?有没有办法让它以只读方式访问单独的 Google 文档文件?
这是我用于测试的函数,大部分文档 ID 都被删除了:
function getDoc() {
var id = '1NLH----------------------------------------'
var templateFile = DocumentApp.openById(id)
var templateText = templateFile.getBody().getText()
Logger.log(templateText)
}
谢谢!
我相信你的目标如下。
您想使用以下脚本从 Google 文档中检索文本数据。
function getDoc() { var id = '1NLH----------------------------------------' var templateFile = DocumentApp.openById(id) var templateText = templateFile.getBody().getText() Logger.log(templateText) }
您想使用
https://www.googleapis.com/auth/documents.readonly
和 Google Apps 脚本的范围来实现此目的。
问题和解决方法:
现阶段使用DocumentApp.openById
的Document服务,需要使用https://www.googleapis.com/auth/documents
的范围。这似乎是当前的规范。因此,在这个答案中,作为一种解决方法,我想建议使用 Google Docs API 而不是 Document 服务。当使用Google Docs API时,你的脚本可以使用https://www.googleapis.com/auth/documents.readonly
的范围来实现。
当您使用Google Docs API修改上述脚本时,它变成如下。
示例脚本:
在您使用此脚本之前,please enable Google Docs API at Advanced Google services。此脚本仅适用于 https://www.googleapis.com/auth/documents.readonly
.
function myFunction() {
const documentId = "###"; // Please set the Document ID.
const obj = Docs.Documents.get(documentId);
const text = obj.body.content.reduce((s, c) => {
if (c.paragraph && c.paragraph.elements) {
s += c.paragraph.elements.map(e => e.textRun.content).join("");
}
return s;
}, "");
console.log(text)
}