등급별 가격 할인 계산

코딩 수준: 초급
시간: 10분
프로젝트 유형: 커스텀 함수

목표

  • 솔루션의 기능을 이해합니다.
  • 애플리케이션 내에서 Apps Script 서비스가 수행하는 작업을 이해 솔루션을 제공합니다
  • 스크립트를 설정합니다.
  • 스크립트를 실행합니다.

이 솔루션 정보

고객에게 등급별 가격 시스템을 제공하는 경우 이 커스텀 함수는 가격에 대한 할인 금액을 더 쉽게 계산할 수 있습니다.

하지만 기본 제공 함수인 SUMPRODUCT를 사용하여 등급별 가격을 책정할 수 있습니다. SUMPRODUCT를 사용하는 것이 이보다 더 복잡하고 덜 유연합니다. 커스텀 함수를 만들어 보겠습니다.

등급 가격 샘플의 스크린샷

작동 방식

계층형 가격 책정 모델은 상품 또는 서비스 비용이 자동으로 계산됩니다.

예를 들어 0달러에서 500달러 사이의 2개의 계층이 있고 하나는 10% 할인이 있고 하나는 $501~$1,000이고 하나는 20% 할인입니다. 할인을 계산해야 하는 총 가격이 $700인 경우 스크립트는 곱하다 첫 500달러는 10%, 나머지 200달러는 20% 증액하여 총 할인율 90달러입니다.

스크립트는 지정된 총 가격에 대해 등급 가격표를 참조하세요 총 가격에서 해당하는 각 비율 해당 부분에 해당 등급의 관련 비율을 곱합니다. 값으로 사용됩니다. 각 등급 계산의 합계가 결과에 표시됩니다.

Apps Script 서비스

이 솔루션은 다음 서비스를 사용합니다.

기본 요건

이 샘플을 사용하려면 다음과 같은 기본 요건이 필요합니다.

  • Google 계정 (Google Workspace 계정은 관리자의 승인이 필요함)
  • 인터넷에 액세스할 수 있는 웹브라우저

스크립트 설정

아래 버튼을 클릭하여 등급 가격 맞춤 기능의 사본을 만드세요. 스프레드시트입니다. 다음에 대한 Apps Script 프로젝트: 이 솔루션이 스프레드시트에 첨부되어 있습니다.
사본 만들기

스크립트 실행

  1. 복사한 스프레드시트의 16행 표에 샘플 가격이 표시되어 있습니다. Software as a Service (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 Developer Experts의 도움을 받아 유지관리합니다.

다음 단계