تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
المكتبة هي مشروع نص برمجي يمكن إعادة استخدام دوالّه في نصوص برمجية أخرى.
الوصول إلى مكتبة
لتضمين مكتبة في مشروعك، يجب أن يكون لديك إذن وصول على مستوى العرض على الأقل. إذا لم تكن مؤلف المكتبة التي تريد تضمينها، تواصَل مع المؤلف واطلب منه منحك الإذن بالوصول إليها.
يجب أن يكون لديك معرّف النص البرمجي للمكتبة التي تريد تضمينها. عندما يكون لديك إذن الوصول إلى المكتبة، يمكنك العثور على رقم تعريف النص البرمجي في صفحة إعدادات المشروعsettings.
إضافة مكتبة إلى مشروع نص برمجي
على يمين محرّر Apps Script، انقر على "إضافة مكتبة" add بجانب "المكتبات".
في حقل "رقم تعريف النص البرمجي"، ألصِق رقم تعريف النص البرمجي للمكتبة.
انقر على بحث.
انقر على القائمة المنسدلة الإصدار واختَر إصدار المكتبة الذي تريد استخدامه.
تحقَّق مما إذا كان اسم "المعرّف" التلقائي هو الاسم الذي تريد استخدامه مع هذه المكتبة. هذا هو الاسم الذي يستخدمه النص البرمجي للإشارة إلى المكتبة. على سبيل المثال، إذا ضبطت القيمة على Test، يمكنك استدعاء إحدى طرق هذه المكتبة على النحو التالي: Test.libraryMethod().
انقر على إضافة.
استخدام مكتبة
استخدِم مكتبتك المضمّنة كما تستخدم خدمة تلقائية. على سبيل المثال، إذا كان Test هو المعرّف الخاص بمكتبتك، اكتب Test متبوعًا بنقطة مباشرةً للاطّلاع على قائمة الطرق في المكتبة.
يمكن فتح المستندات المرجعية لمكتبة مضمّنة باتّباع الخطوات التالية:
على يمين "محرّر النصوص البرمجية"، بجانب اسم المكتبة، انقر على "المزيد"
more_vert> الفتح في علامة تبويب جديدة.
إزالة مكتبة
على يمين "محرِّر النصوص البرمجية"، بجانب اسم المكتبة، انقر على "المزيد"
more_vert> "إزالة" > "إزالة المكتبة".
تعديل مكتبة
يمكنك تغيير إصدار المكتبة أو تعديل معرّفها.
على يمين المحرّر، ضِمن "المكتبات"، انقر على اسم المكتبة.
أدخِل التغييرات التي تريدها، وانقر على حفظ.
إنشاء مكتبة ومشاركتها
لاستخدام مشروع النص البرمجي ومشاركته كمكتبة، اتّبِع الخطوات التالية.
يجب مشاركة إذن الوصول على مستوى العرض على الأقل مع جميع المستخدمين المحتملين للمكتبة.
قدِّم لهؤلاء المستخدمين رقم تعريف النص البرمجي الذي يمكن العثور عليه في صفحة إعدادات
المشروعsettings.
أفضل الممارسات
في ما يلي بعض الإرشادات التي يجب اتّباعها عند كتابة مكتبة:
اختَر اسمًا ذا معنى لمشروعك لأنّه يُستخدَم كمعرّف تلقائي عندما يضمِّن الآخرون مكتبتك.
إذا كنت تريد أن تكون طريقة واحدة أو أكثر من طرق البرنامج النصي غير مرئية (أو غير قابلة للاستخدام) لمستخدمي المكتبة، يمكنك إنهاء اسم الطريقة بشرطة سفلية. مثلاً: myPrivateMethod_()
لا تظهر لمستخدمي المكتبة سوى الخصائص العامة القابلة للتعداد. ويشمل ذلك تعريفات الدوال والمتغيرات التي تم إنشاؤها خارج دالة باستخدام var والخصائص التي تم ضبطها بشكل صريح على الكائن العام. على سبيل المثال، يؤدي ضبط Object.defineProperty() على false مع enumerable إلى إنشاء رمز يمكنك استخدامه في مكتبتك، ولكن لا يمكن للمستخدمين الوصول إلى هذا الرمز.
إذا كنت تريد أن يستفيد مستخدمو مكتبتك من ميزة الإكمال التلقائي في محرّر النصوص البرمجية ومن المستندات التي يتم إنشاؤها تلقائيًا، يجب أن تتضمّن جميع الدوال مستندات بتنسيق JSDoc. وفي ما يلي مثال لذلك:
هناك نوعان من المراجع عند استخدام المكتبات: المراجع المشترَكة والمراجع غير المشترَكة. يعني المورد المشترَك أنّ كلاً من المكتبة والبرنامج النصي المتضمّن لديهما إذن وصول مدمج إلى مثيل المورد نفسه. يوضّح المخطّط التالي موردًا مشتركًا باستخدام مثال "سمات المستخدم":
يعني المورد غير المشترَك أنّ كلاً من المكتبة والنص البرمجي الذي يتضمّنها يمكنهما الوصول إلى نسخة المورد الخاصة بهما فقط. ومع ذلك، يمكن للمكتبة توفير إمكانية الوصول إلى مواردها غير المشترَكة من خلال توفير دوال صريحة تعمل عليها. في ما يلي مثال على دالة يمكنك تضمينها في مكتبتك لعرض "خصائص البرنامج النصي":
يوضّح المخطّط التالي موردًا غير مشترك باستخدام مثال "سمات البرنامج النصي":
يعرض هذا الجدول المراجع المشترَكة وغير المشترَكة للرجوع إليها:
المرجع
تمت المشاركة*
غير مشترك**
ملاحظات
قفل
يكون العنصر نفسه مرئيًا للجميع، بما في ذلك النصوص البرمجية، عند إنشائه في المكتبة.
خصائص النص البرمجي
يكون العنصر نفسه مرئيًا للجميع، بما في ذلك النصوص البرمجية، عند إنشائه في المكتبة.
ذاكرة التخزين المؤقت
يكون العنصر نفسه مرئيًا للجميع، بما في ذلك النصوص البرمجية، عند إنشائه في المكتبة.
العوامل التي تؤدي إلى الظهور
لا يتم تشغيل المشغّلات البسيطة التي تم إنشاؤها في المكتبة بواسطة النص البرمجي الذي يتضمّنها.
ScriptApp
UiApp
خصائص المستخدمين
سجلّ الأنشطة ونص التنفيذ
المواقع الإلكترونية وجداول البيانات والحاويات الأخرى
يؤدي طلب getActive() إلى عرض الحاوية التي تتضمّن النص البرمجي.
MailApp وGmailApp
* يعني ذلك أنّ المكتبة لا تتضمّن نسخة خاصة بها من الميزة أو المورد، بل تستخدم النسخة التي أنشأها النص البرمجي الذي استدعاها.
** هذا يعني أنّ المكتبة تتضمّن نسخة خاصة بها من المورد/الميزة
وأنّ جميع النصوص البرمجية التي تستخدم المكتبة تشارك في هذه النسخة
نفسها ويمكنها الوصول إليها.
اختبار مكتبة
لاختبار المكتبة، استخدِم عملية النشر الرئيسية. يمكن لأي مستخدم لديه إذن وصول على مستوى المحرِّر إلى النص البرمجي استخدام عملية النشر الرئيسية.
تصحيح أخطاء مكتبة
عند استخدام أداة تصحيح الأخطاء في مشروع يتضمّن مكتبة، يمكنك الانتقال إلى دالة في المكتبة المضمّنة. يظهر الرمز في أداة تصحيح الأخطاء في وضع العرض فقط وفي الإصدار الصحيح.
تاريخ التعديل الأخير: 2025-08-31 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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-31 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eLibraries in Apps Script enable code reuse across different scripts but can impact execution speed, especially in UI-heavy applications or add-ons.\u003c/p\u003e\n"],["\u003cp\u003eTo use a library, you need its Script ID and at least view-level access, then add it to your project specifying the version and identifier.\u003c/p\u003e\n"],["\u003cp\u003eLibraries are accessed like built-in services; use the identifier followed by a period to view available methods.\u003c/p\u003e\n"],["\u003cp\u003eWhen creating a library, use meaningful names, consider visibility of methods (using underscores for private ones), and provide JSDoc documentation for user-facing functions.\u003c/p\u003e\n"],["\u003cp\u003eLibraries and scripts share certain resources like User Properties, while others, like Script Properties, are not shared and require explicit functions for access.\u003c/p\u003e\n"]]],[],null,["# Libraries\n\nA library is a script project whose functions can be reused in other scripts.\n| **Warning:** A script that uses a library [doesn't run as quickly](/apps-script/guides/support/best-practices#avoid_libraries_in_ui-heavy_scripts) as it would if all the code were contained within a single script project. Although libraries can make development and maintenance more convenient, use them sparingly in projects where speed is critical. Because of this issue, library use should be limited in [add-ons](/workspace/add-ons/overview).\n\nGain access to a library\n------------------------\n\nTo include a library in your project you must have at least view-level\naccess to it. If you aren't the author of the library that you want to\ninclude, contact the author and request access.\n\nYou need the script ID of the library you want to include. When you have\naccess to the library, you can find the script ID on the **Project Settings**\nsettings page.\n\nAdd a library to your script project\n------------------------------------\n\n1. At the left of the Apps Script editor, next to \"Libraries,\" click Add a library add.\n2. In the \"Script ID\" field, paste in the script ID of the library.\n3. Click **Look up** . If you encounter an error, make sure that you have at least view-level access to the project that you're trying to include.\n4. Click the **Version** dropdown and select the version of the library to use.\n5. Check to see if the default \"Identifier\" name is the one that you want to use with this library. This is the name that your script uses to refer to the library. For example, if you set it to `Test` then you can call a method of that library as follows: `Test.libraryMethod()`. If you use an identifier name that matches the name of an already existing service, such as [`MailApp`](/apps-script/reference/mail/mail-app), or a previously added library, then the library you have added most recently overrides the existing service or library.\n6. Click **Add**.\n\nUse a library\n-------------\n\nUse your included library as you would use a default service. For\nexample, if `Test` is the identifier for your library, type\n`Test` immediately followed by a period to see the list of methods in the library.\n\nThe reference documentation for an included library can be opened by following\nthese steps:\n\nAt the left of the script editor, next to the library name, click More\nmore_vert **\\\u003e Open in a new tab**.\n\nRemove a library\n----------------\n\nAt the left of the script editor, next to the library name, click More\nmore_vert **\\\u003e Remove \\\u003e Remove library**.\n| If a library is deleted by the author you still need to remove it from your list of included libraries.\n\nUpdate a library\n----------------\n\nYou can change the version of the library or update its identifier.\n\n1. At the left of the editor, under \"Libraries,\" click the name of the library.\n2. Make your changes and click **Save**.\n\nCreate and share a library\n--------------------------\n\nTo use and share your script project as a library, follow the below steps.\n\n1. [Create a versioned deployment](/apps-script/guides/versions) of your script.\n2. Share at least view-level access with all potential users of the library.\n3. Give those users the script ID, which can be found on the **Project\n settings** settings page.\n\n### Best practices\n\nHere are some guidelines to follow when writing a library:\n\n1. Choose a meaningful name for your project since it's used as the default identifier when your library is included by others.\n2. If you want one or more methods of your script to not be visible (nor usable) to your library users, you can end the name of the method with an underscore. For example, `myPrivateMethod_()`.\n3. Only enumerable global properties are visible to library users. This includes function declarations, variables created outside a function with `var`, and properties explicitly set on the global object. For example, `Object.defineProperty()` with `enumerable` set to `false` creates a symbol you can use in your library, but this symbol isn't accessible by your users.\n4. If you want your library users to make use of the script editor autocomplete and\n the automatically generated documentation, you must have JSDoc-style\n documentation for all your functions. Here's an example:\n\n /**\n * Raises a number to the given power, and returns the result.\n *\n * @param {number} base the number we're raising to a power\n * @param {number} exp the exponent we're raising the base to\n * @return {number} the result of the exponential calculation\n */\n function power(base, exp) { ... }\n\n### Resource scoping\n\nThere are two types of resources when you are working with libraries: shared\nand not-shared. A shared resource means that both the library and the including\nscript have a built-in access to the same instance of the resource. The\nfollowing diagram illustrates a shared resource using the example of\nUser Properties:\n\nA not-shared resource means that both library and the including script have\nbuilt-in access only to their instance of the resource. However, a library can\nprovide access to its not-shared resources by having explicit functions that\noperate on them. Here is an example of a function that you would include in\nyour library to expose its Script Properties: \n\n function getLibraryProperty(key) {\n const scriptProperties = PropertiesService.getScriptProperties();\n return scriptProperties.getProperty(key);\n }\n\nThe following diagram illustrates a not-shared resource using the example of\nScript Properties:\n\nThis table lists the shared and not-shared resources for your reference:\n\n| Resource | Shared\\* | Not-Shared\\*\\* | Notes |\n|------------------------------------|----------|----------------|------------------------------------------------------------------------------------|\n| Lock | | | The same instance is visible to all including scripts when created in the library. |\n| Script Properties | | | The same instance is visible to all including scripts when created in the library. |\n| Cache | | | The same instance is visible to all including scripts when created in the library. |\n| Triggers | | | Simple triggers created in library are not triggered by the including script. |\n| ScriptApp | | | |\n| UiApp | | | |\n| User Properties | | | |\n| Logger and execution transcript | | | |\n| Sites, Sheets and other containers | | | A call to `getActive()` returns the container of the including script. |\n| MailApp and GmailApp | | | |\n\nTest a library\n--------------\n\nTo test your library, use the head deployment. Anyone who has editor-level\naccess to the script can use the head deployment.\n| You still need at least one version of the library saved.\n\nDebug a library\n---------------\n\nWhen you use the debugger in a project that includes a library you\ncan step into a function of the included library. The code shows up in\nthe debugger in view-only mode and at the right version."]]