首页 > 解决方案 > 我可以以编程方式关闭 Google Apps 脚本库吗?

问题描述

所以我有一堆想要分发的电子表格函数。我实际上想为它们收费,只要应用内订阅是最新的,就可以保持它们的功能和可访问性,然后在续订之前使它们不可用。

因此,当我弄清楚如何为 Google Apps 脚本设置订阅机制时(这已经在某个地方解决了吗?),你如何阻止人们使用你的收藏?

我曾想过类似的事情

function WHATEVER() {
    if (!Subscribed()) {
        return new Error("Subscription not current");
    }
    // ... rest of function
}

我想这很好,但有更好的方法吗?

标签: google-apps-scriptgoogle-sheets

解决方案


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;
  }

}

推荐阅读