計算分級定價折扣

程式設計層級:入門
時間長度:10 分鐘
專案類型自訂函式

目標

  • 瞭解解決方案的功能。
  • 瞭解 Apps Script 服務在 解決方案
  • 設定指令碼。
  • 執行指令碼。

認識這項解決方案

如果您為客戶提供分級定價系統,這項自訂函式 可讓您更輕鬆地計算價格的折扣金額。

雖然您可以使用內建的函式「SUMPRODUCT」進行分級價格 計算,使用 SUMPRODUCT 比較複雜,也較不具有彈性 解決方案的自訂函式。

級別定價範例的螢幕截圖

運作方式

分級定價模式意味著 商品或服務的成本會降低 根據購買數量而定

舉例來說,假設您有兩個層級,分別是 $0 美元至 $500 美元, 並提供 10% 的折扣,另一項則介於 $501 到 $1,000 美元之間,可享 20% 折扣。 如果您需要計算折扣總額為 $700 美元,則指令碼 乘號 前 $500 美元乘以 10%,其餘 $200 美元乘以 20%,則總共有 $90 美元。

就指定的總價來說,指令碼會在 級別價目表針對總價的每一部分 比例會乘以級別的相關百分比 值。結果是各級別計算方式的總和。

Apps Script 服務

這項解決方案使用下列服務:

  • 試算表服務:採用指定的 然後計算分數中要乘以各區間的多少部分 百分比折扣。

必要條件

如要使用這個範例,您必須具備下列先決條件:

  • Google 帳戶 (Google Workspace 帳戶可能會 需要管理員核准)。
  • 可存取網際網路的網路瀏覽器。

設定指令碼

點選下方按鈕即可複製級別價格自訂函式 試算表。適用於下列應用程式的 Apps Script 專案: 這個解決方案已附加至試算表
建立副本

執行指令碼

  1. 在複製的試算表中,第 16 列的表格會顯示範例價格 軟體式服務 (SaaS) 產品的定價。
  2. 如要計算折扣金額,請在 C20 儲存格中輸入 =tierPrice(C19,$B$3:$D$6)。儲存格 C21 中的最終價格更新。如果您是 如果位置含有小數點,您可能需要輸入 請改為使用「=tierPrice(C19;$B$3:$D$6)」。

查看程式碼

如要查看這項解決方案的 Apps Script 程式碼,請按一下「查看原始碼」 如下:

查看原始碼

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 Developers 專家的協助下維護。

後續步驟