Ilj*_*lja 3 web-applications google-apps-script google-apps-script-web-application
我正在尝试通过URL触发应用程序脚本。
为此,我将脚本部署为Web App,然后在工作表上提供一个按钮,该按钮连接到访问硬编码URL的简单函数。
我的问题是,仅当Web App部署为时,此机制才有效Anyone, even anonymous
。我宁愿将其保留在公司域中,也就是已登录的用户。
但是,UrlFetchApp.fetch(url)
即使对于已登录的用户也无法使用。
如何使仅已登录的用户能够“运行” URL?
谢谢
我有一个类似的问题,所以这就是我所做的。
Anyone, even anonymous
首先,声明您打算允许访问的域-
var authorizedDomains = 'google.com,google.io,google.org'; // comma separated
// DO NOT add http or https
// Currently, this setup doesn't support sub-domains (as the regex has not been configured to handle that
Run Code Online (Sandbox Code Playgroud)
然后使用以下函数返回布尔值(true或false)-
function userAccess() {
var authorisedUser = false;
var emailRegex = /\@(.*)/;
var userDomain;
try {
userDomain = emailRegex.exec(activeUser)[1];
} catch (error) {
}
var authorizedDomainNames = authorizedDomains.split(',');
for (var i = 0; i < authorizedDomainNames.length; i++) {
if (authorizedDomainNames[i] == userDomain) {
authorisedUser = true;
break;
}
}
Logger.log(authorisedUser);
return authorisedUser;
}
Run Code Online (Sandbox Code Playgroud)
最后,在您的doGet(e)
函数中,在提供HTML文件之前,请使用IF
条件将其包装;如下内容-
function doGet(e) {
var htmlFile;
var title;
if (userAccess()) {
htmlFile = 'Index';
title = 'Index'
return HtmlService.createHtmlOutputFromFile(htmlFile).setTitle(title);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
157 次 |
最近记录: |