计算分层价格折扣

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

目标

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

关于此解决方案

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

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

Google 表格,显示了分级定价计算。

运作方式

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

例如,假设您有两个级别,一个级别为 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 (const [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.
    const 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(C19,$B$3:$D$6,D20)tierPrice()
  3. 选中或取消选中复选框,以刷新自定义函数结果。

贡献者

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

后续步骤