自訂函式快速入門導覽課程
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用 Google Apps Script 編寫自訂函式,然後在函式中使用
就像內建函式一樣
下列快速入門導覽課程範例會建立自訂函式,以計算
折扣商品的特價。特價格式應為美元。
目標
必要條件
如要使用這個範例,您必須具備下列先決條件:
- Google 帳戶 (Google Workspace 帳戶可能會
需要管理員核准)。
- 可存取網際網路的網路瀏覽器。
設定指令碼
- 建立新的新
試算表。
- 在新試算表中選取選單項目
「擴充功能」>「Apps Script」。
刪除指令碼編輯器中的任何程式碼,然後貼上下方的程式碼。接著
按一下「儲存」圖示 。
/**
* Calculates the sale price of a value at a given discount.
* The sale price is formatted as US dollars.
*
* @param {number} input The value to discount.
* @param {number} discount The discount to apply, such as .5 or 50%.
* @return The sale price formatted as USD.
* @customfunction
*/
function salePrice(input, discount) {
let price = input - (input * discount);
let dollarUS = Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
return dollarUS.format(price);
}
執行指令碼
- 切換回試算表。
- 在儲存格中輸入
=salePrice(100,.2)
。第一個參數代表
第二個參數則代表折扣百分比。
如果您位於使用小數點的地區,您可能需要輸入
請改為使用「=salePrice(100;0,2)
」。
您在儲存格中輸入的公式會執行
產生一個指令碼函式會促成交易
價格:$80.00
。
後續步驟
如要繼續學習如何使用
Apps Script
請參閱下列資源:
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-09-10 (世界標準時間)。
[{
"type": "thumb-down",
"id": "missingTheInformationINeed",
"label":"缺少我需要的資訊"
},{
"type": "thumb-down",
"id": "tooComplicatedTooManySteps",
"label":"過於複雜/步驟過多"
},{
"type": "thumb-down",
"id": "outOfDate",
"label":"過時"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"翻譯問題"
},{
"type": "thumb-down",
"id": "samplesCodeIssue",
"label":"示例/程式碼問題"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"其他"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"容易理解"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"確實解決了我的問題"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"其他"
}]
{"lastModified": "\u4e0a\u6b21\u66f4\u65b0\u6642\u9593\uff1a2024-09-10 (\u4e16\u754c\u6a19\u6e96\u6642\u9593)\u3002"}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2024-09-10 (世界標準時間)。"]]