计算分层价格折扣

编码级别:初级
时长: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. 点击插入可在空白单元格中添加复选框 &gt; 复选框
  2. 将包含复选框的单元格添加为自定义参数的额外参数 函数。例如,如果您在单元格 D20 中添加复选框,则更新 将单元格 C20 中的 tierPrice() 函数 =tierPrice(C19,$B$3:$D$6,D20)
  3. 选中或取消选中该复选框可刷新自定义函数结果。

贡献者

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

后续步骤