计算分层价格折扣

编码水平:初级
时长:10 分钟
项目类型自定义函数

目标

  • 了解解决方案的功能。
  • 了解 Apps 脚本服务在解决方案中的作用。
  • 设置脚本。
  • 运行脚本。

关于此解决方案

如果您为客户提供分级定价系统,则此自定义函数可让您更轻松地计算价格的折扣金额。

虽然您可以使用内置函数 SUMPRODUCT 进行分层定价计算,但与此解决方案的自定义函数相比,使用 SUMPRODUCT 更复杂且灵活性更低。

层级价格示例的屏幕截图

运作方式

分层定价模式是指商品或服务的费用会根据购买的数量而降低。

例如,假设您有两个层级,一个层级的价格范围为 0-500 美元,可享受 10% 的折扣;另一个层级的价格范围为 501-1,000 美元,可享受 20% 的折扣。如果您需要计算折扣的总价为 700 美元,该脚本会将前 500 美元乘以 10%,将剩余的 200 美元乘以 20%,得出总折扣为 90 美元。

对于给定的总价,脚本会在分层价格表中循环遍历指定层级。对于总价格中属于某个层级的每个部分,该部分都会乘以相应层级的百分比值。结果是每个层级的计算结果之和。

Apps 脚本服务

此解决方案使用以下服务:

  • 电子表格服务 - 接受给定的值,并计算要将该值的哪一部分乘以每个层级的折扣百分比。

前提条件

如需使用此示例,您需要满足以下前提条件:

  • Google 账号(Google Workspace 账号可能需要管理员批准)。
  • 可访问互联网的网络浏览器。

设置脚本

点击下方的按钮,复制分级定价自定义函数电子表格。此解决方案的 Apps 脚本项目已附加到电子表格。
制作副本

运行脚本

  1. 在您复制的电子表格中,第 16 行的表格显示了软件即服务 (SaaS) 产品的价格计算示例。
  2. 如需计算折扣金额,请在单元格 C20 中输入 =tierPrice(C19,$B$3:$D$6)。最终价格更新位于单元格 C21 中。如果您所在的地区使用英文逗号作为小数点,可能需要输入 =tierPrice(C19;$B$3:$D$6)

查看代码

如需查看此解决方案的 Apps 脚本代码,请点击下方的查看源代码

查看源代码

Code.gs

solutions/custom-functions/tier-pricing/Code.js
// To learn how to use this script, refer to the documentation:
// https://developers.google.com/apps-script/samples/custom-functions/tier-pricing

/*
Copyright 2022 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
 * Calculates the tiered pricing discount.  
 *  
 * You must provide a value to calculate its discount. The value can be a string or a reference
 * to a cell that contains a string.
 * You must provide a data table range, for example, $B$4:$D$7, that includes the 
 * tier start, end, and percent columns. If your table has headers, don't include
 * the headers in the range.
 * 
 * @param {string} value The value to calculate the discount for, which can be a string or a 
 * reference to a cell that contains a string.
 * @param {string} table The tier table data range using A1 notation.
 * @return number The total discount amount for the value.
 * @customfunction
 *  
 */
function tierPrice(value, table) {
  let total = 0;
  // Creates an array for each row of the table and loops through each array.
  for (let [start, end, percent] of table) {
  // Checks if the value is less than the starting value of the tier. If it is less, the loop stops.
    if (value < start) {
      break;
    }
  // Calculates the portion of the value to be multiplied by the tier's percent value.
    let amount = Math.min(value, end) - start;
  // Multiplies the amount by the tier's percent value and adds the product to the total.
    total += amount * percent;
  }
  return total;
}

修改

您可以根据需要随意修改自定义函数。以下是手动刷新自定义函数结果的可选补充。

刷新缓存结果

与内置函数不同,Google 会缓存自定义函数以优化性能。因此,如果您更改自定义函数中的某些内容(例如正在计算的值),系统可能不会立即强制更新。如需手动刷新函数结果,请按以下步骤操作:

  1. 如需向空白单元格添加复选框,请依次点击插入 > 复选框
  2. 将包含复选框的单元格添加为自定义函数的额外参数。例如,如果您向单元格 D20 添加复选框,请将单元格 C20 中的 tierPrice() 函数更新为 =tierPrice(C19,$B$3:$D$6,D20)
  3. 选中或取消选中相应复选框,以刷新自定义函数结果。

贡献者

此示例由 Google 在 Google 开发者专家的帮助下维护。

后续步骤