计算分层价格折扣

编码级别:新手
时长: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 开发者专家的帮助下进行维护。

后续步骤