我有客户端和服务器脚本,服务器脚本由客户端脚本调用,客户端脚本从UI元素调用。因此,我需要隐藏服务器脚本方法以使我的数据更安全。我怎样才能做到这一点?他们在文档中说,我们可以通过在方法名称后加上下划线来隐藏它,这称为实用程序功能,但如果这样做,则只能从其他服务器脚本而不是客户端脚本中调用它们。就我而言,应该从客户端脚本的方法中调用它们。
答案 0 :(得分:1)
您可以使服务器端帮助程序函数由客户端代码调用并调用服务器端私有函数。
Bruce Mcpherson在Using promises with Google Apps Script上对此进行了解释。
客户端代码:
/**
* @namespace Provoke
* promise management for async calls
*/
var Provoke =(function (ns) {
/**
* run something asynchronously
* @param {string} namespace the namespace (null for global)
* @param {string} method the method or function to call
* @param {...} the args
* @return {Promise} a promise
*/
ns.run = function (namespace,method) {
// the args to the server function
var runArgs = Array.prototype.slice.call(arguments).slice(2);
console.log(runArgs);
if (arguments.length<2) {
throw new Error ('need at least a namespace and method');
}
// this will return a promise
return new Promise(function ( resolve , reject ) {
google.script.run
.withFailureHandler (function(err) {
reject (err);
})
.withSuccessHandler (function(result) {
resolve (result);
})
.exposeRun (namespace,method,runArgs);
});
};
return ns;
})(Provoke || {});
服务器端代码:
/**
* used to expose memebers of a namespace
* @param {string} namespace name
* @param {method} method name
*/
function exposeRun (namespace, method , argArray ) {
var func = (namespace ? this[namespace][method] : this[method])
if (argArray && argArray.length) {
return func.apply(this,argArray);
}
else {
return func();
}
}
在客户端使用上述方法的方式是
Provoke.run ( 'Server', 'getData_' , 100)
.then (function (result) {
'//do something with the result
},
function (err) {
//do something with the error
});
答案 1 :(得分:0)
在函数名称的末尾使用下划线符号_
将其设为私有:
function iAmPrivate_() {
// safe to execute code
}
function iAmPublic() {
// I am not safe, do security checks before execute
}
了解更多:
https://developers.google.com/apps-script/guides/html/communication#private_functions