# 自定义函数快速入门

• 设置脚本。
• 运行脚本。

## 前提条件

• 能够访问互联网的网络浏览器。

## 设置脚本

1. 创建新电子表格
2. 在新电子表格中，依次选择菜单项扩展程序 > Apps 脚本
3. 删除脚本编辑器中的所有代码，然后粘贴以下代码。然后点击“Save”

```/**
* 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);
}
```

## 运行脚本

1. 切换回电子表格。
2. 在单元格中输入 `=salePrice(100,.2)`。第一个参数表示原价，第二个参数表示折扣百分比。如果您所在的位置使用小数逗号，则可能需要输入 `=salePrice(100;0,2)`

## 后续步骤

[{ "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":"其他" }]