คำนวณส่วนลดราคาตามลำดับชั้น
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ระดับการเขียนโค้ด: ผู้เริ่มต้น
ระยะเวลา: 10 นาที
ประเภทโปรเจ็กต์: ฟังก์ชันที่กำหนดเอง
วัตถุประสงค์
- ทำความเข้าใจว่าโซลูชันทำอะไรได้บ้าง
- ทำความเข้าใจสิ่งที่บริการ Apps Script ทำภายในโซลูชัน
- ตั้งค่าสคริปต์
- เรียกใช้สคริปต์
เกี่ยวกับโซลูชันนี้
หากคุณเสนอระบบการกำหนดราคาแบบเป็นชั้นสำหรับลูกค้า ฟังก์ชันที่กำหนดเองนี้
จะช่วยให้คำนวณจำนวนส่วนลดสำหรับราคาได้ง่ายขึ้น
แม้ว่าคุณจะใช้ฟังก์ชันในตัว SUMPRODUCT
เพื่อคำนวณการกำหนดราคาตามระดับได้ แต่การใช้ SUMPRODUCT
นั้นซับซ้อนกว่าและยืดหยุ่นน้อยกว่าฟังก์ชันที่กำหนดเองของโซลูชันนี้

วิธีการทำงาน
รูปแบบการกำหนดราคาแบบเป็นขั้นหมายความว่าต้นทุนของสินค้าหรือบริการจะลดลง
ตามจำนวนที่ซื้อ
ตัวอย่างเช่น สมมติว่าคุณมี 2 ระดับ โดยระดับหนึ่งมีช่วงตั้งแต่ $0-$500 และ
ลดราคา 10% ส่วนอีกระดับมีช่วงตั้งแต่ $501-$1,000 และลดราคา 20%
หากราคาที่คุณต้องคำนวณส่วนลดคือ $700 สคริปต์จะ
คูณ $500 แรกด้วย 10% และ $200 ที่เหลือด้วย 20% ซึ่งจะทำให้ได้ส่วนลดรวมเป็น
$90
สำหรับราคาเต็มที่ระบุ สคริปต์จะวนซ้ำผ่านระดับที่ระบุใน
ตารางการกำหนดราคาระดับ สำหรับราคาแต่ละส่วนของราคารวมที่อยู่ใน
ระดับหนึ่ง ระบบจะนำราคาส่วนนั้นมาคูณกับค่าเปอร์เซ็นต์ที่เชื่อมโยงกับระดับ
นั้น ผลลัพธ์คือผลรวมของการคำนวณของแต่ละระดับ
บริการ Apps Script
โซลูชันนี้ใช้บริการต่อไปนี้
- บริการสเปรดชีต - รับค่าที่ระบุและคำนวณสัดส่วนของค่าที่จะคูณด้วยส่วนลดเปอร์เซ็นต์ของแต่ละระดับ
ข้อกำหนดเบื้องต้น
หากต้องการใช้ตัวอย่างนี้ คุณต้องมีข้อกำหนดเบื้องต้นต่อไปนี้
- บัญชี Google (บัญชี Google Workspace อาจต้องได้รับการอนุมัติจากผู้ดูแลระบบ)
- เว็บเบราว์เซอร์ที่มีสิทธิ์เข้าถึงอินเทอร์เน็ต
ตั้งค่าสคริปต์
คลิกปุ่มด้านล่างเพื่อทำสำเนาสเปรดชีตฟังก์ชันที่กำหนดเองสำหรับการกำหนดราคาตามระดับ
โปรเจ็กต์ Apps Script สำหรับ
โซลูชันนี้แนบมากับสเปรดชีต
ทำสำเนา
เรียกใช้สคริปต์
- ในสเปรดชีตที่คัดลอก ตารางที่แถว 16 จะแสดงตัวอย่างการคำนวณราคา
สำหรับผลิตภัณฑ์ซอฟต์แวร์เป็นบริการ (SaaS)
- หากต้องการคำนวณจำนวนส่วนลด ให้ป้อน
=tierPrice(C19,$B$3:$D$6)
ในเซลล์ C20
ราคาสุดท้ายจะอัปเดตในเซลล์ C21
หากคุณอยู่ในสถานที่ที่ใช้คอมมาเป็นจุดทศนิยม คุณอาจต้องป้อน =tierPrice(C19;$B$3:$D$6)
แทน
ตรวจสอบโค้ด
หากต้องการตรวจสอบโค้ด Apps Script สำหรับโซลูชันนี้ ให้คลิกดูซอร์สโค้ด
ด้านล่าง
การปรับเปลี่ยน
คุณสามารถแก้ไขฟังก์ชันที่กำหนดเองได้มากเท่าที่ต้องการเพื่อให้ตรงกับความต้องการของคุณ ด้านล่างนี้
เป็นส่วนเพิ่มเติมที่ไม่บังคับสำหรับการรีเฟรชผลลัพธ์ของฟังก์ชันที่กำหนดเองด้วยตนเอง
รีเฟรชผลลัพธ์ที่แคชไว้
Google จะแคชฟังก์ชันที่กำหนดเองเพื่อเพิ่มประสิทธิภาพ
ซึ่งต่างจากฟังก์ชันในตัว ดังนั้น หากคุณเปลี่ยนแปลงสิ่งใดก็ตามภายในฟังก์ชันที่กำหนดเอง
เช่น ค่าที่กำลังคำนวณ ระบบอาจไม่อัปเดตทันที
หากต้องการรีเฟรชผลลัพธ์ของฟังก์ชันด้วยตนเอง ให้ทำตามขั้นตอนต่อไปนี้
- เพิ่มช่องทำเครื่องหมายในเซลล์ว่างโดยคลิกแทรก
> ช่องทำเครื่องหมาย
- เพิ่มเซลล์ที่มีช่องทําเครื่องหมายเป็นพารามิเตอร์เพิ่มเติมของฟังก์ชันที่กําหนดเอง
เช่น หากคุณเพิ่มช่องทําเครื่องหมายในเซลล์
D20
ให้อัปเดต
ฟังก์ชัน tierPrice()
ในเซลล์ C20
เป็น
=tierPrice(C19,$B$3:$D$6,D20)
- เลือกหรือยกเลิกการเลือกช่องทำเครื่องหมายเพื่อรีเฟรชผลลัพธ์ของฟังก์ชันที่กำหนดเอง
ผู้ร่วมให้ข้อมูล
ตัวอย่างนี้ได้รับการดูแลโดย Google โดยได้รับความช่วยเหลือจาก Google Developer Expert
ขั้นตอนถัดไป
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 2025-08-31 UTC"],[[["\u003cp\u003eThis Google Apps Script custom function simplifies tiered pricing calculations in Google Sheets, offering a more straightforward approach than using \u003ccode\u003eSUMPRODUCT\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe function calculates discounts by iterating through pricing tiers, applying the corresponding percentage discount to the portion of the total price falling within each tier.\u003c/p\u003e\n"],["\u003cp\u003eTo use the function, you need to provide the total price and a table specifying the pricing tiers, start and end values, and discount percentages.\u003c/p\u003e\n"],["\u003cp\u003eThe script utilizes the Spreadsheet service to access and process data within the sheet.\u003c/p\u003e\n"],["\u003cp\u003eYou can manually refresh the custom function's results by adding a checkbox and including its cell reference as an additional parameter in the function call.\u003c/p\u003e\n"]]],[],null,["# Calculate a tiered pricing discount\n\n**Coding level** : Beginner \n\n**Duration** : 10 minutes \n\n**Project type** : [Custom function](/apps-script/guides/sheets/functions)\n\nObjectives\n----------\n\n- Understand what the solution does.\n- Understand what the Apps Script services do within the solution.\n- Set up the script.\n- Run the script.\n\nAbout this solution\n-------------------\n\nIf you offer a tiered pricing system for your customers, this custom function\nmakes it easier to calculate the discount amounts for your prices.\n\nThough you could use the built-in function `SUMPRODUCT` to make a tiered pricing\ncalculation, using `SUMPRODUCT` is more complex and less flexible than this\nsolution's custom function.\n\n### How it works\n\nA tiered pricing model means that the cost of goods or services goes down\nbased on the quantity purchased.\n\nFor example, imagine you have two tiers, one that ranges from $0-$500 and is\ndiscounted by 10% and one that ranges from $501-$1,000 and is discounted by 20%.\nIf the total price you need to calculate a discount for is $700, the script\nmultiplies\nthe first $500 by 10% and the remaining $200 by 20%, for a total discount of\n$90.\n\nFor a given total price, the script loops through the specified tiers in the\ntier pricing table. For each portion of the total price that falls\nwithin a tier, that portion is multiplied by the tier's associated percent\nvalue. The result is the sum of each tier's calculation.\n\n### Apps Script services\n\nThis solution uses the following service:\n\n- [Spreadsheet service](/apps-script/reference/spreadsheet)--Takes the given value and calculates what portion of the value to multiply by each tier's percent discount.\n\nPrerequisites\n-------------\n\nTo use this sample, you need the following prerequisites:\n\n- A Google Account (Google Workspace accounts might require administrator approval).\n- A web browser with access to the internet.\n\nSet up the script\n-----------------\n\nClick the button below to make a copy of the **Tier pricing custom function**\nspreadsheet. The Apps Script project for\nthis solution is attached to the spreadsheet.\n\n[Make a copy](https://docs.google.com/spreadsheets/d/13X-3pz8P_sfAyNqUJrGe0HdgzKARIMxC6Y6xZDAF7M0/copy)\n\nRun the script\n--------------\n\n1. In your copied spreadsheet, the table at row 16 shows a sample price calculation for a Software as a Service (SaaS) product.\n2. To calculate the discount amount, in cell `C20`, enter `=tierPrice(C19,$B$3:$D$6)`. The final price updates in cell `C21`. If you're in a location that uses decimal commas, you might need to enter `=tierPrice(C19;$B$3:$D$6)` instead.\n\nReview the code\n---------------\n\nTo review the Apps Script code for this solution, click **View source code**\nbelow: \n\n#### View source code\n\n### Code.gs\n\n\u003cbr /\u003e\n\nsolutions/custom-functions/tier-pricing/Code.js \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/solutions/custom-functions/tier-pricing/Code.js) \n\n```javascript\n// To learn how to use this script, refer to the documentation:\n// https://developers.google.com/apps-script/samples/custom-functions/tier-pricing\n\n/*\nCopyright 2022 Google LLC\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n https://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n*/\n\n/**\n * Calculates the tiered pricing discount. \n * \n * You must provide a value to calculate its discount. The value can be a string or a reference\n * to a cell that contains a string.\n * You must provide a data table range, for example, $B$4:$D$7, that includes the \n * tier start, end, and percent columns. If your table has headers, don't include\n * the headers in the range.\n * \n * @param {string} value The value to calculate the discount for, which can be a string or a \n * reference to a cell that contains a string.\n * @param {string} table The tier table data range using A1 notation.\n * @return number The total discount amount for the value.\n * @customfunction\n * \n */\nfunction tierPrice(value, table) {\n let total = 0;\n // Creates an array for each row of the table and loops through each array.\n for (let [start, end, percent] of table) {\n // Checks if the value is less than the starting value of the tier. If it is less, the loop stops.\n if (value \u003c start) {\n break;\n }\n // Calculates the portion of the value to be multiplied by the tier's percent value.\n let amount = Math.min(value, end) - start;\n // Multiplies the amount by the tier's percent value and adds the product to the total.\n total += amount * percent;\n }\n return total;\n}\n```\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nModifications\n-------------\n\nYou can edit the custom function as much as you'd like to fit your needs. Below\nis an optional addition to manually refresh custom function results. \n\n#### Refresh cached results\n\n\nUnlike built-in functions, Google caches custom functions to optimize\nperformance. Therefore, if you change something within your custom\nfunction, such as a value that's being calculated, it might not immediately\nforce an update. To refresh the function result manually, take the following\nsteps:\n\n1. Add a checkbox to an empty cell by clicking **Insert** \\\u003e **Checkbox**.\n2. Add the cell that has the checkbox as an extra parameter of the custom function. For example, if you add a checkbox to cell `D20`, update the `tierPrice()` function in cell `C20` to `=tierPrice(C19,$B$3:$D$6,D20)`.\n3. Check or uncheck the checkbox to refresh the custom function results.\n\nContributors\n------------\n\nThis sample is maintained by Google with the help of Google Developer Experts.\n\nNext steps\n----------\n\n- [Custom functions in Google Sheets](/apps-script/guides/sheets/functions)\n- [Extending Google Sheets](/apps-script/guides/sheets)"]]