با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
سرویس HTML به شما امکان می دهد صفحات وب را ارائه دهید که می توانند با عملکردهای برنامه های اسکریپت سمت سرور تعامل داشته باشند. این به ویژه برای ساخت برنامه های وب یا افزودن رابط های کاربری سفارشی در Google Docs، Sheets و Forms مفید است. حتی می توانید از آن برای تولید متن ایمیل استفاده کنید.
فایل های HTML ایجاد کنید
برای افزودن یک فایل HTML به پروژه Apps Script خود، این مراحل را دنبال کنید:
ویرایشگر Apps Script را باز کنید.
در سمت چپ، روی Add a file add > HTML کلیک کنید.
در فایل HTML، می توانید اکثر استانداردهای HTML، CSS و جاوا اسکریپت سمت کلاینت را بنویسید. این صفحه بهعنوان HTML5 ارائه میشود، اگرچه برخی از ویژگیهای پیشرفته HTML5 در دسترس نیستند، همانطور که در محدودیتها توضیح داده شده است.
فایل شما همچنین میتواند شامل اسکریپتهای قالب باشد که قبل از ارسال صفحه برای کاربر در سرور پردازش میشوند - مشابه PHP - همانطور که در بخش HTML قالب توضیح داده شده است.
HTML را به عنوان یک برنامه وب ارائه دهید
برای ایجاد یک برنامه وب با سرویس HTML، کد شما باید دارای یک تابع doGet() باشد که به اسکریپت می گوید چگونه صفحه را ارائه دهد. تابع باید یک شی HtmlOutput برگرداند، همانطور که در این مثال نشان داده شده است.
Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
HTML را به عنوان رابط کاربری Google Docs، Sheets، Slides یا Forms ارائه دهید
اگر اسکریپت شما به فایل محدود شده باشد، سرویس HTML میتواند یک گفتگو یا نوار کناری را در Google Docs، Sheets، Slides یا Forms نمایش دهد. (در فرمهای Google، رابطهای کاربری سفارشی فقط برای ویرایشگری قابل مشاهده است که فرم را برای تغییر آن باز میکند، نه برای کاربری که فرم را برای پاسخگویی باز میکند.)
بر خلاف یک برنامه وب، اسکریپتی که یک رابط کاربری برای یک سند، صفحه گسترده یا فرم ایجاد می کند، به طور خاص به تابع doGet() نیاز ندارد، و شما نیازی به ذخیره نسخه ای از اسکریپت خود یا استقرار آن ندارید. در عوض، تابعی که رابط کاربری را باز می کند باید فایل HTML شما را به عنوان یک شی HtmlOutput به متدهای showModalDialog()) یا showSidebar() شی Ui برای سند، فرم یا صفحه گسترده فعال منتقل کند.
این مثالها شامل چند ویژگی اضافی برای راحتی است: تابع onOpen() یک منوی سفارشی ایجاد میکند که باز کردن رابط را آسان میکند، و دکمه موجود در فایل HTML یک روش close() ویژه از API google.script.host را برای بستن رابط فراخوانی میکند.
Code.gs
// Use this code for Google Docs, Slides, Forms, or Sheets.
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.createMenu('Dialog')
.addItem('Open', 'openDialog')
.addToUi();
}
function openDialog() {
var html = HtmlService.createHtmlOutputFromFile('Index');
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showModalDialog(html, 'Dialog title');
}
توجه داشته باشید که اولین باری که میخواهید این رابط کاربری را نمایش دهید، باید تابع onOpen() را به صورت دستی در ویرایشگر اسکریپت اجرا کنید یا پنجره ویرایشگر Docs، Sheets یا Forms را بارگیری مجدد کنید (که ویرایشگر اسکریپت را میبندد). پس از آن، هر بار که فایل را باز می کنید، منوی سفارشی باید در عرض چند ثانیه ظاهر شود. برای مشاهده رابط، گزینه Dialog > Open را انتخاب کنید.
تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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"]],["تاریخ آخرین بهروزرسانی 2025-08-29 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThe HTML service allows you to create web pages that interact with Apps Script functions, enabling you to build web apps or add custom interfaces to Google Docs, Sheets, and Forms.\u003c/p\u003e\n"],["\u003cp\u003eYou can build HTML files within your Apps Script project using standard HTML, CSS, and client-side JavaScript, enhancing the functionality of your applications.\u003c/p\u003e\n"],["\u003cp\u003eDeploying your script as a web app allows external access, or it can be embedded in a Google Site for integration with your existing web presence.\u003c/p\u003e\n"],["\u003cp\u003eFor Google Docs, Sheets, Slides, or Forms, the HTML service enables the creation of custom dialogs or sidebars, providing interactive elements within these applications.\u003c/p\u003e\n"],["\u003cp\u003eContainer-bound scripts can display user interfaces using the HTML service by invoking \u003ccode\u003eshowModalDialog()\u003c/code\u003e or \u003ccode\u003eshowSidebar()\u003c/code\u003e methods, offering a tailored user experience.\u003c/p\u003e\n"]]],[],null,["# HTML Service: Create and Serve HTML\n\nThe [HTML service](/apps-script/reference/html) lets you serve web pages that\ncan interact with server-side Apps Script functions. It is particularly useful\nfor building web apps or adding custom user interfaces in Google Docs, Sheets,\nand Forms. You can even use it to generate the body of an email.\n\nCreate HTML files\n-----------------\n\nTo add an HTML file to your Apps Script project, follow these steps:\n\n1. Open the Apps Script editor.\n2. At the left, click Add a file add \\\u003e **HTML**.\n\nWithin the HTML file, you can write most standard HTML, CSS, and client-side\nJavaScript. The page will be served as HTML5, although some advanced features of\nHTML5 are not available, as explained in\n[Restrictions](/apps-script/guides/html/restrictions).\n\nYour file can also include template scriptlets that are processed on the server\nbefore the page is sent to the user --- similar to PHP --- as explained in the\nsection on [templated HTML](/apps-script/guides/html/templates).\n\nServe HTML as a web app\n-----------------------\n\nTo create a web app with the HTML service, your code must include a `doGet()`\nfunction that tells the script how to serve the page. The function must return\nan [`HtmlOutput`](/apps-script/reference/html/html-output) object, as shown in\nthis example. \n\n### Code.gs\n\n```html\nfunction doGet() {\n return HtmlService.createHtmlOutputFromFile('Index');\n}\n```\n\n### Index.html\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n \u003chead\u003e\n \u003cbase target=\"_top\"\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n Hello, World!\n \u003c/body\u003e\n\u003c/html\u003e\n```\n\nOnce that basic framework is in place, all you have to do is\n[save a version of your script](/apps-script/guides/versions), then\n[deploy your script as a web app](/apps-script/execution_web_apps#deploying).\n\nAfter the script is deployed as a web app, you can also\n[embed it in a Google Site](/apps-script/guides/web#embed_your_web_app_in).\n\nServe HTML as a Google Docs, Sheets, Slides, or Forms user interface\n--------------------------------------------------------------------\n\nThe HTML service can display a [dialog or sidebar](/apps-script/guides/dialogs)\nin Google Docs, Sheets, Slides, or Forms if your script is\n[container-bound](/apps-script/guides/bound) to the file. (In Google Forms,\ncustom user interfaces are only visible to an editor who opens the form to\nmodify it, not to a user who opens the form to respond.)\n\nUnlike a web app, a script that creates a user interface for a document,\nspreadsheet, or form does not need a `doGet()` function specifically, and you do\nnot need to save a version of your script or deploy it. Instead, the function\nthat opens the user interface must pass your HTML file as an\n[`HtmlOutput`](/apps-script/reference/html/html-output) object to the\n`showModalDialog())` or `showSidebar()` methods of the\n[`Ui`](/apps-script/reference/base/ui) object for the active document, form, or\nspreadsheet.\n\nThese examples include a few extra features for convenience: the `onOpen()`\nfunction creates a [custom menu](/apps-script/guides/menus) that makes it easy\nto open the interface, and the button in the HTML file invokes a special\n`close()` method of the\n[`google.script.host`](/apps-script/guides/html/communication) API to close the\ninterface. \n\n### Code.gs\n\n```html\n// Use this code for Google Docs, Slides, Forms, or Sheets.\nfunction onOpen() {\n SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.\n .createMenu('Dialog')\n .addItem('Open', 'openDialog')\n .addToUi();\n}\n\nfunction openDialog() {\n var html = HtmlService.createHtmlOutputFromFile('Index');\n SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.\n .showModalDialog(html, 'Dialog title');\n}\n```\n\n### Index.html\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n \u003chead\u003e\n \u003cbase target=\"_top\"\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n Hello, World!\n \u003cinput type=\"button\" value=\"Close\"\n onclick=\"google.script.host.close()\" /\u003e\n \u003c/body\u003e\n\u003c/html\u003e\n```\n\nNote that the first time you want to display this user interface, you must\neither run the `onOpen()` function\n[manually in the script editor](/apps-script/execution_script_editor)\nor reload the window for the Docs, Sheets, or Forms editor (which will close the\nscript editor). After that, the custom menu should appear within a few seconds\nevery time you open the file. Choose **Dialog \\\u003e Open** to see the\ninterface."]]