google-apps-script - 我可以以编程方式关闭 Google Apps 脚本库吗?
问题描述
所以我有一堆想要分发的电子表格函数。我实际上想为它们收费,只要应用内订阅是最新的,就可以保持它们的功能和可访问性,然后在续订之前使它们不可用。
因此,当我弄清楚如何为 Google Apps 脚本设置订阅机制时(这已经在某个地方解决了吗?),你如何阻止人们使用你的收藏?
我曾想过类似的事情
function WHATEVER() {
if (!Subscribed()) {
return new Error("Subscription not current");
}
// ... rest of function
}
我想这很好,但有更好的方法吗?
解决方案
Accessing persistent storage every time a function runs in order to test for whether the user is still subscribed or not, increases processing time. So, your Subscribed()
function should cache the current paid status in order to avoid reading Properties Service, (Or wherever you've stored the expiration date) every time a function runs.
But even using cache could be a burden. I'm not sure how much of a burden it would be, you'll need to test it or get user feedback.
You could also check the current paid status when a sidebar or dialog opens, or when the spreadsheet opens if you wanted to avoid running a test every time a function runs. But then there's the problem of how do you stop the function from running.
Because every function name is different, you'd need to add your if
statement to every function, but I don't think there is any alternative to having a test in every function.
You didn't show your Subscribed()
function, so I'll provide a suggestion.
function Subscribed_() {
var cache,usrProps,expireDate,todaysDate;
cache = CacheService.getUserCache();
todaysDate = new Date();
todaysDate = todaysDate.getTime();//Today's date in milliseconds
expireDate = cache.get('expDate');//Get the expiration date
if (expireDate === null) {//No value stored in cache
//Now look up persistent value
usrProps = PropertiesService.getUserProperties();
expireDate = usrProps.get('expDate');//Get the expiration date
}
if (!expireDate) {
return false;
}
expireDate = Number(expireDate);//Store the expiration date in milliseconds
if (todaysDate < expireDate) {
return true;
} else {
return false;
}
}
推荐阅读
- rust - 如何限制 futures::Stream::concat2 读取的字节数?
- android - 为什么所有的arraylist都存储在数据库中?
- mysql - 带有 node/express 和 mysql 的 mvc
- python - Openpyxl:“权限被拒绝”但 Excel 工作表未打开
- r - R - 每个邮政编码的二分法变量的比率,按邮政编码分组到新的 df
- mysql - 从 mysql 执行时找不到 Linux Bash 自定义函数
- r - “model.matrix”的内存高效表示
- javascript - 向节点服务器发布请求未命中端点
- javascript - 每次我在 GTM 中单击预览时都会收到验证容器弹出窗口和 Javascript 编译器错误?
- javascript - 从预选链接中选择链接并转到选择的链接一次