Bắt đầu nhanh hàm tuỳ chỉnh
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bạn có thể sử dụng Google Apps Script để viết một hàm tuỳ chỉnh, sau đó sử dụng hàm đó trong Google Trang tính giống như một hàm tích hợp sẵn.
Mẫu hướng dẫn nhanh sau đây tạo một hàm tuỳ chỉnh để tính giá bán của các mặt hàng được chiết khấu. Giá ưu đãi được định dạng bằng đô la Mỹ.
Mục tiêu
- Thiết lập tập lệnh.
- Chạy tập lệnh.
Điều kiện tiên quyết
Để sử dụng mẫu này, bạn cần đáp ứng các điều kiện tiên quyết sau:
- Một Tài khoản Google (tài khoản Google Workspace có thể yêu cầu quản trị viên phê duyệt).
- Một trình duyệt web có quyền truy cập vào Internet.
Thiết lập tập lệnh
- Tạo bảng tính mới.
- Trong bảng tính mới, hãy chọn mục Tiện ích > Apps Script trong trình đơn.
Xoá mọi mã trong trình chỉnh sửa tập lệnh rồi dán mã bên dưới vào. Sau đó, nhấp vào biểu tượng Lưu
.
/**
* 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);
}
Chạy tập lệnh
- Chuyển về bảng tính.
- Nhập
=salePrice(100,.2)
vào một ô. Tham số đầu tiên biểu thị giá gốc và tham số thứ hai biểu thị tỷ lệ phần trăm chiết khấu.
Nếu ở một địa điểm sử dụng dấu phẩy thập phân, bạn có thể cần nhập =salePrice(100;0,2)
.
Công thức mà bạn nhập vào ô sẽ chạy hàm trong tập lệnh mà bạn đã tạo ở phần trước. Hàm này sẽ cho ra giá bán là $80.00
.
Các bước tiếp theo
Để tiếp tục tìm hiểu về cách mở rộng Trang tính bằng Apps Script, hãy xem các tài nguyên sau:
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eGoogle Apps Script allows you to create custom functions that can be used directly within Google Sheets, similar to built-in functions.\u003c/p\u003e\n"],["\u003cp\u003eThis tutorial demonstrates how to build a custom function that calculates and formats sale prices based on provided input and discount values.\u003c/p\u003e\n"],["\u003cp\u003eTo use the custom function, simply paste the provided code into the Apps Script editor linked within your Google Sheet and call it using \u003ccode\u003e=salePrice(input, discount)\u003c/code\u003e in any cell.\u003c/p\u003e\n"]]],[],null,["# Custom function quickstart\n\nYou can use Google Apps Script to write a custom function, then use it in\nGoogle Sheets just like a built-in function.\n\nThe following quickstart sample creates a custom function that calculates the\nsale price of discounted items. The sale price is formatted as US dollars.\n\nObjectives\n----------\n\n- Set up the script.\n- Run the script.\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\n1. Create a [new\n spreadsheet](https://sheets.google.com/create).\n2. From within your new spreadsheet, select the menu item **Extensions** \\\u003e **Apps Script**.\n3. Delete any code in the script editor and paste in the code below. Then\n click Save .\n\n ```scilab\n /**\n * Calculates the sale price of a value at a given discount.\n * The sale price is formatted as US dollars.\n *\n * @param {number} input The value to discount.\n * @param {number} discount The discount to apply, such as .5 or 50%.\n * @return The sale price formatted as USD.\n * @customfunction\n */\n function salePrice(input, discount) {\n let price = input - (input * discount);\n let dollarUS = Intl.NumberFormat(\"en-US\", {\n style: \"currency\",\n currency: \"USD\",\n });\n return dollarUS.format(price);\n }\n ```\n\nRun the script\n--------------\n\n1. Switch back to your spreadsheet.\n2. In a cell, enter `=salePrice(100,.2)`. The first parameter represents the original price and the second parameter represents the discount percentage. If you're in a location that uses decimal commas, you might need to enter `=salePrice(100;0,2)` instead.\n\nThe formula that you enter in the cell runs the function in the\nscript you created in the previous section. The function results in a sale\nprice of `$80.00`.\n\nNext steps\n----------\n\nTo continue learning about how to extend Sheets with\nApps Script, take a\nlook at the following resources:\n\n- [Spreadsheet custom functions](/apps-script/execution_custom_functions)\n- [Custom menus in Google Workspace](/apps-script/guides/menus)\n- [Extend Google Sheets](/apps-script/guides/sheets)"]]