عملکرد سفارشی شروع سریع
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
میتوانید از Google Apps Script برای نوشتن یک عملکرد سفارشی استفاده کنید، سپس از آن در Google Sheets درست مانند یک عملکرد داخلی استفاده کنید.
نمونه شروع سریع زیر یک تابع سفارشی ایجاد می کند که قیمت فروش اقلام تخفیف خورده را محاسبه می کند. قیمت فروش به صورت دلار آمریکا درج شده است.
اهداف
- اسکریپت را تنظیم کنید
- اسکریپت را اجرا کنید.
پیش نیازها
برای استفاده از این نمونه به پیش نیازهای زیر نیاز دارید:
- یک حساب Google (حسابهای Google Workspace ممکن است به تأیید سرپرست نیاز داشته باشند).
- یک مرورگر وب با دسترسی به اینترنت.
اسکریپت را تنظیم کنید
- یک صفحه گسترده جدید ایجاد کنید.
- از داخل صفحهگسترده جدید، مورد منو Extensions > 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
می شود.
مراحل بعدی
برای ادامه یادگیری نحوه گسترش Sheets با Apps Script، به منابع زیر نگاهی بیندازید:
جز در مواردی که غیر از این ذکر شده باشد،محتوای این صفحه تحت مجوز Creative Commons Attribution 4.0 License است. نمونه کدها نیز دارای مجوز Apache 2.0 License است. برای اطلاع از جزئیات، به خطمشیهای سایت Google Developers مراجعه کنید. جاوا علامت تجاری ثبتشده Oracle و/یا شرکتهای وابسته به آن است.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[[["\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)"]]