fx Google Sheets के साथ काम
ENहिन्दी

कस्टम फ़ंक्शन (=MYFUNCTION)

⏱ 15 मिनट

आप क्या सीखेंगे

  • अपना formula बनाना जो =SUM() की तरह काम करे
  • ranges लेना और कई values लौटाना
  • @customfunction से help text जोड़ना
  • custom functions क्या नहीं कर सकते

समझिए

आपका पहला custom function

Bound script में (Extensions → Apps Script):

/**
 * Adds GST to an amount.
 *
 * @param {number} amount The amount before tax.
 * @param {number} rate The GST rate, e.g. 0.03 for 3%.
 * @return {number} The amount including GST.
 * @customfunction
 */
function WITH_GST(amount, rate) {
  return amount * (1 + rate);
}

Save करें, शीट पर लौटें और लिखें =WITH_GST(48500, 0.03) → 49955।

@customfunction वाला comment block आपके function को description के साथ formula autocomplete में दिखाता है। Function के नाम आमतौर पर capital letters में लिखे जाते हैं ताकि built-in formulas जैसे दिखें।

Range लेना

A2:A10 जैसी range देने पर function को 2D array मिलता है। एक cell देने पर एक value मिलती है। दोनों संभालें:

/**
 * Converts weight in grams to tola (1 tola = 11.6638 g).
 *
 * @param {number|Array<Array<number>>} grams Weight in grams, or a range.
 * @return Weight in tola, rounded to 3 decimals.
 * @customfunction
 */
function TOLA(grams) {
  const convert = g => (typeof g === 'number' ? Math.round((g / 11.6638) * 1000) / 1000 : '');
  if (Array.isArray(grams)) {
    return grams.map(row => row.map(convert));
  }
  return convert(grams);
}

=TOLA(B2) एक value देता है; =TOLA(B2:B20) नीचे 19 cells भर देता है — array formula की तरह।

कई values लौटाना

नतीजे कई cells में फैलाने के लिए 2D array लौटाएँ:

/**
 * Splits a full name into first and last name.
 * @param {string} fullName
 * @return First and last name in two cells.
 * @customfunction
 */
function SPLIT_NAME(fullName) {
  const parts = String(fullName).trim().split(/\s+/);
  const first = parts.shift() || '';
  return [[first, parts.join(' ')]];   // one row, two columns
}

नियम और सीमाएँ

Custom functions हर बार शीट recalculate होने पर चलते हैं, इसलिए Google इन पर पाबंदियाँ रखता है:

  • इन्हें लगभग 30 सेकंड में पूरा होना चाहिए।
  • ये सिर्फ़ value लौटा सकते हैं — दूसरे cells, formatting नहीं बदल सकते, न शीट बना सकते हैं।
  • ये ऐसी services इस्तेमाल नहीं कर सकते जिन्हें आपकी निजी permission चाहिए: GmailApp, DriveApp नहीं, दूसरी spreadsheets खोलना नहीं। (UrlFetchApp चलता है।)
  • ये तभी recalculate होते हैं जब इनके inputs बदलें। बिना input वाला या new Date() पढ़ने वाला function अपने-आप refresh नहीं होगा।
  • धीमे function की सैकड़ों copies शीट को सुस्त बना देती हैं — 500 अलग =TOLA(B2) cells की जगह range पर एक call (=TOLA(B2:B500)) बेहतर है।

अगर email भेजना है या दूसरे cells बदलने हैं तो menu item या trigger इस्तेमाल करें (अगले lessons में)।

मुख्य बातें

  • Bound script का कोई भी function cell से call किया जा सकता है।
  • Ranges 2D arrays के रूप में आती हैं; कई cells भरने के लिए 2D array लौटाएँ।
  • autocomplete help के लिए @customfunction जोड़ें।
  • Custom functions सिर्फ़ गणना करके value लौटाते हैं — और कुछ नहीं बदलते।

अभ्यास

अभ्यासअभ्यास 1

=MAKING_CHARGE(weight, ratePerGram, percent) लिखिए जो weight × ratePerGram × percent लौटाए। इसे एक cell और weights की range — दोनों के लिए चलने लायक बनाइए।

प्रश्नोत्तरी

A2:A5 देने पर custom function को क्या मिलता है? a) एक string b) एक 2D array c) सिर्फ़ A2
एक 2D array
इनमें से custom function क्या कर सकता है? a) email भेजना b) दूसरे cell का रंग बदलना c) गणना करके value लौटाना
गणना करके value लौटाना
कस्टम फ़ंक्शन (=MYFUNCTION) · हिंदी | ExcelWalaa