計算分級定價折扣

程式設計層級:新手
時間長度: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. 依序按一下「插入」>「核取方塊」,在空白儲存格中加入核取方塊。
  2. 新增含有核取方塊的儲存格,做為自訂函式的額外參數。舉例來說,如果您在儲存格 D20 中新增核取方塊,請將儲存格 C20 中的 tierPrice() 函式更新為 =tierPrice(C19,$B$3:$D$6,D20)
  3. 勾選或取消勾選核取方塊,即可重新整理自訂函式結果。

貢獻者

這個範例由 Google 開發人員專家協助維護。

後續步驟