การเริ่มต้นใช้งานฟังก์ชันที่กำหนดเองอย่างรวดเร็ว
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
คุณสามารถใช้ Google Apps Script เพื่อเขียนฟังก์ชันที่กำหนดเอง แล้วนำไปใช้ใน
Google ชีตได้เหมือนกับฟังก์ชันในตัว
ตัวอย่างการเริ่มต้นใช้งานอย่างรวดเร็วต่อไปนี้จะสร้างฟังก์ชันที่กำหนดเองซึ่งคำนวณ
ราคาลดของสินค้าที่ลดราคา ราคาลดจะจัดรูปแบบเป็นสกุลเงินดอลลาร์สหรัฐ
วัตถุประสงค์
- ตั้งค่าสคริปต์
- เรียกใช้สคริปต์
ข้อกำหนดเบื้องต้น
หากต้องการใช้ตัวอย่างนี้ คุณต้องมีข้อกำหนดเบื้องต้นต่อไปนี้
- บัญชี 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)
ในเซลล์ พารามิเตอร์แรกแสดงถึง
ราคาเดิม และพารามิเตอร์ที่ 2 แสดงถึงเปอร์เซ็นต์ส่วนลด
หากคุณอยู่ในสถานที่ที่ใช้คอมมาเป็นจุดทศนิยม คุณอาจต้องป้อน
=salePrice(100;0,2)
แทน
สูตรที่คุณป้อนในเซลล์จะเรียกใช้ฟังก์ชันใน
สคริปต์ที่คุณสร้างไว้ในส่วนก่อนหน้า ฟังก์ชันนี้จะทำให้ราคาขายเป็น $80.00
ขั้นตอนถัดไป
หากต้องการเรียนรู้เพิ่มเติมเกี่ยวกับวิธีขยายชีตด้วย
Apps Script โปรดดูแหล่งข้อมูลต่อไปนี้
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-31 UTC
[null,null,["อัปเดตล่าสุด 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)"]]